@@ -862,13 +862,10 @@ Other constructors, all class methods:
862
862
(for example, this may be possible on platforms supplying the C
863
863
:c:func: `gettimeofday ` function).
864
864
865
- If *tz * is not ``None ``, it must be an instance of a :class: `tzinfo ` subclass, and the
866
- current date and time are converted to *tz *’s time zone. In this case the
867
- result is equivalent to::
868
-
869
- tz.fromutc(datetime.utcnow().replace(tzinfo=tz))
865
+ If *tz * is not ``None ``, it must be an instance of a :class: `tzinfo ` subclass,
866
+ and the current date and time are converted to *tz *’s time zone.
870
867
871
- See also :meth: `today `, :meth: `utcnow `.
868
+ This function is preferred over :meth: `today ` and :meth: `utcnow `.
872
869
873
870
874
871
.. classmethod :: datetime.utcnow()
@@ -879,6 +876,14 @@ Other constructors, all class methods:
879
876
:class: `.datetime ` object. An aware current UTC datetime can be obtained by
880
877
calling ``datetime.now(timezone.utc) ``. See also :meth: `now `.
881
878
879
+ .. warning ::
880
+
881
+ Because naive ``datetime `` objects are treated by many ``datetime `` methods
882
+ as local times, it is preferred to use aware datetimes to represent times
883
+ in UTC. As such, the recommended way to create an object representing the
884
+ current time in UTC by calling ``datetime.now(timezone.utc) ``.
885
+
886
+
882
887
.. classmethod :: datetime.fromtimestamp(timestamp, tz=None)
883
888
884
889
Return the local date and time corresponding to the POSIX timestamp, such as is
@@ -889,10 +894,6 @@ Other constructors, all class methods:
889
894
If *tz * is not ``None ``, it must be an instance of a :class: `tzinfo ` subclass, and the
890
895
timestamp is converted to *tz *’s time zone.
891
896
892
- In this case the result is equivalent to::
893
-
894
- tz.fromutc(datetime.utcfromtimestamp(timestamp).replace(tzinfo=tz))
895
-
896
897
:meth: `fromtimestamp ` may raise :exc: `OverflowError `, if the timestamp is out of
897
898
the range of values supported by the platform C :c:func: `localtime ` or
898
899
:c:func: `gmtime ` functions, and :exc: `OSError ` on :c:func: `localtime ` or
@@ -901,7 +902,8 @@ Other constructors, all class methods:
901
902
1970 through 2038. Note that on non-POSIX systems that include leap seconds in
902
903
their notion of a timestamp, leap seconds are ignored by :meth: `fromtimestamp `,
903
904
and then it's possible to have two timestamps differing by a second that yield
904
- identical :class: `.datetime ` objects. See also :meth: `utcfromtimestamp `.
905
+ identical :class: `.datetime ` objects. This method is preferred over
906
+ :meth: `utcfromtimestamp `.
905
907
906
908
.. versionchanged :: 3.3
907
909
Raise :exc: `OverflowError ` instead of :exc: `ValueError ` if the timestamp
@@ -935,6 +937,14 @@ Other constructors, all class methods:
935
937
except the latter formula always supports the full years range: between
936
938
:const: `MINYEAR ` and :const: `MAXYEAR ` inclusive.
937
939
940
+ .. warning ::
941
+
942
+ Because naive ``datetime `` objects are treated by many ``datetime `` methods
943
+ as local times, it is preferred to use aware datetimes to represent times
944
+ in UTC. As such, the recommended way to create an object representing a
945
+ specific timestamp in UTC by calling
946
+ ``datetime.fromtimestamp(timestamp, tz=timezone.utc) ``.
947
+
938
948
.. versionchanged :: 3.3
939
949
Raise :exc: `OverflowError ` instead of :exc: `ValueError ` if the timestamp
940
950
is out of the range of values supported by the platform C
@@ -1322,6 +1332,14 @@ Instance methods:
1322
1332
``MINYEAR `` or ``MAXYEAR `` and UTC adjustment spills over a year
1323
1333
boundary.
1324
1334
1335
+ .. warning ::
1336
+
1337
+ Because naive ``datetime `` objects are treated by many ``datetime `` methods
1338
+ as local times, it is preferred to use aware datetimes to represent times
1339
+ in UTC; as a result, using ``utcfromtimetuple `` may give misleading
1340
+ results. If you have a naive ``datetime `` representing UTC, use
1341
+ ``datetime.replace(tzinfo=timezone.utc) `` to make it aware, at which point
1342
+ you can use :meth: `.datetime.timetuple `.
1325
1343
1326
1344
.. method :: datetime.toordinal()
1327
1345
@@ -1500,19 +1518,19 @@ Examples of working with :class:`~datetime.datetime` objects:
1500
1518
1501
1519
.. doctest ::
1502
1520
1503
- >>> from datetime import datetime, date, time
1521
+ >>> from datetime import datetime, date, time, timezone
1504
1522
1505
1523
>>> # Using datetime.combine()
1506
1524
>>> d = date(2005 , 7 , 14 )
1507
1525
>>> t = time(12 , 30 )
1508
1526
>>> datetime.combine(d, t)
1509
1527
datetime.datetime(2005, 7, 14, 12, 30)
1510
1528
1511
- >>> # Using datetime.now() or datetime.utcnow()
1529
+ >>> # Using datetime.now()
1512
1530
>>> datetime.now() # doctest: +SKIP
1513
1531
datetime.datetime(2007, 12, 6, 16, 29, 43, 79043) # GMT +1
1514
- >>> datetime.utcnow( ) # doctest: +SKIP
1515
- datetime.datetime(2007, 12, 6, 15, 29, 43, 79060)
1532
+ >>> datetime.now(timezone.utc ) # doctest: +SKIP
1533
+ datetime.datetime(2007, 12, 6, 15, 29, 43, 79060, tzinfo=datetime.timezone.utc )
1516
1534
1517
1535
>>> # Using datetime.strptime()
1518
1536
>>> dt = datetime.strptime(" 21/11/06 16:30" , " %d /%m/%y %H:%M" )
@@ -1616,7 +1634,7 @@ Usage of ``KabulTz`` from above::
1616
1634
datetime.datetime(2006, 6, 14, 8, 30, tzinfo=datetime.timezone.utc)
1617
1635
>>> dt2
1618
1636
datetime.datetime(2006, 6, 14, 13, 0, tzinfo=KabulTz())
1619
- >>> dt2.utctimetuple() == dt3.utctimetuple()
1637
+ >>> dt2 == dt3
1620
1638
True
1621
1639
1622
1640
.. _datetime-time :
0 commit comments