@@ -70,17 +70,18 @@ def convert_to_datetime(dstring):
70
70
else :
71
71
results = eval (str (dstring ), {"__builtins__" : None , "time" : time , "math" : math }, {})
72
72
if isinstance (results , (int , float )):
73
+ # Use utcfromtimestamp for UTC time
73
74
results = datetime .datetime .utcfromtimestamp (int (results ))
74
75
elif isinstance (results , datetime .datetime ):
75
- pass
76
+ results = results . astimezone ( datetime . timezone . utc ) # Ensure in UTC
76
77
else :
77
78
raise ValueError ("Unknown datetime type!" )
78
79
except Exception :
79
80
t = None
80
81
for dateformat in datestrings :
81
82
try :
82
83
t = time .strptime (dstring , dateformat )
83
- timestamp = calendar .timegm (t ) # -time.timezone
84
+ timestamp = calendar .timegm (t ) # Convert to UTC timestamp
84
85
results = datetime .datetime .utcfromtimestamp (timestamp )
85
86
break
86
87
except Exception :
@@ -89,12 +90,12 @@ def convert_to_datetime(dstring):
89
90
try :
90
91
dstring = dstring .split ("." , 1 )[0 ]
91
92
t = time .strptime (dstring , dateformat )
92
- timestamp = time . mktime (t ) # -time.timezone
93
+ timestamp = calendar . timegm (t ) # Convert to UTC timestamp
93
94
results = datetime .datetime .utcfromtimestamp (timestamp )
94
95
except Exception :
95
96
raise ValueError (
96
97
"Unable to create time from string!\n Expecting "
97
- "format of: '12/06/06 12:54:67'\n Recieved :%s" % orig_string
98
+ "format of: '12/06/06 12:54:67'\n Received :%s" % orig_string
98
99
)
99
100
return results
100
101
0 commit comments