~ubuntu-branches/ubuntu/trusty/python3.3/trusty

« back to all changes in this revision

Viewing changes to Doc/library/datetime.rst

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-11-19 08:46:55 UTC
  • mfrom: (22.1.15 sid)
  • Revision ID: package-import@ubuntu.com-20131119084655-pueqfadzs5v1xf53
Tags: 3.3.3-1
* Python 3.3.3 release.
* Update to 20131119 from the 3.3 branch.
* Regenerate the patches.
* Update the symbols files.
* Fix test support when the running kernel doesn't handle port reuse.
* libpython3.3-minimal replaces libpython3.3-stdlib (<< 3.2.3-7).
  Closes: #725240.

Show diffs side-by-side

added added

removed removed

Lines of Context:
593
593
.. method:: date.strftime(format)
594
594
 
595
595
   Return a string representing the date, controlled by an explicit format string.
596
 
   Format codes referring to hours, minutes or seconds will see 0 values. See
597
 
   section :ref:`strftime-strptime-behavior`.
 
596
   Format codes referring to hours, minutes or seconds will see 0 values. For a
 
597
   complete list of formatting directives, see
 
598
   :ref:`strftime-strptime-behavior`.
598
599
 
599
600
 
600
601
.. method:: date.__format__(format)
601
602
 
602
603
   Same as :meth:`.date.strftime`. This makes it possible to specify format
603
 
   string for a :class:`.date` object when using :meth:`str.format`.
604
 
   See section :ref:`strftime-strptime-behavior`.
 
604
   string for a :class:`.date` object when using :meth:`str.format`. For a
 
605
   complete list of formatting directives, see
 
606
   :ref:`strftime-strptime-behavior`.
605
607
 
606
608
 
607
609
Example of counting days to an event::
793
795
   *format*.  This is equivalent to ``datetime(*(time.strptime(date_string,
794
796
   format)[0:6]))``. :exc:`ValueError` is raised if the date_string and format
795
797
   can't be parsed by :func:`time.strptime` or if it returns a value which isn't a
796
 
   time tuple. See section :ref:`strftime-strptime-behavior`.
 
798
   time tuple. For a complete list of formatting directives, see
 
799
   :ref:`strftime-strptime-behavior`.
797
800
 
798
801
 
799
802
 
1160
1163
.. method:: datetime.strftime(format)
1161
1164
 
1162
1165
   Return a string representing the date and time, controlled by an explicit format
1163
 
   string.  See section :ref:`strftime-strptime-behavior`.
 
1166
   string.  For a complete list of formatting directives, see
 
1167
   :ref:`strftime-strptime-behavior`.
1164
1168
 
1165
1169
 
1166
1170
.. method:: datetime.__format__(format)
1167
1171
 
1168
1172
   Same as :meth:`.datetime.strftime`.  This makes it possible to specify format
1169
 
   string for a :class:`.datetime` object when using :meth:`str.format`.
1170
 
   See section :ref:`strftime-strptime-behavior`.
 
1173
   string for a :class:`.datetime` object when using :meth:`str.format`.  For a
 
1174
   complete list of formatting directives, see
 
1175
   :ref:`strftime-strptime-behavior`.
1171
1176
 
1172
1177
 
1173
1178
Examples of working with datetime objects:
1399
1404
 
1400
1405
.. method:: time.strftime(format)
1401
1406
 
1402
 
   Return a string representing the time, controlled by an explicit format string.
1403
 
   See section :ref:`strftime-strptime-behavior`.
 
1407
   Return a string representing the time, controlled by an explicit format
 
1408
   string.  For a complete list of formatting directives, see
 
1409
   :ref:`strftime-strptime-behavior`.
1404
1410
 
1405
1411
 
1406
1412
.. method:: time.__format__(format)
1407
1413
 
1408
1414
   Same as :meth:`.time.strftime`. This makes it possible to specify format string
1409
 
   for a :class:`.time` object when using :meth:`str.format`.
1410
 
   See section :ref:`strftime-strptime-behavior`.
 
1415
   for a :class:`.time` object when using :meth:`str.format`.  For a
 
1416
   complete list of formatting directives, see
 
1417
   :ref:`strftime-strptime-behavior`.
1411
1418
 
1412
1419
 
1413
1420
.. method:: time.utcoffset()
1773
1780
microseconds should not be used, as :class:`date` objects have no such
1774
1781
values.  If they're used anyway, ``0`` is substituted for them.
1775
1782
 
1776
 
For a naive object, the ``%z`` and ``%Z`` format codes are replaced by empty
1777
 
strings.
1778
 
 
1779
 
For an aware object:
1780
 
 
1781
 
``%z``
1782
 
   :meth:`utcoffset` is transformed into a 5-character string of the form +HHMM or
1783
 
   -HHMM, where HH is a 2-digit string giving the number of UTC offset hours, and
1784
 
   MM is a 2-digit string giving the number of UTC offset minutes.  For example, if
1785
 
   :meth:`utcoffset` returns ``timedelta(hours=-3, minutes=-30)``, ``%z`` is
1786
 
   replaced with the string ``'-0330'``.
1787
 
 
1788
 
``%Z``
1789
 
   If :meth:`tzname` returns ``None``, ``%Z`` is replaced by an empty string.
1790
 
   Otherwise ``%Z`` is replaced by the returned value, which must be a string.
1791
 
 
1792
1783
The full set of format codes supported varies across platforms, because Python
1793
1784
calls the platform C library's :func:`strftime` function, and platform
1794
 
variations are common.
 
1785
variations are common.  To see the full set of format codes supported on your
 
1786
platform, consult the :manpage:`strftime(3)` documentation.
1795
1787
 
1796
1788
The following is a list of all the format codes that the C standard (1989
1797
1789
version) requires, and these work on all platforms with a standard C
1798
1790
implementation.  Note that the 1999 version of the C standard added additional
1799
1791
format codes.
1800
1792
 
1801
 
+-----------+--------------------------------+-------+
1802
 
| Directive | Meaning                        | Notes |
1803
 
+===========+================================+=======+
1804
 
| ``%a``    | Locale's abbreviated weekday   |       |
1805
 
|           | name.                          |       |
1806
 
+-----------+--------------------------------+-------+
1807
 
| ``%A``    | Locale's full weekday name.    |       |
1808
 
+-----------+--------------------------------+-------+
1809
 
| ``%b``    | Locale's abbreviated month     |       |
1810
 
|           | name.                          |       |
1811
 
+-----------+--------------------------------+-------+
1812
 
| ``%B``    | Locale's full month name.      |       |
1813
 
+-----------+--------------------------------+-------+
1814
 
| ``%c``    | Locale's appropriate date and  |       |
1815
 
|           | time representation.           |       |
1816
 
+-----------+--------------------------------+-------+
1817
 
| ``%d``    | Day of the month as a decimal  |       |
1818
 
|           | number [01,31].                |       |
1819
 
+-----------+--------------------------------+-------+
1820
 
| ``%f``    | Microsecond as a decimal       | \(1)  |
1821
 
|           | number [0,999999], zero-padded |       |
1822
 
|           | on the left                    |       |
1823
 
+-----------+--------------------------------+-------+
1824
 
| ``%H``    | Hour (24-hour clock) as a      |       |
1825
 
|           | decimal number [00,23].        |       |
1826
 
+-----------+--------------------------------+-------+
1827
 
| ``%I``    | Hour (12-hour clock) as a      |       |
1828
 
|           | decimal number [01,12].        |       |
1829
 
+-----------+--------------------------------+-------+
1830
 
| ``%j``    | Day of the year as a decimal   |       |
1831
 
|           | number [001,366].              |       |
1832
 
+-----------+--------------------------------+-------+
1833
 
| ``%m``    | Month as a decimal number      |       |
1834
 
|           | [01,12].                       |       |
1835
 
+-----------+--------------------------------+-------+
1836
 
| ``%M``    | Minute as a decimal number     |       |
1837
 
|           | [00,59].                       |       |
1838
 
+-----------+--------------------------------+-------+
1839
 
| ``%p``    | Locale's equivalent of either  | \(2)  |
1840
 
|           | AM or PM.                      |       |
1841
 
+-----------+--------------------------------+-------+
1842
 
| ``%S``    | Second as a decimal number     | \(3)  |
1843
 
|           | [00,59].                       |       |
1844
 
+-----------+--------------------------------+-------+
1845
 
| ``%U``    | Week number of the year        | \(4)  |
1846
 
|           | (Sunday as the first day of    |       |
1847
 
|           | the week) as a decimal number  |       |
1848
 
|           | [00,53].  All days in a new    |       |
1849
 
|           | year preceding the first       |       |
1850
 
|           | Sunday are considered to be in |       |
1851
 
|           | week 0.                        |       |
1852
 
+-----------+--------------------------------+-------+
1853
 
| ``%w``    | Weekday as a decimal number    |       |
1854
 
|           | [0(Sunday),6].                 |       |
1855
 
+-----------+--------------------------------+-------+
1856
 
| ``%W``    | Week number of the year        | \(4)  |
1857
 
|           | (Monday as the first day of    |       |
1858
 
|           | the week) as a decimal number  |       |
1859
 
|           | [00,53].  All days in a new    |       |
1860
 
|           | year preceding the first       |       |
1861
 
|           | Monday are considered to be in |       |
1862
 
|           | week 0.                        |       |
1863
 
+-----------+--------------------------------+-------+
1864
 
| ``%x``    | Locale's appropriate date      |       |
1865
 
|           | representation.                |       |
1866
 
+-----------+--------------------------------+-------+
1867
 
| ``%X``    | Locale's appropriate time      |       |
1868
 
|           | representation.                |       |
1869
 
+-----------+--------------------------------+-------+
1870
 
| ``%y``    | Year without century as a      |       |
1871
 
|           | decimal number [00,99].        |       |
1872
 
+-----------+--------------------------------+-------+
1873
 
| ``%Y``    | Year with century as a decimal | \(5)  |
1874
 
|           | number [0001,9999].            |       |
1875
 
+-----------+--------------------------------+-------+
1876
 
| ``%z``    | UTC offset in the form +HHMM   | \(6)  |
1877
 
|           | or -HHMM (empty string if the  |       |
1878
 
|           | the object is naive).          |       |
1879
 
+-----------+--------------------------------+-------+
1880
 
| ``%Z``    | Time zone name (empty string   |       |
1881
 
|           | if the object is naive).       |       |
1882
 
+-----------+--------------------------------+-------+
1883
 
| ``%%``    | A literal ``'%'`` character.   |       |
1884
 
+-----------+--------------------------------+-------+
 
1793
+-----------+--------------------------------+------------------------+-------+
 
1794
| Directive | Meaning                        | Example                | Notes |
 
1795
+===========+================================+========================+=======+
 
1796
| ``%a``    | Weekday as locale's            || Sun, Mon, ..., Sat    | \(1)  |
 
1797
|           | abbreviated name.              |  (en_US);              |       |
 
1798
|           |                                || So, Mo, ..., Sa       |       |
 
1799
|           |                                |  (de_DE)               |       |
 
1800
+-----------+--------------------------------+------------------------+-------+
 
1801
| ``%A``    | Weekday as locale's full name. || Sunday, Monday, ...,  | \(1)  |
 
1802
|           |                                |  Saturday (en_US);     |       |
 
1803
|           |                                || Sonntag, Montag, ..., |       |
 
1804
|           |                                |  Samstag (de_DE)       |       |
 
1805
+-----------+--------------------------------+------------------------+-------+
 
1806
| ``%w``    | Weekday as a decimal number,   | 0, 1, ..., 6           |       |
 
1807
|           | where 0 is Sunday and 6 is     |                        |       |
 
1808
|           | Saturday.                      |                        |       |
 
1809
+-----------+--------------------------------+------------------------+-------+
 
1810
| ``%d``    | Day of the month as a          | 01, 02, ..., 31        |       |
 
1811
|           | zero-padded decimal number.    |                        |       |
 
1812
+-----------+--------------------------------+------------------------+-------+
 
1813
| ``%b``    | Month as locale's abbreviated  || Jan, Feb, ..., Dec    | \(1)  |
 
1814
|           | name.                          |  (en_US);              |       |
 
1815
|           |                                || Jan, Feb, ..., Dez    |       |
 
1816
|           |                                |  (de_DE)               |       |
 
1817
+-----------+--------------------------------+------------------------+-------+
 
1818
| ``%B``    | Month as locale's full name.   || January, February,    | \(1)  |
 
1819
|           |                                |  ..., December (en_US);|       |
 
1820
|           |                                || Januar, Februar, ..., |       |
 
1821
|           |                                |  Dezember (de_DE)      |       |
 
1822
+-----------+--------------------------------+------------------------+-------+
 
1823
| ``%m``    | Month as a zero-padded         | 01, 02, ..., 12        |       |
 
1824
|           | decimal number.                |                        |       |
 
1825
+-----------+--------------------------------+------------------------+-------+
 
1826
| ``%y``    | Year without century as a      | 00, 01, ..., 99        |       |
 
1827
|           | zero-padded decimal number.    |                        |       |
 
1828
+-----------+--------------------------------+------------------------+-------+
 
1829
| ``%Y``    | Year with century as a decimal | 0001, 0002, ..., 2013, | \(2)  |
 
1830
|           | number.                        | 2014, ..., 9998, 9999  |       |
 
1831
+-----------+--------------------------------+------------------------+-------+
 
1832
| ``%H``    | Hour (24-hour clock) as a      | 00, 01, ..., 23        |       |
 
1833
|           | zero-padded decimal number.    |                        |       |
 
1834
+-----------+--------------------------------+------------------------+-------+
 
1835
| ``%I``    | Hour (12-hour clock) as a      | 01, 02, ..., 12        |       |
 
1836
|           | zero-padded decimal number.    |                        |       |
 
1837
+-----------+--------------------------------+------------------------+-------+
 
1838
| ``%p``    | Locale's equivalent of either  || AM, PM (en_US);       | \(1), |
 
1839
|           | AM or PM.                      || am, pm (de_DE)        | \(3)  |
 
1840
+-----------+--------------------------------+------------------------+-------+
 
1841
| ``%M``    | Minute as a zero-padded        | 00, 01, ..., 59        |       |
 
1842
|           | decimal number.                |                        |       |
 
1843
+-----------+--------------------------------+------------------------+-------+
 
1844
| ``%S``    | Second as a zero-padded        | 00, 01, ..., 59        | \(4)  |
 
1845
|           | decimal number.                |                        |       |
 
1846
+-----------+--------------------------------+------------------------+-------+
 
1847
| ``%f``    | Microsecond as a decimal       | 000000, 000001, ...,   | \(5)  |
 
1848
|           | number, zero-padded on the     | 999999                 |       |
 
1849
|           | left.                          |                        |       |
 
1850
+-----------+--------------------------------+------------------------+-------+
 
1851
| ``%z``    | UTC offset in the form +HHMM   | (empty), +0000, -0400, | \(6)  |
 
1852
|           | or -HHMM (empty string if the  | +1030                  |       |
 
1853
|           | the object is naive).          |                        |       |
 
1854
+-----------+--------------------------------+------------------------+-------+
 
1855
| ``%Z``    | Time zone name (empty string   | (empty), UTC, EST, CST |       |
 
1856
|           | if the object is naive).       |                        |       |
 
1857
+-----------+--------------------------------+------------------------+-------+
 
1858
| ``%j``    | Day of the year as a           | 001, 002, ..., 366     |       |
 
1859
|           | zero-padded decimal number.    |                        |       |
 
1860
+-----------+--------------------------------+------------------------+-------+
 
1861
| ``%U``    | Week number of the year        | 00, 01, ..., 53        | \(7)  |
 
1862
|           | (Sunday as the first day of    |                        |       |
 
1863
|           | the week) as a zero padded     |                        |       |
 
1864
|           | decimal number. All days in a  |                        |       |
 
1865
|           | new year preceding the first   |                        |       |
 
1866
|           | Sunday are considered to be in |                        |       |
 
1867
|           | week 0.                        |                        |       |
 
1868
+-----------+--------------------------------+------------------------+-------+
 
1869
| ``%W``    | Week number of the year        | 00, 01, ..., 53        | \(7)  |
 
1870
|           | (Monday as the first day of    |                        |       |
 
1871
|           | the week) as a decimal number. |                        |       |
 
1872
|           | All days in a new year         |                        |       |
 
1873
|           | preceding the first Monday     |                        |       |
 
1874
|           | are considered to be in        |                        |       |
 
1875
|           | week 0.                        |                        |       |
 
1876
+-----------+--------------------------------+------------------------+-------+
 
1877
| ``%c``    | Locale's appropriate date and  || Tue Aug 16 21:30:00   | \(1)  |
 
1878
|           | time representation.           |  1988 (en_US);         |       |
 
1879
|           |                                || Di 16 Aug 21:30:00    |       |
 
1880
|           |                                |  1988 (de_DE)          |       |
 
1881
+-----------+--------------------------------+------------------------+-------+
 
1882
| ``%x``    | Locale's appropriate date      || 08/16/88 (None);      | \(1)  |
 
1883
|           | representation.                || 08/16/1988 (en_US);   |       |
 
1884
|           |                                || 16.08.1988 (de_DE)    |       |
 
1885
+-----------+--------------------------------+------------------------+-------+
 
1886
| ``%X``    | Locale's appropriate time      || 21:30:00 (en_US);     | \(1)  |
 
1887
|           | representation.                || 21:30:00 (de_DE)      |       |
 
1888
+-----------+--------------------------------+------------------------+-------+
 
1889
| ``%%``    | A literal ``'%'`` character.   | %                      |       |
 
1890
+-----------+--------------------------------+------------------------+-------+
1885
1891
 
1886
1892
Notes:
1887
1893
 
1888
1894
(1)
 
1895
   Because the format depends on the current locale, care should be taken when
 
1896
   making assumptions about the output value. Field orderings will vary (for
 
1897
   example, "month/day/year" versus "day/month/year"), and the output may
 
1898
   contain Unicode characters encoded using the locale's default encoding (for
 
1899
   example, if the current locale is ``js_JP``, the default encoding could be
 
1900
   any one of ``eucJP``, ``SJIS``, or ``utf-8``; use :meth:`locale.getlocale`
 
1901
   to determine the current locale's encoding).
 
1902
 
 
1903
(2)
 
1904
   The :meth:`strptime` method can parse years in the full [1, 9999] range, but
 
1905
   years < 1000 must be zero-filled to 4-digit width.
 
1906
 
 
1907
   .. versionchanged:: 3.2
 
1908
      In previous versions, :meth:`strftime` method was restricted to
 
1909
      years >= 1900.
 
1910
 
 
1911
   .. versionchanged:: 3.3
 
1912
      In version 3.2, :meth:`strftime` method was restricted to
 
1913
      years >= 1000.
 
1914
 
 
1915
(3)
 
1916
   When used with the :meth:`strptime` method, the ``%p`` directive only affects
 
1917
   the output hour field if the ``%I`` directive is used to parse the hour.
 
1918
 
 
1919
(4)
 
1920
   Unlike the :mod:`time` module, the :mod:`datetime` module does not support
 
1921
   leap seconds.
 
1922
 
 
1923
(5)
1889
1924
   When used with the :meth:`strptime` method, the ``%f`` directive
1890
1925
   accepts from one to six digits and zero pads on the right.  ``%f`` is
1891
1926
   an extension to the set of format characters in the C standard (but
1892
1927
   implemented separately in datetime objects, and therefore always
1893
1928
   available).
1894
1929
 
1895
 
(2)
1896
 
   When used with the :meth:`strptime` method, the ``%p`` directive only affects
1897
 
   the output hour field if the ``%I`` directive is used to parse the hour.
1898
 
 
1899
 
(3)
1900
 
   Unlike :mod:`time` module, :mod:`datetime` module does not support
1901
 
   leap seconds.
1902
 
 
1903
 
(4)
1904
 
   When used with the :meth:`strptime` method, ``%U`` and ``%W`` are only used in
1905
 
   calculations when the day of the week and the year are specified.
1906
 
 
1907
 
(5)
1908
 
   The :meth:`strptime` method can
1909
 
   parse years in the full [1, 9999] range, but years < 1000 must be
1910
 
   zero-filled to 4-digit width.
1911
 
 
1912
 
   .. versionchanged:: 3.2
1913
 
      In previous versions, :meth:`strftime` method was restricted to
1914
 
      years >= 1900.
1915
 
 
1916
 
   .. versionchanged:: 3.3
1917
 
      In version 3.2, :meth:`strftime` method was restricted to
1918
 
      years >= 1000.
1919
 
 
1920
1930
(6)
1921
 
   For example, if :meth:`utcoffset` returns ``timedelta(hours=-3, minutes=-30)``,
1922
 
   ``%z`` is replaced with the string ``'-0330'``.
1923
 
 
1924
 
.. versionchanged:: 3.2
1925
 
   When the ``%z`` directive is provided to the :meth:`strptime` method, an
1926
 
   aware :class:`.datetime` object will be produced.  The ``tzinfo`` of the
1927
 
   result will be set to a :class:`timezone` instance.
 
1931
   For a naive object, the ``%z`` and ``%Z`` format codes are replaced by empty
 
1932
   strings.
 
1933
 
 
1934
   For an aware object:
 
1935
 
 
1936
   ``%z``
 
1937
      :meth:`utcoffset` is transformed into a 5-character string of the form
 
1938
      +HHMM or -HHMM, where HH is a 2-digit string giving the number of UTC
 
1939
      offset hours, and MM is a 2-digit string giving the number of UTC offset
 
1940
      minutes.  For example, if :meth:`utcoffset` returns
 
1941
      ``timedelta(hours=-3, minutes=-30)``, ``%z`` is replaced with the string
 
1942
      ``'-0330'``.
 
1943
 
 
1944
   ``%Z``
 
1945
      If :meth:`tzname` returns ``None``, ``%Z`` is replaced by an empty
 
1946
      string.  Otherwise ``%Z`` is replaced by the returned value, which must
 
1947
      be a string.
 
1948
 
 
1949
   .. versionchanged:: 3.2
 
1950
      When the ``%z`` directive is provided to the :meth:`strptime` method, an
 
1951
      aware :class:`.datetime` object will be produced.  The ``tzinfo`` of the
 
1952
      result will be set to a :class:`timezone` instance.
 
1953
 
 
1954
(7)
 
1955
   When used with the :meth:`strptime` method, ``%U`` and ``%W`` are only used
 
1956
   in calculations when the day of the week and the year are specified.
1928
1957
 
1929
1958
.. rubric:: Footnotes
1930
1959