Why my Python timestamp to datetime conversion is wrong? - python-2.7

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)

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)

Time Difference is wrongly calculated in Rails 4

I am working on a project using Ruby 2.2 and Rails 4 and sqlite database in which time difference is wrongly calculated.
I have used TimeDifference method for that purpose.
In my database there is a table MachineAttendance.
InTime is stored as Tue, 12 Feb 2017 20:30:00 UTC +00:00
OutTime is stored as Tue, 12 Feb 2017 05:30:00 UTC +00:00
#c1.in_time.strftime("%I:%M %p")
"08:30 PM"
#c1.out_time.strftime("%I:%M %p")
"05:30 AM"
And my method is
time_diff=TimeDifference.between(#c1.in_time.strftime('%H:%M:%S'), #c1.out_time.strftime('%H:%M:%S')).in_hours.to_f
its giving 15.0 hrs which is wrong
as it should be 9.0 hrs.
will anyone let me know how may i get correct difference.....thanks :)
You can try below its work for me
Time.at((#c1.out_time-#c1.in_time).round.abs).utc.strftime "%H:%M"
It will return time like "09:00"

Convert string datetime format into integer time in seconds in python

My input is input_time = "May 5 2016 11:29:32".
Expected output should be in seconds or milli seconds which is type of integer i.e., output_time = 2424241313113.
The above conversion should be done in python. How to do this conversion?
Here's how to convert date time into epoch seconds (dated starting from 00:00:00 UTC on 1 January 1970)
In Python 3.3+
from datetime import datetime
datetime.strptime('May 5 2016 11:29:32','%b %d %Y %H:%M:%S').timestamp()
In Python 2.7.9
datetime.strptime('May 5 2016 11:29:32','%b %d %Y %H:%M:%S').strftime('%s')
Note that strftime('%s') use your local time zone.

Coldfusion crontime incorrectly running on weekend

I have a scheduled task that needs to run three times a day, on each weekday. The setup surrounding the task is Coldfusion, and it is in the Crontime format. It should run at 11:30, 15:45 and 18:30 server time.
For some reason the task is occasionally running on weekends, which it should not do.
Here are the three strings for each of the days:
0 30 11 ? * 1-5
0 45 15 ? * 1-5
0 30 18 ? * 1-5
Can anyone point out to me why the task is sometimes running on weekends? Is there a mistake in my string?
The Coldfusion crontime documentation can be found here:
According to This, 1 = Sunday.
Days-of-Week can be specified as values between 1 and 7 (1 = Sunday) or by using the strings SUN, MON, TUE, WED, THU, FRI and SAT.
Try replacing 1-5 with MON-FRI?
An example of a complete cron-expression is the string "0 0 12 ? * WED" - which means "every Wednesday at 12:00:00 pm".
Individual sub-expressions can contain ranges and/or lists. For example, the day of week field in the previous (which reads "WED") example could be replaced with "MON-FRI", "MON,WED,FRI", or even "MON-WED,SAT".

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.