Repository: da4089/simplefix

Issue:
Python 3.12: datetime changes
I *think* we use this -- need to check (from What's New in Python 3.12):

datetime’s utcnow() and utcfromtimestamp() are deprecated and will be removed in a future version. Instead, use timezone-aware objects to represent datetimes in UTC: respectively, call now() and fromtimestamp() with the tz parameter set to datetime.UTC. (Contributed by Paul Ganssle in gh-103857.

Hints from issue comments:
@da4089: Fixed in #62.

Solution patch:
diff --git a/simplefix/message.py b/simplefix/message.py
index 260ebd4..e9f3ab8 100644
--- a/simplefix/message.py
+++ b/simplefix/message.py
@@ -145,13 +145,13 @@ def append_time(self, tag, timestamp=None, precision=3, utc=True,
                       "Use append_utc_timestamp() or append_tz_timestamp() "
                       "instead.", DeprecationWarning)
         if not timestamp:
-            t = datetime.datetime.utcnow()
+            t = datetime.datetime.now(datetime.timezone.utc)
 
         elif type(timestamp) is float:
             if utc:
-                t = datetime.datetime.utcfromtimestamp(timestamp)
+                t = datetime.datetime.fromtimestamp(timestamp, tz=datetime.timezone.utc)
             else:
-                t = datetime.datetime.fromtimestamp(timestamp)
+                t = datetime.datetime.fromtimestamp(timestamp, tz=datetime.datetime.now().astimezone().tzinfo)
 
         else:
             t = timestamp
@@ -169,9 +169,9 @@ def append_time(self, tag, timestamp=None, precision=3, utc=True,
     def _append_utc_datetime(self, tag, fmt, ts, precision, header):
         """(Internal) Append formatted datetime."""
         if ts is None:
-            t = datetime.datetime.utcnow()
+            t = datetime.datetime.now(datetime.timezone.utc)
         elif type(ts) is float:
-            t = datetime.datetime.utcfromtimestamp(ts)
+            t = datetime.datetime.fromtimestamp(ts, tz=datetime.timezone.utc)
         else:
             t = ts
 
@@ -311,8 +311,8 @@ def append_tz_timestamp(self, tag, timestamp=None, precision=3,
                   (timestamp.microsecond * 1e-6)
 
         # Get offset of local timezone east of UTC.
-        utc = datetime.datetime.utcfromtimestamp(now)
-        local = datetime.datetime.fromtimestamp(now)
+        utc = datetime.datetime.fromtimestamp(now, tz=datetime.timezone.utc)
+        local = datetime.datetime.fromtimestamp(now, tz=datetime.datetime.now().astimezone().tzinfo)
         td = local - utc
         offset = int(((td.days * 86400) + td.seconds) / 60)
 
@@ -351,14 +351,14 @@ def append_tz_time_only(self, tag, timestamp=None, precision=3,
         standard.
         """
         if timestamp is None:
-            t = datetime.datetime.now()
+            t = datetime.datetime.now(tz=datetime.timezone.utc)
         elif type(timestamp) is float:
-            t = datetime.datetime.fromtimestamp(timestamp)
+            t = datetime.datetime.fromtimestamp(timestamp, tz=datetime.datetime.now().astimezone().tzinfo)
         else:
             t = timestamp
 
         now = time.mktime(t.timetuple()) + (t.microsecond * 1e-6)
-        utc = datetime.datetime.utcfromtimestamp(now)
+        utc = datetime.datetime.fromtimestamp(now, tz=datetime.timezone.utc)
         td = t - utc
         offset = int(((td.days * 86400) + td.seconds) / 60)
 


Test patch:
diff --git a/test/test_message.py b/test/test_message.py
index 367b5e7..7bb9de4 100644
--- a/test/test_message.py
+++ b/test/test_message.py
@@ -499,8 +499,8 @@ def test_append_tzts_none(self):
 
     @staticmethod
     def calculate_tz_offset(t):
-        local = datetime.datetime.fromtimestamp(t)
-        utc = datetime.datetime.utcfromtimestamp(t)
+        local = datetime.datetime.fromtimestamp(t, tz=datetime.datetime.now().astimezone().tzinfo)
+        utc = datetime.datetime.fromtimestamp(t, tz=datetime.timezone.utc)
         td = local - utc
         offset = int(((td.days * 86400) + td.seconds) / 60)
         return offset
@@ -531,7 +531,7 @@ def test_append_tzts_float(self):
     def test_append_tzts_datetime(self):
         msg = FixMessage()
         t = 1484581872.933458
-        local = datetime.datetime.fromtimestamp(t)
+        local = datetime.datetime.fromtimestamp(t, tz=datetime.datetime.now().astimezone().tzinfo)
         msg.append_tz_timestamp(1132, local)
 
         test = time.localtime(t)
@@ -609,7 +609,7 @@ def test_tzts_bad_precision(self):
     def test_tzto_datetime(self):
         msg = FixMessage()
         t = 1484581872.933458
-        local = datetime.datetime.fromtimestamp(t)
+        local = datetime.datetime.fromtimestamp(t, tz=datetime.datetime.now().astimezone().tzinfo)
         msg.append_tz_time_only(1079, local)
 
         test = time.localtime(t)