Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
UTC time record 5 hours before start of Spring Day Light Saving, displays 1:00 to 1:59 UTC time duration am twice. First, from 8:00 pm to 8:59 pm and then again from 9:00 pm to 9:59 pm. UTC time becomes normal 3:00 am again at local 10 pm time.

when local time goes to 8:59 pm to 9:00 pm on 11 March '17, UTC time goes back from 1:59 am to 1:00 am and since then continue to record time upto 1:59 am. And when local time goes to 9:59 pm to 10:00 pm same day, UTC time records goes to 1:59 am to 3:00 am. It all happening before start of Day light saving (5 hours before in UTC-5 time zone).

Summary:
local time duration ----------------- UTC time duration
(11th March '17) --------------------- (12th March '17)
8:00pm to 8:59 pm -------------- 1:00 am to 1:59 am
9:00pm to 9:59 pm -------------- 1:00 am to 1:59 am
10:00 pm onwards --------------- 3:00 am onwards


What I have tried:

double TimeCalculationClass :: GetUTCTime()
{
   struct tm*   starTime; 
   struct tm stmTime;
   time_t localTimestamp = (long)m_dTime;
   double dTime;
   SYSTEMTIME   sysTime, sysUTcST;
   FILETIME  locFileTime, utcFileTime;

   dTime = m_dTime;

   /* Convert to a SYSTEMTIME */
   sysTime.wSecond = (unsigned short)starTime->tm_sec;
   sysTime.wMinute = (unsigned short)starTime->tm_min;
   sysTime.wHour = (unsigned short)starTime->tm_hour;
   sysTime.wDay = (unsigned short)starTime->tm_mday;
   sysTime.wMonth = (unsigned short)(starTime->tm_mon + 1);
   sysTime.wYear = (unsigned short)(starTime->tm_year + 1900);
   sysTime.wMilliseconds = 0;

   /* convert to a local FileTime */
   SystemTimeToFileTime(&sysTime, &locFileTime);

   /* convert local filetime to a file time based on the Coordinated 
   Universal Time (UTC). */
   LocalFileTimeToFileTime(&locFileTime, &utcFileTime);

   /* convert utc filetime to utc system time */
   FileTimeToSystemTime(&utcFileTime, &sysUTcST);

   /* convert system time to struct tm */
   stmTime.tm_sec = sysUTcST.wSecond;
   stmTime.tm_min = sysUTcST.wMinute;
   stmTime.tm_hour = sysUTcST.wHour;
   stmTime.tm_mday = sysUTcST.wDay;
   stmTime.tm_mon = sysUTcST.wMonth - 1;
   stmTime.tm_year = sysUTcST.wYear - 1900;
   stmTime.tm_wday = sysUTcST.wDayOfWeek;
   stmTime.tm_yday = 0;
   stmTime.tm_isdst = -1;
   dTime = (double)mktime(&stmTime);

   //if (stTime.wMilliseconds != 0)
   dTime = dTime + ((double)sysTime.wMilliseconds / 1000);

   return dTime;
}
Posted
Updated 30-Oct-17 2:05am
v2

1 solution

See the SystemTimeToFileTime function (Windows)[^] documentation:
Quote:
Converts a system time to file time format. System time is based on Coordinated Universal Time (UTC).
and SYSTEMTIME structure (Windows)[^]:
Quote:
The time is either in coordinated universal time (UTC) or local time, depending on the function that is being called.

Your code creates a local SYSTEMTIME and passes that to SystemTimeToFileTime which requires an UTC time. You have to call TzSpecificLocalTimeToSystemTime function (Windows)[^] first.

The general rule for handling times:
Always store them in UTC and perform all calculations (time span / duration) using UTC times. Use local time only for user interaction (input / output).
 
Share this answer
 

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