88from mongoengine import *
99from tests .asynchronous .utils import MongoDBAsyncTestCase
1010
11+ try :
12+ # Python 3.11+
13+ from datetime import UTC
14+ except ImportError :
15+ # Python ≤ 3.10
16+ from datetime import timezone
17+ UTC = timezone .utc
18+
1119
1220class ComplexDateTimeFieldTest (MongoDBAsyncTestCase ):
1321 async def test_complexdatetime_storage (self ):
@@ -23,7 +31,7 @@ class LogEntry(Document):
2331
2432 # Post UTC - microseconds are rounded (down) nearest millisecond and
2533 # dropped - with default datetime fields
26- d1 = datetime .datetime (1970 , 1 , 1 , 0 , 0 , 1 , 999 ,tzinfo = datetime . UTC )
34+ d1 = datetime .datetime (1970 , 1 , 1 , 0 , 0 , 1 , 999 ,tzinfo = UTC )
2735 log = LogEntry ()
2836 log .date = d1
2937 await log .asave ()
@@ -32,15 +40,15 @@ class LogEntry(Document):
3240
3341 # Post UTC - microseconds are rounded (down) nearest millisecond - with
3442 # default datetime fields
35- d1 = datetime .datetime (1970 , 1 , 1 , 0 , 0 , 1 , 9999 ,tzinfo = datetime . UTC )
43+ d1 = datetime .datetime (1970 , 1 , 1 , 0 , 0 , 1 , 9999 ,tzinfo = UTC )
3644 log .date = d1
3745 await log .asave ()
3846 await log .areload ()
3947 assert log .date == d1
4048
4149 # Pre UTC dates microseconds below 1000 are dropped - with default
4250 # datetime fields
43- d1 = datetime .datetime (1969 , 12 , 31 , 23 , 59 , 59 , 999 ,tzinfo = datetime . UTC )
51+ d1 = datetime .datetime (1969 , 12 , 31 , 23 , 59 , 59 , 999 ,tzinfo = UTC )
4452 log .date = d1
4553 await log .asave ()
4654 await log .areload ()
@@ -50,7 +58,7 @@ class LogEntry(Document):
5058 # log.date has an invalid microsecond value, so I can't construct
5159 # a date to compare.
5260 for i in range (1001 , 3113 , 33 ):
53- d1 = datetime .datetime (1969 , 12 , 31 , 23 , 59 , 59 , i ,tzinfo = datetime . UTC )
61+ d1 = datetime .datetime (1969 , 12 , 31 , 23 , 59 , 59 , i ,tzinfo = UTC )
5462 log = LogEntry (
5563 date = d1
5664 )
@@ -164,7 +172,7 @@ class Log(Document):
164172 assert fetched_log .timestamp is None
165173
166174 async def test_default_static_value (self ):
167- NOW = datetime .datetime .now (datetime . UTC )
175+ NOW = datetime .datetime .now (UTC )
168176
169177 class Log (Document ):
170178 timestamp = ComplexDateTimeField (default = NOW )
@@ -179,7 +187,7 @@ class Log(Document):
179187 assert fetched_log .timestamp == NOW
180188
181189 async def test_default_callable (self ):
182- NOW = datetime .datetime .now (datetime . UTC )
190+ NOW = datetime .datetime .now (UTC )
183191
184192 class Log (Document ):
185193 timestamp = ComplexDateTimeField (default = NOW )
0 commit comments