Converting UTC to local - c++

My timezone is United States Eastern Standard Time which is 5 hours behind UTC. Given that:
struct tm t = { 0, 30, 15, 10, 3, 112, 0, 0, -1 };
time_t utc_in_timet = _mkgmtime(&t);
struct tm tt = { 0 };
localtime_s(&tt, &utc_in_timet);
tt is off by one hour when localtime_s returns. I have 11:30 in there instead of 10:30.
What am I missing?

I think it has something to do with daylight saving time. Are you sure your timezone currently is EST (-5) ? Because it seems your system should be using EDT (-4) ?

I have tried your code in my machine and it works correctly (my time zone is GMT+2). Since you are telling your system to check for daylight savings itself (the last parameter for the tm is -1), it is actually using EDT and is thus giving you GMT-4.
You can try replacing the month (3) with 2, so that the date would be March 10th, just before the daylight savings change; I bet you will get the expected 10:30 in that case.

Verify your local timezone. Both England (e.g. London) and the east coast of the U.S. are currently in daylight savings time so this looks to be the issue (as someone already mentioned). For the U.S. east coast EDT would be 4 hours different.

Thought the problem is the month is March: struct tm t = { 0, 30, 15, 10, 3, 112, 0, 0, -1 };
Then, it is daylight saving time issue. But as pointed below by Gorpik "months go from 0 to 11, so April is indeed 3".
So, i checked - it shows 18:30 in Haifa which is correct +2UTC.

Related

how to handle precise hours when using WIN32 _mktime on date with DST (daylight saving time)?

Let's say I develop a ticket application that runs on windows. A given ticket has a validity of 3 hours. Now if I want to print a ticket on 28th of March 2020 (GMT+1, Germany) at 11 PM (23:00:00) it should be valid until 2 AM the next morning. (I manipulate my system time for testing)
Problem is, on the 29th DST-change happens: at 2 AM time will be set to 3 AM.
Due to DST the ticket is only valid until 1 AM (so technically only 2 hours), even though the actual time-leap happens later that day.
Here is what I do:
for the current time I use struct tm myTime;
myTime.tm_mday; \* = 28 *\
myTime.tm_hour; \* = 23 *\
struct tm newTime;
newTime.tm_mday = myTime.tm_mday; \* also done for remaining fields *\
newTime.tm_hour = myTime.tm_hour + 3; \* = 26 *\
no problem so far. On any other day the 26 hours will be converted to the following day 2 AM.
But if I call time_t result = _mktime64( newTime ); in this specific case, the resulting timestamp (e.g. 1585440205) will have mday = 29 and hour = 1 (when converted)
Is there another option, that calculates the time hour-precise, so that my ticket-validity doesn't lose one hour? (I assume _mktime64 recognizes the DST-change and manipulates all times for the day, no matter if they are before or after the actual time change at 2 AM)

Why my Python timestamp to datetime conversion is wrong?

Portal epochconverter.com converts timestamp 1531423084013 to correct date of Thursday, July 12, 2018 3:18:04.013 PM GMT-04:00 DST. But in Python 2.7.12 I got below which is wrong
>>> timestamp=1531423084013
>>> time.ctime(timestamp).rsplit(' ', 1)[0]
'Wed Nov 12 00:06:53'
How to make it correct ?
1531423084013 is in milliseconds not is seconds.
As you can see from epochconverter.com the hour is : 3:18:04.013, so the seconds part is 4.013, this site handle time in seconds and in milliseconds (it seems when the input has 13 digits instead of 10 for time around nowadays).
But time.ctime() from python handle only time in seconds and this is why you get a wrong answer when you enter a time in milliseconds (in my system it throws an out of range).
So you must divide your time in milliseconds by 1000 :
time.ctime(1531423084)
'Thu Jul 12 21:18:04 2018'
(My time zone is UTC+0200)

Trouble rounding times in Stata

I am attempting to round times to the nearest 15 minute interval in Stata, so for instance Dec 31, 2017 23:58 would become Jan 01, 2018 00:00. I have time stored (based on my understanding of the documentation) as the number of milliseconds since the start of 1960. So I thought this would do it:
gen round = round(datetime, 60000*15)
However, this doesn't quite work. For instance Nov 03, 2017 19:45:27 becomes Nov 03, 2017 19:46:01, when I think I should become 19:45:00. Does anyone know what I'm missing here?
Let's show a worked example illustrating my comment that you need to store datetime values as double rather than float.
. clear
. set obs 1
number of observations (_N) was 0, now 1
. gen double datetime = clock("Nov 03, 2017 19:45:27","MDYhms")
. gen round_f = round(datetime, 60000*15)
. gen double round_d = round(datetime, 60000*15)
. format datetime round_f round_d %tc
. list, clean noobs
datetime round_f round_d
03nov2017 19:45:27 03nov2017 19:46:01 03nov2017 19:45:00

MySQL FROM_UNIXTIME command not writing

Here is my SQL query I am running:
string theQuery = "UPDATE readings SET chng = 1, time = FROM_UNIXTIME(";
theQuery += boost::lexical_cast<string>(ss.time);
theQuery += ") WHERE id = 1;";
ss.time is a uint32_t that records the number of seconds since 1 Jan 1970. When I attempt to put the value "3586767203" into the brackets of FROM_UNIXTIME, which is the time value on my device, it updates my time field to NULL. If I enter a smaller number it updates the time field fine.
Why is it updating to NULL if I am entering a valid time??
You've exceeded the limits of UNIXTIME. 3586767203 is 'Sun, 29 Aug 2083 12:13:23 GMT' and UNIXTIME can't be bigger than a date that resolves into '2038-01-18 22:14:07' ( FROM_UNIXTIME(2147483647) ) because the time is stored as a signed 32 bit integer of seconds after the epoch (Jan 1, 1970) and 2^31 seconds after the epoch is 'Tue, 19 Jan 2038 03:14:08 GMT'.
See http://en.wikipedia.org/wiki/Year_2038_problem for an explanation of this problem.

How to increment Date objects in C++

I have an assignees that I've been working on and I'm stuck on the last function.
use the function void Increment(int numDays = 1)
This function should move the date forward by the number of calendar days given in the argument. Default value on the parameter is 1 day. Examples:
Date d1(10, 31, 1998); // Oct 31, 1998
Date d2(6, 29, 1950); // June 29, 1950
d1.Increment(); // d1 is now Nov 1, 1998
d2.Increment(5); // d2 is now July 4, 1950
I don not understand how to do this.
void Date::Increment(int numDays = 1)
I'm stuck, I know how to tell the function to increment, by the ++ operator but i get confuse when I have to get the function to increment the last day of the month to the the fist, or to end at the last date of that month for example. Oct 31 to Nov 1, or June 29 to July 4. I can do July 5 to July 8 but the changing months confuse me
You will need to store a list (or array) of how many days are in each month. If you add numDays to the current date and it becomes bigger than this, you need to increment the month as well.
For example, we have a date object representing 29 March 2010. We call Increment(4) and add 4 to the day variable, ending up with 33 March 2010. We now check how many days March has and find out it's 31 (eg. daysInMonth[3] == 31). Since 33 is greater than 31, we need subtract 31 from 33 and increase the month, ending up with 2 April 2010.
You will need special handling for February in leap years (any year divisible by 4 and not divisible by 100 unless it's also divisible by 400) and for incrementing past the end of December.
30 days has September, April, June, and November. The rest have 31 days, except for February, which has 28 days except on a leap year (every 4 years, and 2008 was the last one) when it has 29 days.
This should be plenty to get you going.
First, construct a function like
int numDaysSinceBeginning( Date );
which counts number of days elapsed from a well known date (e.g. Jan 1 1900) to the specific Date.
Next, construct another function which converts that day-delta to Date
Date createDateWithDelta( int );
From your example,
Date d2(6, 29, 1950); // June 29, 1950
int d2Delta = numDaysSinceBeginnning( d2 );
Date d2Incremented = createDateWithDelta( d2Delta + 5 ); // d2Incremented is July 4, 1950