summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEzio Melotti <ezio.melotti@gmail.com>2023-04-08 08:46:47 (GMT)
committerGitHub <noreply@github.com>2023-04-08 08:46:47 (GMT)
commit3310b94d3db2f477cf2b8789c30ac0f22f82d2dd (patch)
tree9d7889f07a04f3e011f1a68fca983a62770096c3
parent1e9dfdacefa2c8c27762ba6491b0f570147ee355 (diff)
downloadcpython-3310b94d3db2f477cf2b8789c30ac0f22f82d2dd.zip
cpython-3310b94d3db2f477cf2b8789c30ac0f22f82d2dd.tar.gz
cpython-3310b94d3db2f477cf2b8789c30ac0f22f82d2dd.tar.bz2
gh-100574: add examples/links to the `strptime`/`strftime` docs (#100575)
-rw-r--r--Doc/library/datetime.rst47
1 files changed, 26 insertions, 21 deletions
diff --git a/Doc/library/datetime.rst b/Doc/library/datetime.rst
index 50827b2..761f5f0 100644
--- a/Doc/library/datetime.rst
+++ b/Doc/library/datetime.rst
@@ -737,18 +737,16 @@ Instance methods:
.. method:: date.strftime(format)
Return a string representing the date, controlled by an explicit format string.
- Format codes referring to hours, minutes or seconds will see 0 values. For a
- complete list of formatting directives, see
- :ref:`strftime-strptime-behavior`.
+ Format codes referring to hours, minutes or seconds will see 0 values.
+ See also :ref:`strftime-strptime-behavior` and :meth:`date.isoformat`.
.. method:: date.__format__(format)
Same as :meth:`.date.strftime`. This makes it possible to specify a format
string for a :class:`.date` object in :ref:`formatted string
- literals <f-strings>` and when using :meth:`str.format`. For a
- complete list of formatting directives, see
- :ref:`strftime-strptime-behavior`.
+ literals <f-strings>` and when using :meth:`str.format`.
+ See also :ref:`strftime-strptime-behavior` and :meth:`date.isoformat`.
Examples of Usage: :class:`date`
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -1051,8 +1049,8 @@ Other constructors, all class methods:
: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
- time tuple. For a complete list of formatting directives, see
- :ref:`strftime-strptime-behavior`.
+ time tuple. See also :ref:`strftime-strptime-behavior` and
+ :meth:`datetime.fromisoformat`.
@@ -1510,20 +1508,21 @@ Instance methods:
(which :func:`time.ctime` invokes, but which
:meth:`datetime.ctime` does not invoke) conforms to the C standard.
+
.. method:: datetime.strftime(format)
- Return a string representing the date and time, controlled by an explicit format
- string. For a complete list of formatting directives, see
- :ref:`strftime-strptime-behavior`.
+ Return a string representing the date and time,
+ controlled by an explicit format string.
+ See also :ref:`strftime-strptime-behavior` and :meth:`datetime.isoformat`.
.. method:: datetime.__format__(format)
Same as :meth:`.datetime.strftime`. This makes it possible to specify a format
string for a :class:`.datetime` object in :ref:`formatted string
- literals <f-strings>` and when using :meth:`str.format`. For a
- complete list of formatting directives, see
- :ref:`strftime-strptime-behavior`.
+ literals <f-strings>` and when using :meth:`str.format`.
+ See also :ref:`strftime-strptime-behavior` and :meth:`datetime.isoformat`.
+
Examples of Usage: :class:`.datetime`
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -1868,17 +1867,15 @@ Instance methods:
.. method:: time.strftime(format)
Return a string representing the time, controlled by an explicit format
- string. For a complete list of formatting directives, see
- :ref:`strftime-strptime-behavior`.
+ string. See also :ref:`strftime-strptime-behavior` and :meth:`time.isoformat`.
.. method:: time.__format__(format)
- Same as :meth:`.time.strftime`. This makes it possible to specify a format string
- for a :class:`.time` object in :ref:`formatted string
- literals <f-strings>` and when using :meth:`str.format`. For a
- complete list of formatting directives, see
- :ref:`strftime-strptime-behavior`.
+ Same as :meth:`.time.strftime`. This makes it possible to specify
+ a format string for a :class:`.time` object in :ref:`formatted string
+ literals <f-strings>` and when using :meth:`str.format`.
+ See also :ref:`strftime-strptime-behavior` and :meth:`time.isoformat`.
.. method:: time.utcoffset()
@@ -2320,6 +2317,14 @@ versus :meth:`strptime`:
:meth:`strftime` and :meth:`strptime` Format Codes
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+These methods accept format codes that can be used to parse and format dates::
+
+ >>> datetime.strptime('31/01/22 23:59:59.999999',
+ ... '%d/%m/%y %H:%M:%S.%f')
+ datetime.datetime(2022, 1, 31, 23, 59, 59, 999999)
+ >>> _.strftime('%a %d %b %Y, %I:%M%p')
+ 'Mon 31 Jan 2022, 11:59PM'
+
The following is a list of all the format codes that the 1989 C standard
requires, and these work on all platforms with a standard C implementation.