I have logs generated in Java with the form yyyyMMdd:HH:mm:ss:SSS
SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMdd:HH:mm:ss:SSS");
String endDate = sDateFormat.format(new Date()).toString();
log("[" + endDate + "]" + " worker Thread ends);
This gives me the local time.
[20151028:19:38:33:920] worker Thread ends
As part of the logs, I also have unix timestamps.
{"endTime":1446075513000}
The timestamp and the date string match up perfectly. However, in some of the logs, I'm missing the JSON line. So I'm having to reconstruct the timestamp from the date string.
dt = datetime.datetime.strptime('20151028:19:38:33:920', "%Y%m%d:%H:%M:%S:%f")`
epoch = datetime.datetime.fromtimestamp(0)
timestamp = (dt - epoch).total_seconds() # 1446079113.92
Now, if i do test_dt = datetime.datetime.fromtimestamp(timestamp)
, I get datetime.datetime(2015, 10, 28, 20, 38, 33, 920000)
I'm assuming this is because python thinks my dt
was time in UTC; which is not true. Although, given that its precisely 1 hour off, I think it is more likely due to daylight savings.
I also tried:
tz = pytz.timezone('US/Eastern')
dt = datetime.datetime.strptime('20151028:19:38:33:920', "%Y%m%d:%H:%M:%S:%f").replace(tzinfo=tz)
timestamp = dt.strftime("%s") # '1446079113'
This yields the same result as the earlier approach. I think this is because the python datetime documentation says that you can replace tzinfo but it will not recompute the time.
How can I get python to convert this local time to a timestamp while considering daylight savings?