0

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?

Guru Prasad
  • 4,053
  • 2
  • 25
  • 43
  • What's the right timestamp value? – MattH Dec 07 '15 at 15:27
  • `1446075513` would the be right timestamp value. Obtained by entering the datetime string into http://www.epochconverter.com – Guru Prasad Dec 07 '15 at 15:31
  • I can't find it now, but I thought there was a "DST" (Daylight Savings Time) aspect of a `datetime` object; if that's a real thing, hopefully that would know whether or not it was currently in DST and produce the correct UTC time, – dwanderson Dec 07 '15 at 15:31
  • local time may be ambigous. The soft that parses the logs may use a different version of timezone rules from the one that generates the logs and therefore the log may contain non-existent local times (and the resulting unix timestamps might differ from the intended). You need a way to disambiguate the input, e.g., [Parsing of Ordered Timestamps in Local Time (to UTC) While Observing Daylight Saving Time](http://stackoverflow.com/q/26217427/4279). See also [python converting string in localtime to UTC epoch timestamp](http://stackoverflow.com/q/32723834/4279) – jfs Dec 08 '15 at 20:36

0 Answers0