@@ -12,16 +12,42 @@ def test_date_value_to_int__basics():
1212 assert _Entity .date_value_to_int (1234 , 1000000000 ) == 1234
1313 assert _Entity .date_value_to_int (1234.0 , 1000 ) == 1234000 # milliseconds
1414 assert _Entity .date_value_to_int (1234.0 , 1000000000 ) == 1234000000000 # nanoseconds
15+ dt = datetime .fromtimestamp (12345678 ) # May 1970; 1234 is too close to the epoch (special case for that below)
16+ assert _Entity .date_value_to_int (dt , 1000 ) == 12345678000 # milliseconds
17+
18+
19+ def test_date_value_to_int__close_to_epoch ():
20+ assert _Entity .date_value_to_int (datetime .fromtimestamp (0 , timezone .utc ), 1000 ) == 0
21+ assert _Entity .date_value_to_int (datetime .fromtimestamp (1234 , timezone .utc ), 1000 ) == 1234000
22+ assert _Entity .date_value_to_int (datetime .fromtimestamp (0 ), 1000 ) == 0
23+ assert _Entity .date_value_to_int (datetime .fromtimestamp (1234 ), 1000 ) == 1234000
24+
25+ # "Return the local date corresponding to the POSIX timestamp"; but not always!? Was -1 hour off with CEST:
26+ dt0naive = datetime .fromtimestamp (0 )
27+ local_tz = datetime .now ().astimezone ().tzinfo
28+ dt0local = dt0naive .replace (tzinfo = local_tz )
29+ dt0utc = dt0local .astimezone (timezone .utc )
30+
31+ # Print, don't assert... the result Seems to depend on the local timezone configuration!?
32+ print ("\n Naive:" , dt0naive ) # Seen: 1970-01-01 01:00:00
33+ print ("Local:" , dt0local ) # Seen: 1970-01-01 01:00:00+02:00
34+ print ("UTC:" , dt0utc ) # Seen: 1969-12-31 23:00:00+00:00
35+ print ("Timestamp:" , dt0utc .timestamp ()) # Seen: -3600.0
36+
1537 dt = datetime .fromtimestamp (1234 )
1638 if sys .platform == "win32" :
39+ # On Windows, timestamp() seems to raise an OSError if the date is close to the epoch; see bug reports:
40+ # https://github.com/python/cpython/issues/81708 and https://github.com/python/cpython/issues/94414
1741 try :
1842 dt .timestamp ()
19- assert False , "Expected OSError"
43+ assert False , "Expected OSError - Did Python on Windows get fixed? "
2044 except OSError as e :
2145 assert e .errno == 22
2246 else :
47+ # Non-Windows platforms should work fine
2348 assert dt .timestamp () == 1234
24- assert _Entity .date_value_to_int (dt , 1000 ) == 1234000 # milliseconds
49+
50+ assert _Entity .date_value_to_int (dt , 1000 ) == 1234000 # milliseconds
2551
2652
2753def test_date_value_to_int__timezone ():
0 commit comments