Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
n using the GetDynamicTimeZoneInformation(&dtzi) function, and within the 'dtzi' structure, it tells
you whether you're in the Daylight Savings time or not and when they will start, via
these two dates. The return value of this function also tells you about the same information.
But how does someone go about converting these daylightdate and/or standarddate date
structures into readable local date/time forms. May I ask how this is done in "C". I know there
must be some conversions, but how? Thanks though, and for your time.

What I have tried:

the trial and error method. Using examples found and then applying the found idea to the problem. Sometimes it doesn't quite work right and not at all.
Posted
Updated 10-Mar-24 19:17pm
v2

I'm making an assumption here that you are talking about converting the two SYSTEMTIME structures in the DYNAMIC_TIME_ZONE_INFORMATION object. To accomplish this, I believe that you are going to start by converting the SYSTEMTIME structure to a local file time using SystemTimeToLocalFileTime function. Once you have done this, you will use FileTimeToSystemTime to convert the local file time back to a SYSTEMTIME structure representing the date in local time. It might look something like this:
C
SYSTEMTIME standardDate = timeZoneInfo.StandardDate;

FILETIME localFileTime;
SystemTimeToLocalFileTime(&standardDate, &localFileTime);

SYSTEMTIME localStandardDate;
FileTimeToSystemTime(&localFileTime, &localStandardDate);

char dateString[20];
sprintf(dateString, "%d-%02d-%02dT%02d:%02d:%02dT", localStandardDate.wYear, localStandardDate.wMonth, localStandardDate.wDay, localStandardDate.wHour, localStandardDate.wMinute, localStandardDate.wSecond);
Note, I'm just creating this method in the editor here, so there maybe a typo or two here.
 
Share this answer
 
Comments
samtoad 11-Mar-24 13:14pm    
Hey Pete...,

In attempting to impliment your solution, the MSVC2019 compiler kicked out the function(SystemTimeToLocalFileTime) as
undefined... I didn't find any material on the subject(function); thinking that I was missing a certain ".h" include file,
but nope. Were you thinking of using another function? When I did the search on 'SystemTimeToLocalFileTime', it responded
back with a question saying is what you mean: System Time To Local File Time... I said yes, ...
It further goes into the topic of "FileToLocalFileTime". All I wanted to do is to convert two dates, within the timeZoneInfo area,
into a readable Date/Time forms and not get into any timezone stuff. Thanks though, Samtoad.
Pete O'Hanlon 11-Mar-24 14:43pm    
My apologies. As I said, I was doing this from memory. The API I was thinking of was this one. https://learn.microsoft.com/en-us/windows/win32/api/timezoneapi/nf-timezoneapi-systemtimetotzspecificlocaltimeex
you can make use of the 'SystemTimeToTzSpecificLocalTime' function to achieve what you are trying - MS Learn | SystemTimeToTzSpecificLocalTime function (timezoneapi.h)[^]

Your code will look like -
C
#include <Windows.h>
#include <stdio.h>

void PrintSystemTime(const SYSTEMTIME* st) {
    printf("%04d-%02d-%02d %02d:%02d:%02d\n", st->wYear, st->wMonth, st->wDay, st->wHour, st->wMinute, st->wSecond);
}

int main() {
    TIME_ZONE_INFORMATION dtzi;
    DWORD result = GetDynamicTimeZoneInformation(&dtzi);

    if (result == TIME_ZONE_ID_DAYLIGHT || result == TIME_ZONE_ID_STANDARD) {
        SYSTEMTIME daylightTime, standardTime;

        //Convert the daylightdate to local time...
        SystemTimeToTzSpecificLocalTime(NULL, &dtzi.DaylightDate, &daylightTime);

        //Convert your standarddate to local time...
        SystemTimeToTzSpecificLocalTime(NULL, &dtzi.StandardDate, &standardTime);

        printf("Daylight Saving Time starts: ");
        PrintSystemTime(&daylightTime);

        printf("Standard Time starts: ");
        PrintSystemTime(&standardTime);
    } else {
        printf("Unable to get time zone information.\n");
    }

    return 0;
}
 
Share this answer
 
Comments
samtoad 11-Mar-24 21:11pm    
In attempting to implement all these solutions, there have been no success'. All I'm trying to do is to convert these two dates, the standard and daylightsavings dates, into a systemtime/TM readable form and then onto human readable forms.
In reviewing these dates, both are of systemtime datatypes. It seems like everything is of "systemtime" datatypes and would seem easy, but not. Are there any working "C" examples showing these smooth conversions, so to avoid any confusion. I'll keep working on my side. Thanks for the continued help. Samtoad.
samtoad 12-Mar-24 12:51pm    
To all,

I finally figured the problem out, after experiences many trial_and_errors on one line at a time.
So stop scratching your head a little too hard. This is the solution...
The problem lies at line # "**".

printf(" We are in Std Time: %ws \n", dtzi.StandardName); /// The same approach with daylightdate.
** SEdaylightdate = dtzi.StandardDate; //// it worked.
///// I had to get the dtzi.StandardDate systemtime form into an external, but still in the
///// program, systemtime datatype. And the rest is history.
///// This piece of code is straight forward and very simple in approach.
SEdaylightdate.wYear = intccyrval; /// Only put in default CCYY value. I did this.
printf("Standard Time starts: ");
PrintSystemTime(&SEdaylightdate);
///// Here is the output. I put in the 2024 year value before printing.
///// Standard Time starts: 11-01-2024 ----- 02:00:00
///// this "date" data that MS provides is very cryptic. But the date is somewhat
///// there from what trial_ad _errors that I've found with and played with.

And again, many Thanks.
Andre Oosthuizen 12-Mar-24 13:23pm    
Well done, it feels great to solve a real head cruncher to a point where it works, be proud of yourself, keep it up!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900