@@ -862,13 +862,10 @@ Other constructors, all class methods:
862862 (for example, this may be possible on platforms supplying the C
863863 :c:func: `gettimeofday ` function).
864864
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.
870867
871- See also :meth: `today `, :meth: `utcnow `.
868+ This function is preferred over :meth: `today ` and :meth: `utcnow `.
872869
873870
874871.. classmethod :: datetime.utcnow()
@@ -879,6 +876,14 @@ Other constructors, all class methods:
879876 :class: `.datetime ` object. An aware current UTC datetime can be obtained by
880877 calling ``datetime.now(timezone.utc) ``. See also :meth: `now `.
881878
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+
882887.. classmethod :: datetime.fromtimestamp(timestamp, tz=None)
883888
884889 Return the local date and time corresponding to the POSIX timestamp, such as is
@@ -889,10 +894,6 @@ Other constructors, all class methods:
889894 If *tz * is not ``None ``, it must be an instance of a :class: `tzinfo ` subclass, and the
890895 timestamp is converted to *tz *’s time zone.
891896
892- In this case the result is equivalent to::
893-
894- tz.fromutc(datetime.utcfromtimestamp(timestamp).replace(tzinfo=tz))
895-
896897 :meth: `fromtimestamp ` may raise :exc: `OverflowError `, if the timestamp is out of
897898 the range of values supported by the platform C :c:func: `localtime ` or
898899 :c:func: `gmtime ` functions, and :exc: `OSError ` on :c:func: `localtime ` or
@@ -901,7 +902,8 @@ Other constructors, all class methods:
901902 1970 through 2038. Note that on non-POSIX systems that include leap seconds in
902903 their notion of a timestamp, leap seconds are ignored by :meth: `fromtimestamp `,
903904 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 `.
905907
906908 .. versionchanged :: 3.3
907909 Raise :exc: `OverflowError ` instead of :exc: `ValueError ` if the timestamp
@@ -935,6 +937,14 @@ Other constructors, all class methods:
935937 except the latter formula always supports the full years range: between
936938 :const: `MINYEAR ` and :const: `MAXYEAR ` inclusive.
937939
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+
938948 .. versionchanged :: 3.3
939949 Raise :exc: `OverflowError ` instead of :exc: `ValueError ` if the timestamp
940950 is out of the range of values supported by the platform C
@@ -1322,6 +1332,14 @@ Instance methods:
13221332 ``MINYEAR `` or ``MAXYEAR `` and UTC adjustment spills over a year
13231333 boundary.
13241334
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 `.
13251343
13261344.. method :: datetime.toordinal()
13271345
@@ -1500,19 +1518,19 @@ Examples of working with :class:`~datetime.datetime` objects:
15001518
15011519.. doctest ::
15021520
1503- >>> from datetime import datetime, date, time
1521+ >>> from datetime import datetime, date, time, timezone
15041522
15051523 >>> # Using datetime.combine()
15061524 >>> d = date(2005 , 7 , 14 )
15071525 >>> t = time(12 , 30 )
15081526 >>> datetime.combine(d, t)
15091527 datetime.datetime(2005, 7, 14, 12, 30)
15101528
1511- >>> # Using datetime.now() or datetime.utcnow()
1529+ >>> # Using datetime.now()
15121530 >>> datetime.now() # doctest: +SKIP
15131531 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 )
15161534
15171535 >>> # Using datetime.strptime()
15181536 >>> dt = datetime.strptime(" 21/11/06 16:30" , " %d /%m/%y %H:%M" )
@@ -1616,7 +1634,7 @@ Usage of ``KabulTz`` from above::
16161634 datetime.datetime(2006, 6, 14, 8, 30, tzinfo=datetime.timezone.utc)
16171635 >>> dt2
16181636 datetime.datetime(2006, 6, 14, 13, 0, tzinfo=KabulTz())
1619- >>> dt2.utctimetuple() == dt3.utctimetuple()
1637+ >>> dt2 == dt3
16201638 True
16211639
16221640.. _datetime-time :
0 commit comments