Improving strftime documentation.

Re-ordering the table so similar directives are grouped together, adding
examples, and removing some redundancy in the description of the ``%f``
formatter.
This commit is contained in:
David Wolever 2013-04-13 19:12:58 -04:00
parent 3a2b371f26
commit 73480ef578
1 changed files with 124 additions and 122 deletions

View File

@ -551,8 +551,9 @@ Instance methods:
.. method:: date.strftime(format) .. method:: date.strftime(format)
Return a string representing the date, controlled by an explicit format string. Return a string representing the date, controlled by an explicit format string.
Format codes referring to hours, minutes or seconds will see 0 values. See Format codes referring to hours, minutes or seconds will see 0 values. For a
section :ref:`strftime-strptime-behavior`. complete list of formatting directives, see section
:ref:`strftime-strptime-behavior`.
.. method:: date.__format__(format) .. method:: date.__format__(format)
@ -730,7 +731,8 @@ Other constructors, all class methods:
*format*. This is equivalent to ``datetime(*(time.strptime(date_string, *format*. This is equivalent to ``datetime(*(time.strptime(date_string,
format)[0:6]))``. :exc:`ValueError` is raised if the date_string and format format)[0:6]))``. :exc:`ValueError` is raised if the date_string and format
can't be parsed by :func:`time.strptime` or if it returns a value which isn't a can't be parsed by :func:`time.strptime` or if it returns a value which isn't a
time tuple. See section :ref:`strftime-strptime-behavior`. time tuple. For a complete list of formatting directives, see section
:ref:`strftime-strptime-behavior`.
.. versionadded:: 2.5 .. versionadded:: 2.5
@ -1050,7 +1052,8 @@ Instance methods:
.. method:: datetime.strftime(format) .. method:: datetime.strftime(format)
Return a string representing the date and time, controlled by an explicit format Return a string representing the date and time, controlled by an explicit format
string. See section :ref:`strftime-strptime-behavior`. string. For a complete list of formatting directives, see section
:ref:`strftime-strptime-behavior`.
.. method:: datetime.__format__(format) .. method:: datetime.__format__(format)
@ -1283,7 +1286,8 @@ Instance methods:
.. method:: time.strftime(format) .. method:: time.strftime(format)
Return a string representing the time, controlled by an explicit format string. Return a string representing the time, controlled by an explicit format string.
See section :ref:`strftime-strptime-behavior`. For a complete list of formatting directives, see section
:ref:`strftime-strptime-behavior`.
.. method:: time.__format__(format) .. method:: time.__format__(format)
@ -1597,27 +1601,6 @@ For :class:`date` objects, the format codes for hours, minutes, seconds, and
microseconds should not be used, as :class:`date` objects have no such microseconds should not be used, as :class:`date` objects have no such
values. If they're used anyway, ``0`` is substituted for them. values. If they're used anyway, ``0`` is substituted for them.
.. versionadded:: 2.6
:class:`.time` and :class:`.datetime` objects support a ``%f`` format code
which expands to the number of microseconds in the object, zero-padded on
the left to six places.
For a naive object, the ``%z`` and ``%Z`` format codes are replaced by empty
strings.
For an aware object:
``%z``
:meth:`utcoffset` is transformed into a 5-character string of the form +HHMM or
-HHMM, where HH is a 2-digit string giving the number of UTC offset hours, and
MM is a 2-digit string giving the number of UTC offset minutes. For example, if
:meth:`utcoffset` returns ``timedelta(hours=-3, minutes=-30)``, ``%z`` is
replaced with the string ``'-0330'``.
``%Z``
If :meth:`tzname` returns ``None``, ``%Z`` is replaced by an empty string.
Otherwise ``%Z`` is replaced by the returned value, which must be a string.
The full set of format codes supported varies across platforms, because Python The full set of format codes supported varies across platforms, because Python
calls the platform C library's :func:`strftime` function, and platform calls the platform C library's :func:`strftime` function, and platform
variations are common. variations are common.
@ -1630,105 +1613,101 @@ format codes.
The exact range of years for which :meth:`strftime` works also varies across The exact range of years for which :meth:`strftime` works also varies across
platforms. Regardless of platform, years before 1900 cannot be used. platforms. Regardless of platform, years before 1900 cannot be used.
+-----------+--------------------------------+-------+ +-----------+--------------------------------+------------------------+-------+
| Directive | Meaning | Notes | | Directive | Meaning | Example | Notes |
+===========+================================+=======+ +===========+================================+========================+=======+
| ``%a`` | Locale's abbreviated weekday | | | ``%a`` | Weekday as locale's | Sun, Mon, ..., Sat | |
| | name. | | | | abbreviated name. | | |
+-----------+--------------------------------+-------+ +-----------+--------------------------------+------------------------+-------+
| ``%A`` | Locale's full weekday name. | | | ``%A`` | Weekday as locale's full name. | Sunday, Monday, ..., | |
+-----------+--------------------------------+-------+ | | | Saturday | |
| ``%b`` | Locale's abbreviated month | | +-----------+--------------------------------+------------------------+-------+
| | name. | | | ``%w`` | Weekday as a decimal number, | 0, 1, ..., 6 | |
+-----------+--------------------------------+-------+ | | where 0 is Sunday and 6 is | | |
| ``%B`` | Locale's full month name. | | | | Saturday. | | |
+-----------+--------------------------------+-------+ +-----------+--------------------------------+------------------------+-------+
| ``%c`` | Locale's appropriate date and | | | ``%d`` | Day of the month as a | 01, 02, ..., 31 | |
| | time representation. | | | | zero-padded decimal number. | | |
+-----------+--------------------------------+-------+ +-----------+--------------------------------+------------------------+-------+
| ``%d`` | Day of the month as a decimal | | | ``%j`` | Day of the year as a | 001, 002, ..., 366 | |
| | number [01,31]. | | | | zero-padded decimal number. | | |
+-----------+--------------------------------+-------+ +-----------+--------------------------------+------------------------+-------+
| ``%f`` | Microsecond as a decimal | \(1) | | ``%b`` | Month as locale's abbreviated | Jan, Feb, ..., Dec | |
| | number [0,999999], zero-padded | | | | name. | | |
| | on the left | | +-----------+--------------------------------+------------------------+-------+
+-----------+--------------------------------+-------+ | ``%B`` | Month as locale's full name. | January, February, | |
| ``%H`` | Hour (24-hour clock) as a | | | | | ..., December | |
| | decimal number [00,23]. | | +-----------+--------------------------------+------------------------+-------+
+-----------+--------------------------------+-------+ | ``%m`` | Month as a zero-padded | 01, 02, ..., 12 | |
| ``%I`` | Hour (12-hour clock) as a | | | | decimal number. | | |
| | decimal number [01,12]. | | +-----------+--------------------------------+------------------------+-------+
+-----------+--------------------------------+-------+ | ``%y`` | Year without century as a | 00, 01, ..., 99 | |
| ``%j`` | Day of the year as a decimal | | | | zero-padded decimal number. | | |
| | number [001,366]. | | +-----------+--------------------------------+------------------------+-------+
+-----------+--------------------------------+-------+ | ``%Y`` | Year with century as a decimal | 1970, 1988, 2001, 2013 | |
| ``%m`` | Month as a decimal number | | | | number. | | |
| | [01,12]. | | +-----------+--------------------------------+------------------------+-------+
+-----------+--------------------------------+-------+ | ``%H`` | Hour (24-hour clock) as a | 00, 01, ..., 23 | |
| ``%M`` | Minute as a decimal number | | | | zero-padded decimal number. | | |
| | [00,59]. | | +-----------+--------------------------------+------------------------+-------+
+-----------+--------------------------------+-------+ | ``%I`` | Hour (12-hour clock) as a | 01, 02, ..., 12 | |
| ``%p`` | Locale's equivalent of either | \(2) | | | zero-padded decimal number. | | |
| | AM or PM. | | +-----------+--------------------------------+------------------------+-------+
+-----------+--------------------------------+-------+ | ``%p`` | Locale's equivalent of either | AM, PM | \(1) |
| ``%S`` | Second as a decimal number | \(3) | | | AM or PM. | | |
| | [00,61]. | | +-----------+--------------------------------+------------------------+-------+
+-----------+--------------------------------+-------+ | ``%M`` | Minute as a zero-padded | 00, 01, ..., 59 | |
| ``%U`` | Week number of the year | \(4) | | | decimal number. | | |
| | (Sunday as the first day of | | +-----------+--------------------------------+------------------------+-------+
| | the week) as a decimal number | | | ``%S`` | Second as a zero-padded | 00, 01, ..., 61 | \(2) |
| | [00,53]. All days in a new | | | | decimal number. | | |
| | year preceding the first | | +-----------+--------------------------------+------------------------+-------+
| | Sunday are considered to be in | | | ``%f`` | Microsecond as a decimal | 000000, 000001, ..., | \(3) |
| | week 0. | | | | number, zero-padded on the | 999999 | |
+-----------+--------------------------------+-------+ | | left. | | |
| ``%w`` | Weekday as a decimal number | | +-----------+--------------------------------+------------------------+-------+
| | [0(Sunday),6]. | | | ``%z`` | UTC offset in the form +HHMM | (empty), +0000, -0400, | \(4) |
+-----------+--------------------------------+-------+ | | or -HHMM (empty string if the | +1030 | |
| ``%W`` | Week number of the year | \(4) | | | the object is naive). | | |
| | (Monday as the first day of | | +-----------+--------------------------------+------------------------+-------+
| | the week) as a decimal number | | | ``%Z`` | Time zone name (empty string | (empty), UTC, EST, CST | |
| | [00,53]. All days in a new | | | | if the object is naive). | | |
| | year preceding the first | | +-----------+--------------------------------+------------------------+-------+
| | Monday are considered to be in | | | ``%U`` | Week number of the year | 00, 01, ..., 53 | \(5) |
| | week 0. | | | | (Sunday as the first day of | | |
+-----------+--------------------------------+-------+ | | the week) as a zero padded | | |
| ``%x`` | Locale's appropriate date | | | | decimal number. All days in a | | |
| | representation. | | | | new year preceding the first | | |
+-----------+--------------------------------+-------+ | | Sunday are considered to be in | | |
| ``%X`` | Locale's appropriate time | | | | week 0. | | |
| | representation. | | +-----------+--------------------------------+------------------------+-------+
+-----------+--------------------------------+-------+ | ``%W`` | Week number of the year | 00, 01, ..., 53 | \(5) |
| ``%y`` | Year without century as a | | | | (Monday as the first day of | | |
| | decimal number [00,99]. | | | | the week) as a decimal number. | | |
+-----------+--------------------------------+-------+ | | All days in a new year | | |
| ``%Y`` | Year with century as a decimal | | | | preceding the first Monday | | |
| | number. | | | | are considered to be in | | |
+-----------+--------------------------------+-------+ | | week 0. | | |
| ``%z`` | UTC offset in the form +HHMM | \(5) | +-----------+--------------------------------+------------------------+-------+
| | or -HHMM (empty string if the | | | ``%c`` | Locale's appropriate date and | Mon Aug 1 16:00:00 | |
| | the object is naive). | | | | time representation. | 1988 | |
+-----------+--------------------------------+-------+ +-----------+--------------------------------+------------------------+-------+
| ``%Z`` | Time zone name (empty string | | | ``%x`` | Locale's appropriate date | 08/16/88 | |
| | if the object is naive). | | | | representation. | | |
+-----------+--------------------------------+-------+ +-----------+--------------------------------+------------------------+-------+
| ``%%`` | A literal ``'%'`` character. | | | ``%X`` | Locale's appropriate time | 16:00:00 | |
+-----------+--------------------------------+-------+ | | representation. | | |
+-----------+--------------------------------+------------------------+-------+
| ``%%`` | A literal ``'%'`` character. | % | |
+-----------+--------------------------------+------------------------+-------+
Notes: Notes:
(1) (1)
When used with the :meth:`strptime` method, the ``%f`` directive
accepts from one to six digits and zero pads on the right. ``%f`` is
an extension to the set of format characters in the C standard (but
implemented separately in datetime objects, and therefore always
available).
(2)
When used with the :meth:`strptime` method, the ``%p`` directive only affects When used with the :meth:`strptime` method, the ``%p`` directive only affects
the output hour field if the ``%I`` directive is used to parse the hour. the output hour field if the ``%I`` directive is used to parse the hour.
(3) (2)
The range really is ``0`` to ``61``; according to the Posix standard this The range really is ``0`` to ``61``; according to the Posix standard this
accounts for leap seconds and the (very rare) double leap seconds. accounts for leap seconds and the (very rare) double leap seconds.
The :mod:`time` module may produce and does accept leap seconds since The :mod:`time` module may produce and does accept leap seconds since
@ -1736,13 +1715,36 @@ Notes:
does not accept leap seconds in :meth:`strptime` input nor will it does not accept leap seconds in :meth:`strptime` input nor will it
produce them in :func:`strftime` output. produce them in :func:`strftime` output.
(3)
``%f`` is an extension to the set of format characters in the C standard
(but implemented separately in datetime objects, and therefore always
available). When used with the :meth:`strptime` method, the ``%f``
directive accepts from one to six digits and zero pads on the right.
.. versionadded:: 2.6
(4) (4)
When used with the :meth:`strptime` method, ``%U`` and ``%W`` are only used in For a naive object, the ``%z`` and ``%Z`` format codes are replaced by empty
calculations when the day of the week and the year are specified. strings.
For an aware object:
``%z``
:meth:`utcoffset` is transformed into a 5-character string of the form
+HHMM or -HHMM, where HH is a 2-digit string giving the number of UTC
offset hours, and MM is a 2-digit string giving the number of UTC offset
minutes. For example, if :meth:`utcoffset` returns
``timedelta(hours=-3, minutes=-30)``, ``%z`` is replaced with the string
``'-0330'``.
``%Z``
If :meth:`tzname` returns ``None``, ``%Z`` is replaced by an empty
string. Otherwise ``%Z`` is replaced by the returned value, which must
be a string.
(5) (5)
For example, if :meth:`utcoffset` returns ``timedelta(hours=-3, minutes=-30)``, When used with the :meth:`strptime` method, ``%U`` and ``%W`` are only used
``%z`` is replaced with the string ``'-0330'``. in calculations when the day of the week and the year are specified.
.. rubric:: Footnotes .. rubric:: Footnotes