fix: add timezone to timestamps#64
Conversation
eppo_client/client.py
Outdated
| "variation": result.variation.key if result and result.variation else None, | ||
| "subject": subject_key, | ||
| "timestamp": datetime.datetime.utcnow().isoformat(), | ||
| "timestamp": datetime.datetime.now(datetime.timezone.utc).isoformat(), |
There was a problem hiding this comment.
A desirable test would be to refactor this into a function and the unit test would have consistent time:
import unittest
import datetime
from freezegun import freeze_time
def get_utc_timestamp():
return datetime.datetime.now(datetime.timezone.utc).isoformat()
class TestUTCTimestamp(unittest.TestCase):
@freeze_time("2023-08-20 14:30:45.123456+00:00")
def test_get_utc_timestamp(self):
expected = "2023-08-20T14:30:45.123456+00:00"
result = get_utc_timestamp()
self.assertEqual(result, expected)
def test_timezone_present(self):
result = get_utc_timestamp()
self.assertIn('+00:00', result)
def test_iso_format(self):
result = get_utc_timestamp()
try:
datetime.datetime.fromisoformat(result)
except ValueError:
self.fail("get_utc_timestamp() did not return a valid ISO format")
There was a problem hiding this comment.
I added two higher-level tests asserting that event timestamps carry the timezone information (ultimately, we don't care whether string uses +00:00 or Z suffix):
event = mock_logger.log_assignment.call_args.args[0]
timestamp = datetime.datetime.fromisoformat(event["timestamp"])
assert timestamp.tzinfo == datetime.timezone.utc
schmit
left a comment
There was a problem hiding this comment.
Great catch -- perhaps we should refactor into a get_time() function (probably with a better name) and call that so we don't risk calling the wrong function in the future (although of course we could forget that the get_time function exists...
Also don't forget a version bump!
|
@schmit we actually have a good I'm signing off for today so didn't have much time for a nice fix with tests. I can improve it tomorrow or feel free to pick it up |
bc036c9 to
a021dc2
Compare
|
@schmit @leoromanovsky refactored this into a function + added tests |
labels: mergeable
Fixes: #issue
Motivation and Context
This fixes timestamps, so they get properly serialized with timezone information.
Description
How has this been tested?
Not tested.