1

I tried to convert some time strings to timestamp using Python (pkg time & datetime). Unexpected things happened when I converted time strings during 2AM period. It will reconsider timezone in the timestamp value for no reason.

Here are four timestrings around 2 AM, I convert each of them to timestamp using datetime and time, respectively.

import time
from datetime import datetime
timestrings = ["2008-10-26 02:55:26", "2008-10-26 02:55:31", "2008-10-26 02:59:59", "2008-10-26 03:00:02"]
for i in range(1, len(timestrings)):
    ts00 = datetime.timestamp(datetime.strptime(timestrings[i-1], "%Y-%m-%d %H:%M:%S"))
    ts10 = datetime.timestamp(datetime.strptime(timestrings[i], "%Y-%m-%d %H:%M:%S"))
    print(ts00-ts10)
    ts01 = time.mktime(time.strptime(timestrings[i-1], "%Y-%m-%d %H:%M:%S"))
    ts11 = time.mktime(time.strptime(timestrings[i], "%Y-%m-%d %H:%M:%S"))
    print(ts01-ts11)

Considering the local timezone (mine is UTC+1), the values maybe different, but the differences between pairwise values should be consistent, which means the outputs should be

5.0
5.0
268.0
268.0
3.0
3.0

However, my outputs was

5.0
3605.0
268.0
268.0
3603.0
3.0

This inconsistence happened both in time and datetime, but in different time points when I ran it in IOS system. When I tested it in windows & linux system and everything is ok. I also tested a lot of records in string format, this inconsistence occured randomly and rarely. I can only find out a question mentioning "ambiguous 2AM cases"[https://stackoverflow.com/questions/13464009/calculate-tm-isdst-from-date-and-timezone]. Is it some weird curse for IOS system?

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
jlli
  • 11
  • 1

0 Answers0