Skip to content

Commit

Permalink
Merge pull request DIRACGrid#7956 from fstagni/90_time_utc
Browse files Browse the repository at this point in the history
[9.0] fix: ensure using UTC
  • Loading branch information
fstagni authored Dec 19, 2024
2 parents e75f7dd + 76c0040 commit b7ffc47
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/DIRAC/Core/Utilities/Graphs/GraphUtilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,18 @@ def convert_to_datetime(dstring):
else:
results = eval(str(dstring), {"__builtins__": None, "time": time, "math": math}, {})
if isinstance(results, (int, float)):
# Use utcfromtimestamp for UTC time
results = datetime.datetime.utcfromtimestamp(int(results))
elif isinstance(results, datetime.datetime):
pass
results = results.astimezone(datetime.timezone.utc) # Ensure in UTC
else:
raise ValueError("Unknown datetime type!")
except Exception:
t = None
for dateformat in datestrings:
try:
t = time.strptime(dstring, dateformat)
timestamp = calendar.timegm(t) # -time.timezone
timestamp = calendar.timegm(t) # Convert to UTC timestamp
results = datetime.datetime.utcfromtimestamp(timestamp)
break
except Exception:
Expand All @@ -89,12 +90,12 @@ def convert_to_datetime(dstring):
try:
dstring = dstring.split(".", 1)[0]
t = time.strptime(dstring, dateformat)
timestamp = time.mktime(t) # -time.timezone
timestamp = calendar.timegm(t) # Convert to UTC timestamp
results = datetime.datetime.utcfromtimestamp(timestamp)
except Exception:
raise ValueError(
"Unable to create time from string!\nExpecting "
"format of: '12/06/06 12:54:67'\nRecieved:%s" % orig_string
"format of: '12/06/06 12:54:67'\nReceived:%s" % orig_string
)
return results

Expand Down

0 comments on commit b7ffc47

Please sign in to comment.