~ubuntu-branches/ubuntu/quantal/python2.7/quantal

« back to all changes in this revision

Viewing changes to Doc/library/datetime.rst

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2012-03-09 19:28:43 UTC
  • mto: (36.1.11 sid)
  • mto: This revision was merged to the branch mainline in revision 51.
  • Revision ID: package-import@ubuntu.com-20120309192843-n84bbtrkfxw34p6n
Tags: upstream-2.7.3~rc1
ImportĀ upstreamĀ versionĀ 2.7.3~rc1

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
 
14
14
The :mod:`datetime` module supplies classes for manipulating dates and times in
15
15
both simple and complex ways.  While date and time arithmetic is supported, the
16
 
focus of the implementation is on efficient member extraction for output
 
16
focus of the implementation is on efficient attribute extraction for output
17
17
formatting and manipulation. For related
18
18
functionality, see also the :mod:`time` and :mod:`calendar` modules.
19
19
 
20
20
There are two kinds of date and time objects: "naive" and "aware". This
21
21
distinction refers to whether the object has any notion of time zone, daylight
22
22
saving time, or other kind of algorithmic or political time adjustment.  Whether
23
 
a naive :class:`datetime` object represents Coordinated Universal Time (UTC),
 
23
a naive :class:`.datetime` object represents Coordinated Universal Time (UTC),
24
24
local time, or time in some other timezone is purely up to the program, just
25
25
like it's up to the program whether a particular number represents metres,
26
 
miles, or mass.  Naive :class:`datetime` objects are easy to understand and to
 
26
miles, or mass.  Naive :class:`.datetime` objects are easy to understand and to
27
27
work with, at the cost of ignoring some aspects of reality.
28
28
 
29
 
For applications requiring more, :class:`datetime` and :class:`time` objects
30
 
have an optional time zone information member, :attr:`tzinfo`, that can contain
31
 
an instance of a subclass of the abstract :class:`tzinfo` class.  These
 
29
For applications requiring more, :class:`.datetime` and :class:`.time` objects
 
30
have an optional time zone information attribute, :attr:`tzinfo`, that can be
 
31
set to an instance of a subclass of the abstract :class:`tzinfo` class.  These
32
32
:class:`tzinfo` objects capture information about the offset from UTC time, the
33
33
time zone name, and whether Daylight Saving Time is in effect.  Note that no
34
34
concrete :class:`tzinfo` classes are supplied by the :mod:`datetime` module.
40
40
 
41
41
.. data:: MINYEAR
42
42
 
43
 
   The smallest year number allowed in a :class:`date` or :class:`datetime` object.
 
43
   The smallest year number allowed in a :class:`date` or :class:`.datetime` object.
44
44
   :const:`MINYEAR` is ``1``.
45
45
 
46
46
 
47
47
.. data:: MAXYEAR
48
48
 
49
 
   The largest year number allowed in a :class:`date` or :class:`datetime` object.
 
49
   The largest year number allowed in a :class:`date` or :class:`.datetime` object.
50
50
   :const:`MAXYEAR` is ``9999``.
51
51
 
52
52
 
90
90
.. class:: timedelta
91
91
   :noindex:
92
92
 
93
 
   A duration expressing the difference between two :class:`date`, :class:`time`,
94
 
   or :class:`datetime` instances to microsecond resolution.
 
93
   A duration expressing the difference between two :class:`date`, :class:`.time`,
 
94
   or :class:`.datetime` instances to microsecond resolution.
95
95
 
96
96
 
97
97
.. class:: tzinfo
98
98
 
99
99
   An abstract base class for time zone information objects.  These are used by the
100
 
   :class:`datetime` and :class:`time` classes to provide a customizable notion of
 
100
   :class:`.datetime` and :class:`.time` classes to provide a customizable notion of
101
101
   time adjustment (for example, to account for time zone and/or daylight saving
102
102
   time).
103
103
 
105
105
 
106
106
Objects of the :class:`date` type are always naive.
107
107
 
108
 
An object *d* of type :class:`time` or :class:`datetime` may be naive or aware.
 
108
An object *d* of type :class:`.time` or :class:`.datetime` may be naive or aware.
109
109
*d* is aware if ``d.tzinfo`` is not ``None`` and ``d.tzinfo.utcoffset(d)`` does
110
110
not return ``None``.  If ``d.tzinfo`` is ``None``, or if ``d.tzinfo`` is not
111
111
``None`` but ``d.tzinfo.utcoffset(d)`` returns ``None``, *d* is naive.
269
269
  -1 day, 19:00:00
270
270
 
271
271
In addition to the operations listed above :class:`timedelta` objects support
272
 
certain additions and subtractions with :class:`date` and :class:`datetime`
 
272
certain additions and subtractions with :class:`date` and :class:`.datetime`
273
273
objects (see below).
274
274
 
275
275
Comparisons of :class:`timedelta` objects are supported with the
360
360
 
361
361
   Return the local date corresponding to the POSIX timestamp, such as is returned
362
362
   by :func:`time.time`.  This may raise :exc:`ValueError`, if the timestamp is out
363
 
   of the range of values supported by the platform C :cfunc:`localtime` function.
 
363
   of the range of values supported by the platform C :c:func:`localtime` function.
364
364
   It's common for this to be restricted to years from 1970 through 2038.  Note
365
365
   that on non-POSIX systems that include leap seconds in their notion of a
366
366
   timestamp, leap seconds are ignored by :meth:`fromtimestamp`.
463
463
 
464
464
.. method:: date.replace(year, month, day)
465
465
 
466
 
   Return a date with the same value, except for those members given new values by
467
 
   whichever keyword arguments are specified.  For example, if ``d == date(2002,
468
 
   12, 31)``, then ``d.replace(day=26) == date(2002, 12, 26)``.
 
466
   Return a date with the same value, except for those parameters given new
 
467
   values by whichever keyword arguments are specified.  For example, if ``d ==
 
468
   date(2002, 12, 31)``, then ``d.replace(day=26) == date(2002, 12, 26)``.
469
469
 
470
470
 
471
471
.. method:: date.timetuple()
534
534
   Return a string representing the date, for example ``date(2002, 12,
535
535
   4).ctime() == 'Wed Dec 4 00:00:00 2002'``. ``d.ctime()`` is equivalent to
536
536
   ``time.ctime(time.mktime(d.timetuple()))`` on platforms where the native C
537
 
   :cfunc:`ctime` function (which :func:`time.ctime` invokes, but which
 
537
   :c:func:`ctime` function (which :func:`time.ctime` invokes, but which
538
538
   :meth:`date.ctime` does not invoke) conforms to the C standard.
539
539
 
540
540
 
602
602
:class:`datetime` Objects
603
603
-------------------------
604
604
 
605
 
A :class:`datetime` object is a single object containing all the information
606
 
from a :class:`date` object and a :class:`time` object.  Like a :class:`date`
607
 
object, :class:`datetime` assumes the current Gregorian calendar extended in
608
 
both directions; like a time object, :class:`datetime` assumes there are exactly
 
605
A :class:`.datetime` object is a single object containing all the information
 
606
from a :class:`date` object and a :class:`.time` object.  Like a :class:`date`
 
607
object, :class:`.datetime` assumes the current Gregorian calendar extended in
 
608
both directions; like a time object, :class:`.datetime` assumes there are exactly
609
609
3600\*24 seconds in every day.
610
610
 
611
611
Constructor:
641
641
   or not specified, this is like :meth:`today`, but, if possible, supplies more
642
642
   precision than can be gotten from going through a :func:`time.time` timestamp
643
643
   (for example, this may be possible on platforms supplying the C
644
 
   :cfunc:`gettimeofday` function).
 
644
   :c:func:`gettimeofday` function).
645
645
 
646
646
   Else *tz* must be an instance of a class :class:`tzinfo` subclass, and the
647
647
   current date and time are converted to *tz*'s time zone.  In this case the
653
653
 
654
654
   Return the current UTC date and time, with :attr:`tzinfo` ``None``. This is like
655
655
   :meth:`now`, but returns the current UTC date and time, as a naive
656
 
   :class:`datetime` object. See also :meth:`now`.
 
656
   :class:`.datetime` object. See also :meth:`now`.
657
657
 
658
658
 
659
659
.. classmethod:: datetime.fromtimestamp(timestamp[, tz])
661
661
   Return the local date and time corresponding to the POSIX timestamp, such as is
662
662
   returned by :func:`time.time`. If optional argument *tz* is ``None`` or not
663
663
   specified, the timestamp is converted to the platform's local date and time, and
664
 
   the returned :class:`datetime` object is naive.
 
664
   the returned :class:`.datetime` object is naive.
665
665
 
666
666
   Else *tz* must be an instance of a class :class:`tzinfo` subclass, and the
667
667
   timestamp is converted to *tz*'s time zone.  In this case the result is
669
669
   ``tz.fromutc(datetime.utcfromtimestamp(timestamp).replace(tzinfo=tz))``.
670
670
 
671
671
   :meth:`fromtimestamp` may raise :exc:`ValueError`, if the timestamp is out of
672
 
   the range of values supported by the platform C :cfunc:`localtime` or
673
 
   :cfunc:`gmtime` functions.  It's common for this to be restricted to years in
 
672
   the range of values supported by the platform C :c:func:`localtime` or
 
673
   :c:func:`gmtime` functions.  It's common for this to be restricted to years in
674
674
   1970 through 2038. Note that on non-POSIX systems that include leap seconds in
675
675
   their notion of a timestamp, leap seconds are ignored by :meth:`fromtimestamp`,
676
676
   and then it's possible to have two timestamps differing by a second that yield
677
 
   identical :class:`datetime` objects. See also :meth:`utcfromtimestamp`.
 
677
   identical :class:`.datetime` objects. See also :meth:`utcfromtimestamp`.
678
678
 
679
679
 
680
680
.. classmethod:: datetime.utcfromtimestamp(timestamp)
681
681
 
682
 
   Return the UTC :class:`datetime` corresponding to the POSIX timestamp, with
 
682
   Return the UTC :class:`.datetime` corresponding to the POSIX timestamp, with
683
683
   :attr:`tzinfo` ``None``. This may raise :exc:`ValueError`, if the timestamp is
684
 
   out of the range of values supported by the platform C :cfunc:`gmtime` function.
 
684
   out of the range of values supported by the platform C :c:func:`gmtime` function.
685
685
   It's common for this to be restricted to years in 1970 through 2038. See also
686
686
   :meth:`fromtimestamp`.
687
687
 
688
688
 
689
689
.. classmethod:: datetime.fromordinal(ordinal)
690
690
 
691
 
   Return the :class:`datetime` corresponding to the proleptic Gregorian ordinal,
 
691
   Return the :class:`.datetime` corresponding to the proleptic Gregorian ordinal,
692
692
   where January 1 of year 1 has ordinal 1. :exc:`ValueError` is raised unless ``1
693
693
   <= ordinal <= datetime.max.toordinal()``.  The hour, minute, second and
694
694
   microsecond of the result are all 0, and :attr:`tzinfo` is ``None``.
696
696
 
697
697
.. classmethod:: datetime.combine(date, time)
698
698
 
699
 
   Return a new :class:`datetime` object whose date members are equal to the given
700
 
   :class:`date` object's, and whose time and :attr:`tzinfo` members are equal to
701
 
   the given :class:`time` object's. For any :class:`datetime` object *d*, ``d ==
702
 
   datetime.combine(d.date(), d.timetz())``.  If date is a :class:`datetime`
703
 
   object, its time and :attr:`tzinfo` members are ignored.
 
699
   Return a new :class:`.datetime` object whose date components are equal to the
 
700
   given :class:`date` object's, and whose time components and :attr:`tzinfo`
 
701
   attributes are equal to the given :class:`.time` object's. For any
 
702
   :class:`.datetime` object *d*,
 
703
   ``d == datetime.combine(d.date(), d.timetz())``.  If date is a
 
704
   :class:`.datetime` object, its time components and :attr:`tzinfo` attributes
 
705
   are ignored.
704
706
 
705
707
 
706
708
.. classmethod:: datetime.strptime(date_string, format)
707
709
 
708
 
   Return a :class:`datetime` corresponding to *date_string*, parsed according to
 
710
   Return a :class:`.datetime` corresponding to *date_string*, parsed according to
709
711
   *format*.  This is equivalent to ``datetime(*(time.strptime(date_string,
710
712
   format)[0:6]))``. :exc:`ValueError` is raised if the date_string and format
711
713
   can't be parsed by :func:`time.strptime` or if it returns a value which isn't a
718
720
 
719
721
.. attribute:: datetime.min
720
722
 
721
 
   The earliest representable :class:`datetime`, ``datetime(MINYEAR, 1, 1,
 
723
   The earliest representable :class:`.datetime`, ``datetime(MINYEAR, 1, 1,
722
724
   tzinfo=None)``.
723
725
 
724
726
 
725
727
.. attribute:: datetime.max
726
728
 
727
 
   The latest representable :class:`datetime`, ``datetime(MAXYEAR, 12, 31, 23, 59,
 
729
   The latest representable :class:`.datetime`, ``datetime(MAXYEAR, 12, 31, 23, 59,
728
730
   59, 999999, tzinfo=None)``.
729
731
 
730
732
 
731
733
.. attribute:: datetime.resolution
732
734
 
733
 
   The smallest possible difference between non-equal :class:`datetime` objects,
 
735
   The smallest possible difference between non-equal :class:`.datetime` objects,
734
736
   ``timedelta(microseconds=1)``.
735
737
 
736
738
 
773
775
 
774
776
.. attribute:: datetime.tzinfo
775
777
 
776
 
   The object passed as the *tzinfo* argument to the :class:`datetime` constructor,
 
778
   The object passed as the *tzinfo* argument to the :class:`.datetime` constructor,
777
779
   or ``None`` if none was passed.
778
780
 
779
781
 
780
782
Supported operations:
781
783
 
782
 
+---------------------------------------+-------------------------------+
783
 
| Operation                             | Result                        |
784
 
+=======================================+===============================+
785
 
| ``datetime2 = datetime1 + timedelta`` | \(1)                          |
786
 
+---------------------------------------+-------------------------------+
787
 
| ``datetime2 = datetime1 - timedelta`` | \(2)                          |
788
 
+---------------------------------------+-------------------------------+
789
 
| ``timedelta = datetime1 - datetime2`` | \(3)                          |
790
 
+---------------------------------------+-------------------------------+
791
 
| ``datetime1 < datetime2``             | Compares :class:`datetime` to |
792
 
|                                       | :class:`datetime`. (4)        |
793
 
+---------------------------------------+-------------------------------+
 
784
+---------------------------------------+--------------------------------+
 
785
| Operation                             | Result                         |
 
786
+=======================================+================================+
 
787
| ``datetime2 = datetime1 + timedelta`` | \(1)                           |
 
788
+---------------------------------------+--------------------------------+
 
789
| ``datetime2 = datetime1 - timedelta`` | \(2)                           |
 
790
+---------------------------------------+--------------------------------+
 
791
| ``timedelta = datetime1 - datetime2`` | \(3)                           |
 
792
+---------------------------------------+--------------------------------+
 
793
| ``datetime1 < datetime2``             | Compares :class:`.datetime` to |
 
794
|                                       | :class:`.datetime`. (4)        |
 
795
+---------------------------------------+--------------------------------+
794
796
 
795
797
(1)
796
798
   datetime2 is a duration of timedelta removed from datetime1, moving forward in
797
799
   time if ``timedelta.days`` > 0, or backward if ``timedelta.days`` < 0.  The
798
 
   result has the same :attr:`tzinfo` member as the input datetime, and datetime2 -
799
 
   datetime1 == timedelta after. :exc:`OverflowError` is raised if datetime2.year
800
 
   would be smaller than :const:`MINYEAR` or larger than :const:`MAXYEAR`. Note
801
 
   that no time zone adjustments are done even if the input is an aware object.
 
800
   result has the same :attr:`tzinfo` attribute as the input datetime, and
 
801
   datetime2 - datetime1 == timedelta after. :exc:`OverflowError` is raised if
 
802
   datetime2.year would be smaller than :const:`MINYEAR` or larger than
 
803
   :const:`MAXYEAR`. Note that no time zone adjustments are done even if the
 
804
   input is an aware object.
802
805
 
803
806
(2)
804
807
   Computes the datetime2 such that datetime2 + timedelta == datetime1. As for
805
 
   addition, the result has the same :attr:`tzinfo` member as the input datetime,
806
 
   and no time zone adjustments are done even if the input is aware. This isn't
807
 
   quite equivalent to datetime1 + (-timedelta), because -timedelta in isolation
808
 
   can overflow in cases where datetime1 - timedelta does not.
 
808
   addition, the result has the same :attr:`tzinfo` attribute as the input
 
809
   datetime, and no time zone adjustments are done even if the input is aware.
 
810
   This isn't quite equivalent to datetime1 + (-timedelta), because -timedelta
 
811
   in isolation can overflow in cases where datetime1 - timedelta does not.
809
812
 
810
813
(3)
811
 
   Subtraction of a :class:`datetime` from a :class:`datetime` is defined only if
 
814
   Subtraction of a :class:`.datetime` from a :class:`.datetime` is defined only if
812
815
   both operands are naive, or if both are aware.  If one is aware and the other is
813
816
   naive, :exc:`TypeError` is raised.
814
817
 
815
 
   If both are naive, or both are aware and have the same :attr:`tzinfo` member,
816
 
   the :attr:`tzinfo` members are ignored, and the result is a :class:`timedelta`
 
818
   If both are naive, or both are aware and have the same :attr:`tzinfo` attribute,
 
819
   the :attr:`tzinfo` attributes are ignored, and the result is a :class:`timedelta`
817
820
   object *t* such that ``datetime2 + t == datetime1``.  No time zone adjustments
818
821
   are done in this case.
819
822
 
820
 
   If both are aware and have different :attr:`tzinfo` members, ``a-b`` acts as if
821
 
   *a* and *b* were first converted to naive UTC datetimes first.  The result is
822
 
   ``(a.replace(tzinfo=None) - a.utcoffset()) - (b.replace(tzinfo=None) -
823
 
   b.utcoffset())`` except that the implementation never overflows.
 
823
   If both are aware and have different :attr:`tzinfo` attributes, ``a-b`` acts
 
824
   as if *a* and *b* were first converted to naive UTC datetimes first.  The
 
825
   result is ``(a.replace(tzinfo=None) - a.utcoffset()) - (b.replace(tzinfo=None)
 
826
   - b.utcoffset())`` except that the implementation never overflows.
824
827
 
825
828
(4)
826
829
   *datetime1* is considered less than *datetime2* when *datetime1* precedes
827
830
   *datetime2* in time.
828
831
 
829
832
   If one comparand is naive and the other is aware, :exc:`TypeError` is raised.
830
 
   If both comparands are aware, and have the same :attr:`tzinfo` member, the
831
 
   common :attr:`tzinfo` member is ignored and the base datetimes are compared.  If
832
 
   both comparands are aware and have different :attr:`tzinfo` members, the
833
 
   comparands are first adjusted by subtracting their UTC offsets (obtained from
834
 
   ``self.utcoffset()``).
 
833
   If both comparands are aware, and have the same :attr:`tzinfo` attribute, the
 
834
   common :attr:`tzinfo` attribute is ignored and the base datetimes are
 
835
   compared.  If both comparands are aware and have different :attr:`tzinfo`
 
836
   attributes, the comparands are first adjusted by subtracting their UTC
 
837
   offsets (obtained from ``self.utcoffset()``).
835
838
 
836
839
   .. note::
837
840
 
838
841
      In order to stop comparison from falling back to the default scheme of comparing
839
842
      object addresses, datetime comparison normally raises :exc:`TypeError` if the
840
 
      other comparand isn't also a :class:`datetime` object.  However,
 
843
      other comparand isn't also a :class:`.datetime` object.  However,
841
844
      ``NotImplemented`` is returned instead if the other comparand has a
842
845
      :meth:`timetuple` attribute.  This hook gives other kinds of date objects a
843
 
      chance at implementing mixed-type comparison.  If not, when a :class:`datetime`
 
846
      chance at implementing mixed-type comparison.  If not, when a :class:`.datetime`
844
847
      object is compared to an object of a different type, :exc:`TypeError` is raised
845
848
      unless the comparison is ``==`` or ``!=``.  The latter cases return
846
849
      :const:`False` or :const:`True`, respectively.
847
850
 
848
 
:class:`datetime` objects can be used as dictionary keys. In Boolean contexts,
849
 
all :class:`datetime` objects are considered to be true.
 
851
:class:`.datetime` objects can be used as dictionary keys. In Boolean contexts,
 
852
all :class:`.datetime` objects are considered to be true.
850
853
 
851
854
Instance methods:
852
855
 
857
860
 
858
861
.. method:: datetime.time()
859
862
 
860
 
   Return :class:`time` object with same hour, minute, second and microsecond.
 
863
   Return :class:`.time` object with same hour, minute, second and microsecond.
861
864
   :attr:`tzinfo` is ``None``.  See also method :meth:`timetz`.
862
865
 
863
866
 
864
867
.. method:: datetime.timetz()
865
868
 
866
 
   Return :class:`time` object with same hour, minute, second, microsecond, and
867
 
   tzinfo members.  See also method :meth:`time`.
 
869
   Return :class:`.time` object with same hour, minute, second, microsecond, and
 
870
   tzinfo attributes.  See also method :meth:`time`.
868
871
 
869
872
 
870
873
.. method:: datetime.replace([year[, month[, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]]]]])
871
874
 
872
 
   Return a datetime with the same members, except for those members given new
873
 
   values by whichever keyword arguments are specified.  Note that ``tzinfo=None``
874
 
   can be specified to create a naive datetime from an aware datetime with no
875
 
   conversion of date and time members.
 
875
   Return a datetime with the same attributes, except for those attributes given
 
876
   new values by whichever keyword arguments are specified.  Note that
 
877
   ``tzinfo=None`` can be specified to create a naive datetime from an aware
 
878
   datetime with no conversion of date and time data.
876
879
 
877
880
 
878
881
.. method:: datetime.astimezone(tz)
879
882
 
880
 
   Return a :class:`datetime` object with new :attr:`tzinfo` member *tz*, adjusting
881
 
   the date and time members so the result is the same UTC time as *self*, but in
882
 
   *tz*'s local time.
 
883
   Return a :class:`.datetime` object with new :attr:`tzinfo` attribute *tz*,
 
884
   adjusting the date and time data so the result is the same UTC time as
 
885
   *self*, but in *tz*'s local time.
883
886
 
884
887
   *tz* must be an instance of a :class:`tzinfo` subclass, and its
885
888
   :meth:`utcoffset` and :meth:`dst` methods must not return ``None``.  *self* must
887
890
   not return ``None``).
888
891
 
889
892
   If ``self.tzinfo`` is *tz*, ``self.astimezone(tz)`` is equal to *self*:  no
890
 
   adjustment of date or time members is performed. Else the result is local time
891
 
   in time zone *tz*, representing the same UTC time as *self*:  after ``astz =
892
 
   dt.astimezone(tz)``, ``astz - astz.utcoffset()`` will usually have the same date
893
 
   and time members as ``dt - dt.utcoffset()``. The discussion of class
894
 
   :class:`tzinfo` explains the cases at Daylight Saving Time transition boundaries
895
 
   where this cannot be achieved (an issue only if *tz* models both standard and
896
 
   daylight time).
 
893
   adjustment of date or time data is performed. Else the result is local
 
894
   time in time zone *tz*, representing the same UTC time as *self*:  after
 
895
   ``astz = dt.astimezone(tz)``, ``astz - astz.utcoffset()`` will usually have
 
896
   the same date and time data as ``dt - dt.utcoffset()``. The discussion
 
897
   of class :class:`tzinfo` explains the cases at Daylight Saving Time transition
 
898
   boundaries where this cannot be achieved (an issue only if *tz* models both
 
899
   standard and daylight time).
897
900
 
898
901
   If you merely want to attach a time zone object *tz* to a datetime *dt* without
899
 
   adjustment of date and time members, use ``dt.replace(tzinfo=tz)``.  If you
 
902
   adjustment of date and time data, use ``dt.replace(tzinfo=tz)``.  If you
900
903
   merely want to remove the time zone object from an aware datetime *dt* without
901
 
   conversion of date and time members, use ``dt.replace(tzinfo=None)``.
 
904
   conversion of date and time data, use ``dt.replace(tzinfo=None)``.
902
905
 
903
906
   Note that the default :meth:`tzinfo.fromutc` method can be overridden in a
904
907
   :class:`tzinfo` subclass to affect the result returned by :meth:`astimezone`.
951
954
 
952
955
.. method:: datetime.utctimetuple()
953
956
 
954
 
   If :class:`datetime` instance *d* is naive, this is the same as
 
957
   If :class:`.datetime` instance *d* is naive, this is the same as
955
958
   ``d.timetuple()`` except that :attr:`tm_isdst` is forced to 0 regardless of what
956
959
   ``d.dst()`` returns.  DST is never in effect for a UTC time.
957
960
 
1012
1015
 
1013
1016
.. method:: datetime.__str__()
1014
1017
 
1015
 
   For a :class:`datetime` instance *d*, ``str(d)`` is equivalent to
 
1018
   For a :class:`.datetime` instance *d*, ``str(d)`` is equivalent to
1016
1019
   ``d.isoformat(' ')``.
1017
1020
 
1018
1021
 
1021
1024
   Return a string representing the date and time, for example ``datetime(2002, 12,
1022
1025
   4, 20, 30, 40).ctime() == 'Wed Dec  4 20:30:40 2002'``. ``d.ctime()`` is
1023
1026
   equivalent to ``time.ctime(time.mktime(d.timetuple()))`` on platforms where the
1024
 
   native C :cfunc:`ctime` function (which :func:`time.ctime` invokes, but which
 
1027
   native C :c:func:`ctime` function (which :func:`time.ctime` invokes, but which
1025
1028
   :meth:`datetime.ctime` does not invoke) conforms to the C standard.
1026
1029
 
1027
1030
 
1161
1164
 
1162
1165
.. attribute:: time.min
1163
1166
 
1164
 
   The earliest representable :class:`time`, ``time(0, 0, 0, 0)``.
 
1167
   The earliest representable :class:`.time`, ``time(0, 0, 0, 0)``.
1165
1168
 
1166
1169
 
1167
1170
.. attribute:: time.max
1168
1171
 
1169
 
   The latest representable :class:`time`, ``time(23, 59, 59, 999999)``.
 
1172
   The latest representable :class:`.time`, ``time(23, 59, 59, 999999)``.
1170
1173
 
1171
1174
 
1172
1175
.. attribute:: time.resolution
1173
1176
 
1174
 
   The smallest possible difference between non-equal :class:`time` objects,
1175
 
   ``timedelta(microseconds=1)``, although note that arithmetic on :class:`time`
1176
 
   objects is not supported.
 
1177
   The smallest possible difference between non-equal :class:`.time` objects,
 
1178
   ``timedelta(microseconds=1)``, although note that arithmetic on
 
1179
   :class:`.time` objects is not supported.
1177
1180
 
1178
1181
 
1179
1182
Instance attributes (read-only):
1200
1203
 
1201
1204
.. attribute:: time.tzinfo
1202
1205
 
1203
 
   The object passed as the tzinfo argument to the :class:`time` constructor, or
 
1206
   The object passed as the tzinfo argument to the :class:`.time` constructor, or
1204
1207
   ``None`` if none was passed.
1205
1208
 
1206
1209
 
1207
1210
Supported operations:
1208
1211
 
1209
 
* comparison of :class:`time` to :class:`time`, where *a* is considered less
 
1212
* comparison of :class:`.time` to :class:`.time`, where *a* is considered less
1210
1213
  than *b* when *a* precedes *b* in time.  If one comparand is naive and the other
1211
1214
  is aware, :exc:`TypeError` is raised.  If both comparands are aware, and have
1212
 
  the same :attr:`tzinfo` member, the common :attr:`tzinfo` member is ignored and
1213
 
  the base times are compared.  If both comparands are aware and have different
1214
 
  :attr:`tzinfo` members, the comparands are first adjusted by subtracting their
1215
 
  UTC offsets (obtained from ``self.utcoffset()``). In order to stop mixed-type
1216
 
  comparisons from falling back to the default comparison by object address, when
1217
 
  a :class:`time` object is compared to an object of a different type,
1218
 
  :exc:`TypeError` is raised unless the comparison is ``==`` or ``!=``.  The
1219
 
  latter cases return :const:`False` or :const:`True`, respectively.
 
1215
  the same :attr:`tzinfo` attribute, the common :attr:`tzinfo` attribute is
 
1216
  ignored and the base times are compared.  If both comparands are aware and
 
1217
  have different :attr:`tzinfo` attributes, the comparands are first adjusted by
 
1218
  subtracting their UTC offsets (obtained from ``self.utcoffset()``). In order
 
1219
  to stop mixed-type comparisons from falling back to the default comparison by
 
1220
  object address, when a :class:`.time` object is compared to an object of a
 
1221
  different type, :exc:`TypeError` is raised unless the comparison is ``==`` or
 
1222
  ``!=``.  The latter cases return :const:`False` or :const:`True`, respectively.
1220
1223
 
1221
1224
* hash, use as dict key
1222
1225
 
1223
1226
* efficient pickling
1224
1227
 
1225
 
* in Boolean contexts, a :class:`time` object is considered to be true if and
 
1228
* in Boolean contexts, a :class:`.time` object is considered to be true if and
1226
1229
  only if, after converting it to minutes and subtracting :meth:`utcoffset` (or
1227
1230
  ``0`` if that's ``None``), the result is non-zero.
1228
1231
 
1231
1234
 
1232
1235
.. method:: time.replace([hour[, minute[, second[, microsecond[, tzinfo]]]]])
1233
1236
 
1234
 
   Return a :class:`time` with the same value, except for those members given new
1235
 
   values by whichever keyword arguments are specified.  Note that ``tzinfo=None``
1236
 
   can be specified to create a naive :class:`time` from an aware :class:`time`,
1237
 
   without conversion of the time members.
 
1237
   Return a :class:`.time` with the same value, except for those attributes given
 
1238
   new values by whichever keyword arguments are specified.  Note that
 
1239
   ``tzinfo=None`` can be specified to create a naive :class:`.time` from an
 
1240
   aware :class:`.time`, without conversion of the time data.
1238
1241
 
1239
1242
 
1240
1243
.. method:: time.isoformat()
1312
1315
:class:`tzinfo` is an abstract base class, meaning that this class should not be
1313
1316
instantiated directly.  You need to derive a concrete subclass, and (at least)
1314
1317
supply implementations of the standard :class:`tzinfo` methods needed by the
1315
 
:class:`datetime` methods you use.  The :mod:`datetime` module does not supply
 
1318
:class:`.datetime` methods you use.  The :mod:`datetime` module does not supply
1316
1319
any concrete subclasses of :class:`tzinfo`.
1317
1320
 
1318
1321
An instance of (a concrete subclass of) :class:`tzinfo` can be passed to the
1319
 
constructors for :class:`datetime` and :class:`time` objects. The latter objects
1320
 
view their members as being in local time, and the :class:`tzinfo` object
 
1322
constructors for :class:`.datetime` and :class:`.time` objects. The latter objects
 
1323
view their attributes as being in local time, and the :class:`tzinfo` object
1321
1324
supports methods revealing offset of local time from UTC, the name of the time
1322
1325
zone, and DST offset, all relative to a date or time object passed to them.
1323
1326
 
1362
1365
   already been added to the UTC offset returned by :meth:`utcoffset`, so there's
1363
1366
   no need to consult :meth:`dst` unless you're interested in obtaining DST info
1364
1367
   separately.  For example, :meth:`datetime.timetuple` calls its :attr:`tzinfo`
1365
 
   member's :meth:`dst` method to determine how the :attr:`tm_isdst` flag should be
1366
 
   set, and :meth:`tzinfo.fromutc` calls :meth:`dst` to account for DST changes
1367
 
   when crossing time zones.
 
1368
   attribute's :meth:`dst` method to determine how the :attr:`tm_isdst` flag
 
1369
   should be set, and :meth:`tzinfo.fromutc` calls :meth:`dst` to account for
 
1370
   DST changes when crossing time zones.
1368
1371
 
1369
1372
   An instance *tz* of a :class:`tzinfo` subclass that models both standard and
1370
1373
   daylight times must be consistent in this sense:
1371
1374
 
1372
1375
   ``tz.utcoffset(dt) - tz.dst(dt)``
1373
1376
 
1374
 
   must return the same result for every :class:`datetime` *dt* with ``dt.tzinfo ==
 
1377
   must return the same result for every :class:`.datetime` *dt* with ``dt.tzinfo ==
1375
1378
   tz``  For sane :class:`tzinfo` subclasses, this expression yields the time
1376
1379
   zone's "standard offset", which should not depend on the date or the time, but
1377
1380
   only on geographic location.  The implementation of :meth:`datetime.astimezone`
1382
1385
 
1383
1386
   Most implementations of :meth:`dst` will probably look like one of these two::
1384
1387
 
1385
 
      def dst(self):
 
1388
      def dst(self, dt):
1386
1389
          # a fixed-offset class:  doesn't account for DST
1387
1390
          return timedelta(0)
1388
1391
 
1389
1392
   or ::
1390
1393
 
1391
 
      def dst(self):
 
1394
      def dst(self, dt):
1392
1395
          # Code to set dston and dstoff to the time zone's DST
1393
1396
          # transition times based on the input dt.year, and expressed
1394
1397
          # in standard local time.  Then
1403
1406
 
1404
1407
.. method:: tzinfo.tzname(self, dt)
1405
1408
 
1406
 
   Return the time zone name corresponding to the :class:`datetime` object *dt*, as
 
1409
   Return the time zone name corresponding to the :class:`.datetime` object *dt*, as
1407
1410
   a string. Nothing about string names is defined by the :mod:`datetime` module,
1408
1411
   and there's no requirement that it mean anything in particular.  For example,
1409
1412
   "GMT", "UTC", "-500", "-5:00", "EDT", "US/Eastern", "America/New York" are all
1416
1419
   The default implementation of :meth:`tzname` raises :exc:`NotImplementedError`.
1417
1420
 
1418
1421
 
1419
 
These methods are called by a :class:`datetime` or :class:`time` object, in
1420
 
response to their methods of the same names.  A :class:`datetime` object passes
1421
 
itself as the argument, and a :class:`time` object passes ``None`` as the
 
1422
These methods are called by a :class:`.datetime` or :class:`.time` object, in
 
1423
response to their methods of the same names.  A :class:`.datetime` object passes
 
1424
itself as the argument, and a :class:`.time` object passes ``None`` as the
1422
1425
argument.  A :class:`tzinfo` subclass's methods should therefore be prepared to
1423
 
accept a *dt* argument of ``None``, or of class :class:`datetime`.
 
1426
accept a *dt* argument of ``None``, or of class :class:`.datetime`.
1424
1427
 
1425
1428
When ``None`` is passed, it's up to the class designer to decide the best
1426
1429
response.  For example, returning ``None`` is appropriate if the class wishes to
1428
1431
may be more useful for ``utcoffset(None)`` to return the standard UTC offset, as
1429
1432
there is no other convention for discovering the standard offset.
1430
1433
 
1431
 
When a :class:`datetime` object is passed in response to a :class:`datetime`
 
1434
When a :class:`.datetime` object is passed in response to a :class:`.datetime`
1432
1435
method, ``dt.tzinfo`` is the same object as *self*.  :class:`tzinfo` methods can
1433
1436
rely on this, unless user code calls :class:`tzinfo` methods directly.  The
1434
1437
intent is that the :class:`tzinfo` methods interpret *dt* as being in local
1439
1442
 
1440
1443
.. method:: tzinfo.fromutc(self, dt)
1441
1444
 
1442
 
   This is called from the default :class:`datetime.astimezone()` implementation.
1443
 
   When called from that, ``dt.tzinfo`` is *self*, and *dt*'s date and time members
1444
 
   are to be viewed as expressing a UTC time.  The purpose of :meth:`fromutc` is to
1445
 
   adjust the date and time members, returning an equivalent datetime in *self*'s
1446
 
   local time.
 
1445
   This is called from the default :class:`datetime.astimezone()`
 
1446
   implementation.  When called from that, ``dt.tzinfo`` is *self*, and *dt*'s
 
1447
   date and time data are to be viewed as expressing a UTC time.  The purpose
 
1448
   of :meth:`fromutc` is to adjust the date and time data, returning an
 
1449
   equivalent datetime in *self*'s local time.
1447
1450
 
1448
1451
   Most :class:`tzinfo` subclasses should be able to inherit the default
1449
1452
   :meth:`fromutc` implementation without problems.  It's strong enough to handle
1524
1527
:meth:`strftime` and :meth:`strptime` Behavior
1525
1528
----------------------------------------------
1526
1529
 
1527
 
:class:`date`, :class:`datetime`, and :class:`time` objects all support a
 
1530
:class:`date`, :class:`.datetime`, and :class:`.time` objects all support a
1528
1531
``strftime(format)`` method, to create a string representing the time under the
1529
1532
control of an explicit format string.  Broadly speaking, ``d.strftime(fmt)``
1530
1533
acts like the :mod:`time` module's ``time.strftime(fmt, d.timetuple())``
1531
1534
although not all objects support a :meth:`timetuple` method.
1532
1535
 
1533
1536
Conversely, the :meth:`datetime.strptime` class method creates a
1534
 
:class:`datetime` object from a string representing a date and time and a
 
1537
:class:`.datetime` object from a string representing a date and time and a
1535
1538
corresponding format string. ``datetime.strptime(date_string, format)`` is
1536
1539
equivalent to ``datetime(*(time.strptime(date_string, format)[0:6]))``.
1537
1540
 
1538
 
For :class:`time` objects, the format codes for year, month, and day should not
 
1541
For :class:`.time` objects, the format codes for year, month, and day should not
1539
1542
be used, as time objects have no such values.  If they're used anyway, ``1900``
1540
1543
is substituted for the year, and ``1`` for the month and day.
1541
1544
 
1544
1547
values.  If they're used anyway, ``0`` is substituted for them.
1545
1548
 
1546
1549
.. versionadded:: 2.6
1547
 
   :class:`time` and :class:`datetime` objects support a ``%f`` format code
 
1550
   :class:`.time` and :class:`.datetime` objects support a ``%f`` format code
1548
1551
   which expands to the number of microseconds in the object, zero-padded on
1549
1552
   the left to six places.
1550
1553