[Python-checkins] cpython (3.3): #8913: add examples and docs for date/time/datetime.__format__. Patch by

ezio.melotti python-checkins at python.org
Thu Apr 4 08:18:39 CEST 2013


http://hg.python.org/cpython/rev/40f582a73901
changeset:   83098:40f582a73901
branch:      3.3
parent:      83096:36130b84cd22
user:        Ezio Melotti <ezio.melotti at gmail.com>
date:        Thu Apr 04 09:16:15 2013 +0300
summary:
  #8913: add examples and docs for date/time/datetime.__format__.  Patch by Heikki Partanen.

files:
  Doc/library/datetime.rst |  27 +++++++++++++++++++++++++++
  Misc/ACKS                |   1 +
  2 files changed, 28 insertions(+), 0 deletions(-)


diff --git a/Doc/library/datetime.rst b/Doc/library/datetime.rst
--- a/Doc/library/datetime.rst
+++ b/Doc/library/datetime.rst
@@ -597,6 +597,13 @@
    section :ref:`strftime-strptime-behavior`.
 
 
+.. method:: date.__format__(format)
+
+   Same as :meth:`.date.strftime`. This makes it possible to specify format
+   string for a :class:`.date` object when using :meth:`str.format`.
+   See section :ref:`strftime-strptime-behavior`.
+
+
 Example of counting days to an event::
 
     >>> import time
@@ -647,6 +654,8 @@
     '11/03/02'
     >>> d.strftime("%A %d. %B %Y")
     'Monday 11. March 2002'
+    >>> 'The {1} is {0:%d}, the {2} is {0:%B}.'.format(d, "day", "month")
+    'The day is 11, the month is March.'
 
 
 .. _datetime-datetime:
@@ -1154,6 +1163,13 @@
    string.  See section :ref:`strftime-strptime-behavior`.
 
 
+.. method:: datetime.__format__(format)
+
+   Same as :meth:`.datetime.strftime`.  This makes it possible to specify format
+   string for a :class:`.datetime` object when using :meth:`str.format`.
+   See section :ref:`strftime-strptime-behavior`.
+
+
 Examples of working with datetime objects:
 
 .. doctest::
@@ -1198,6 +1214,8 @@
     >>> # Formatting datetime
     >>> dt.strftime("%A, %d. %B %Y %I:%M%p")
     'Tuesday, 21. November 2006 04:30PM'
+    >>> 'The {1} is {0:%d}, the {2} is {0:%B}, the {3} is {0:%I:%M%p}.'.format(dt, "day", "month", "time")
+    'The day is 21, the month is November, the time is 04:30PM.'
 
 Using datetime with tzinfo:
 
@@ -1385,6 +1403,13 @@
    See section :ref:`strftime-strptime-behavior`.
 
 
+.. method:: time.__format__(format)
+
+   Same as :meth:`.time.strftime`. This makes it possible to specify format string
+   for a :class:`.time` object when using :meth:`str.format`.
+   See section :ref:`strftime-strptime-behavior`.
+
+
 .. method:: time.utcoffset()
 
    If :attr:`tzinfo` is ``None``, returns ``None``, else returns
@@ -1431,6 +1456,8 @@
     'Europe/Prague'
     >>> t.strftime("%H:%M:%S %Z")
     '12:10:30 Europe/Prague'
+    >>> 'The {} is {:%H:%M}.'.format("time", t)
+    'The time is 12:10.'
 
 
 .. _datetime-tzinfo:
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -907,6 +907,7 @@
 Alexandre Parenteau
 Dan Parisien
 William Park
+Heikki Partanen
 Harri Pasanen
 Gaël Pasgrimaud
 Randy Pausch

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list