The :mod:`datetime` module exports the following constants:
-
.. data:: MINYEAR
The smallest year number allowed in a :class:`date` or :class:`datetime` object.
Available Types
---------------
-
.. class:: date
:noindex:
A :class:`timedelta` object represents a duration, the difference between two
dates or times.
-
.. class:: timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
All arguments are optional and default to ``0``. Arguments may be integers
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
-Class attributes are:
+Class attributes are:
.. attribute:: timedelta.min
If an argument outside those ranges is given, :exc:`ValueError` is raised.
-Other constructors, all class methods:
+Other constructors, all class methods:
-.. method:: date.today()
+.. classmethod:: date.today()
Return the current local date. This is equivalent to
``date.fromtimestamp(time.time())``.
-.. method:: date.fromtimestamp(timestamp)
+.. classmethod:: date.fromtimestamp(timestamp)
Return the local date corresponding to the POSIX timestamp, such as is returned
by :func:`time.time`. This may raise :exc:`ValueError`, if the timestamp is out
timestamp, leap seconds are ignored by :meth:`fromtimestamp`.
-.. method:: date.fromordinal(ordinal)
+.. classmethod:: date.fromordinal(ordinal)
Return the date corresponding to the proleptic Gregorian ordinal, where January
1 of year 1 has ordinal 1. :exc:`ValueError` is raised unless ``1 <= ordinal <=
date.max.toordinal()``. For any date *d*, ``date.fromordinal(d.toordinal()) ==
d``.
-Class attributes:
+Class attributes:
.. attribute:: date.min
The smallest possible difference between non-equal date objects,
``timedelta(days=1)``.
-Instance attributes (read-only):
+Instance attributes (read-only):
.. attribute:: date.year
Between 1 and the number of days in the given month of the given year.
+
Supported operations:
+-------------------------------+----------------------------------------------+
Instance methods:
-
.. method:: date.replace(year, month, day)
Return a date with the same value, except for those members given new values by
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
- section :ref:`strftime-behavior`.
+ section :ref:`strftime-strptime-behavior`.
+
Example of counting days to an event::
Constructor:
-
.. class:: datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None)
The year, month and day arguments are required. *tzinfo* may be ``None``, or an
Other constructors, all class methods:
-
-.. method:: datetime.today()
+.. classmethod:: datetime.today()
Return the current local datetime, with :attr:`tzinfo` ``None``. This is
equivalent to ``datetime.fromtimestamp(time.time())``. See also :meth:`now`,
:meth:`fromtimestamp`.
-.. method:: datetime.now(tz=None)
+.. classmethod:: datetime.now(tz=None)
Return the current local date and time. If optional argument *tz* is ``None``
or not specified, this is like :meth:`today`, but, if possible, supplies more
See also :meth:`today`, :meth:`utcnow`.
-.. method:: datetime.utcnow()
+.. classmethod:: datetime.utcnow()
Return the current UTC date and time, with :attr:`tzinfo` ``None``. This is like
:meth:`now`, but returns the current UTC date and time, as a naive
:class:`datetime` object. See also :meth:`now`.
-.. method:: datetime.fromtimestamp(timestamp, tz=None)
+.. classmethod:: datetime.fromtimestamp(timestamp, tz=None)
Return the local date and time corresponding to the POSIX timestamp, such as is
returned by :func:`time.time`. If optional argument *tz* is ``None`` or not
identical :class:`datetime` objects. See also :meth:`utcfromtimestamp`.
-.. method:: datetime.utcfromtimestamp(timestamp)
+.. classmethod:: datetime.utcfromtimestamp(timestamp)
Return the UTC :class:`datetime` corresponding to the POSIX timestamp, with
:attr:`tzinfo` ``None``. This may raise :exc:`ValueError`, if the timestamp is
:meth:`fromtimestamp`.
-.. method:: datetime.fromordinal(ordinal)
+.. classmethod:: datetime.fromordinal(ordinal)
Return the :class:`datetime` corresponding to the proleptic Gregorian ordinal,
where January 1 of year 1 has ordinal 1. :exc:`ValueError` is raised unless ``1
microsecond of the result are all 0, and :attr:`tzinfo` is ``None``.
-.. method:: datetime.combine(date, time)
+.. classmethod:: datetime.combine(date, time)
Return a new :class:`datetime` object whose date members are equal to the given
:class:`date` object's, and whose time and :attr:`tzinfo` members are equal to
object, its time and :attr:`tzinfo` members are ignored.
-.. method:: datetime.strptime(date_string, format)
+.. classmethod:: datetime.strptime(date_string, format)
Return a :class:`datetime` corresponding to *date_string*, parsed according to
*format*. This is equivalent to ``datetime(*(time.strptime(date_string,
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
- time tuple.
+ time tuple. See section :ref:`strftime-strptime-behavior`.
-Class attributes:
+Class attributes:
.. attribute:: datetime.min
The smallest possible difference between non-equal :class:`datetime` objects,
``timedelta(microseconds=1)``.
-Instance attributes (read-only):
+Instance attributes (read-only):
.. attribute:: datetime.year
The object passed as the *tzinfo* argument to the :class:`datetime` constructor,
or ``None`` if none was passed.
+
Supported operations:
+---------------------------------------+-------------------------------+
Instance methods:
-
.. method:: datetime.date()
Return :class:`date` object with same year, month and day.
.. method:: datetime.strftime(format)
Return a string representing the date and time, controlled by an explicit format
- string. See section :ref:`strftime-behavior`.
+ string. See section :ref:`strftime-strptime-behavior`.
+
Examples of working with datetime objects:
A time object represents a (local) time of day, independent of any particular
day, and subject to adjustment via a :class:`tzinfo` object.
-
.. class:: time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None)
All arguments are optional. *tzinfo* may be ``None``, or an instance of a
``timedelta(microseconds=1)``, although note that arithmetic on :class:`time`
objects is not supported.
-Instance attributes (read-only):
+Instance attributes (read-only):
.. attribute:: time.hour
The object passed as the tzinfo argument to the :class:`time` constructor, or
``None`` if none was passed.
+
Supported operations:
* comparison of :class:`time` to :class:`time`, where *a* is considered less
only if, after converting it to minutes and subtracting :meth:`utcoffset` (or
``0`` if that's ``None``), the result is non-zero.
-Instance methods:
+Instance methods:
.. method:: time.replace([hour[, minute[, second[, microsecond[, tzinfo]]]]])
.. method:: time.strftime(format)
Return a string representing the time, controlled by an explicit format string.
- See section :ref:`strftime-behavior`.
+ See section :ref:`strftime-strptime-behavior`.
.. method:: time.utcoffset()
``self.tzinfo.tzname(None)``, or raises an exception if the latter doesn't
return ``None`` or a string object.
+
Example:
>>> from datetime import time, tzinfo
The default implementation of :meth:`tzname` raises :exc:`NotImplementedError`.
+
These methods are called by a :class:`datetime` or :class:`time` object, in
response to their methods of the same names. A :class:`datetime` object passes
itself as the argument, and a :class:`time` object passes ``None`` as the
EST (fixed offset -5 hours), or only EDT (fixed offset -4 hours)).
-.. _strftime-behavior:
+.. _strftime-strptime-behavior:
-:meth:`strftime` Behavior
--------------------------
+:meth:`strftime` and :meth:`strptime` Behavior
+----------------------------------------------
:class:`date`, :class:`datetime`, and :class:`time` objects all support a
``strftime(format)`` method, to create a string representing the time under the
acts like the :mod:`time` module's ``time.strftime(fmt, d.timetuple())``
although not all objects support a :meth:`timetuple` method.
+Conversely, the :meth:`datetime.strptime` class method creates a
+:class:`datetime` object from a string representing a date and time and a
+corresponding format string. ``datetime.strptime(date_string, format)`` is
+equivalent to ``datetime(*(time.strptime(date_string, format)[0:6]))``.
+
For :class:`time` objects, the format codes for year, month, and day should not
be used, as time objects have no such values. If they're used anyway, ``1900``
-is substituted for the year, and ``0`` for the month and day.
+is substituted for the year, and ``1`` for the month and day.
For :class:`date` objects, the format codes for hours, minutes, seconds, and
microseconds should not be used, as :class:`date` objects have no such
Notes:
(1)
- When used with the :func:`strptime` function, the ``%f`` directive
+ 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 :func:`strptime` function, 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.
(3)
accounts for leap seconds and the (very rare) double leap seconds.
The :mod:`time` module may produce and does accept leap seconds since
it is based on the Posix standard, but the :mod:`datetime` module
- does not accept leap seconds in :func:`strptime` input nor will it
+ does not accept leap seconds in :meth:`strptime` input nor will it
produce them in :func:`strftime` output.
(4)
- When used with the :func:`strptime` function, ``%U`` and ``%W`` are only used in
+ When used with the :meth:`strptime` method, ``%U`` and ``%W`` are only used in
calculations when the day of the week and the year are specified.
(5)
'4106250198039490000000000000000000000000000000000000000e-38',
# issue 7632 bug 8: the following produced 10.0
'10.900000000000000012345678912345678912345',
+
+ # two humongous values from issue 7743
+ '116512874940594195638617907092569881519034793229385' #...
+ '228569165191541890846564669771714896916084883987920' #...
+ '473321268100296857636200926065340769682863349205363' #...
+ '349247637660671783209907949273683040397979984107806' #...
+ '461822693332712828397617946036239581632976585100633' #...
+ '520260770761060725403904123144384571612073732754774' #...
+ '588211944406465572591022081973828448927338602556287' #...
+ '851831745419397433012491884869454462440536895047499' #...
+ '436551974649731917170099387762871020403582994193439' #...
+ '761933412166821484015883631622539314203799034497982' #...
+ '130038741741727907429575673302461380386596501187482' #...
+ '006257527709842179336488381672818798450229339123527' #...
+ '858844448336815912020452294624916993546388956561522' #...
+ '161875352572590420823607478788399460162228308693742' #...
+ '05287663441403533948204085390898399055004119873046875e-1075',
+
+ '525440653352955266109661060358202819561258984964913' #...
+ '892256527849758956045218257059713765874251436193619' #...
+ '443248205998870001633865657517447355992225852945912' #...
+ '016668660000210283807209850662224417504752264995360' #...
+ '631512007753855801075373057632157738752800840302596' #...
+ '237050247910530538250008682272783660778181628040733' #...
+ '653121492436408812668023478001208529190359254322340' #...
+ '397575185248844788515410722958784640926528544043090' #...
+ '115352513640884988017342469275006999104519620946430' #...
+ '818767147966495485406577703972687838176778993472989' #...
+ '561959000047036638938396333146685137903018376496408' #...
+ '319705333868476925297317136513970189073693314710318' #...
+ '991252811050501448326875232850600451776091303043715' #...
+ '157191292827614046876950225714743118291034780466325' #...
+ '085141343734564915193426994587206432697337118211527' #...
+ '278968731294639353354774788602467795167875117481660' #...
+ '4738791256853675690543663283782215866825e-1180',
+
# exercise exit conditions in bigcomp comparison loop
'2602129298404963083833853479113577253105939995688e2',
'260212929840496308383385347911357725310593999568896e0',