◐ Shell
reader mode source ↗
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
42 changes: 42 additions & 0 deletions Doc/library/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,19 @@ Other constructors, all class methods:
:exc:`ValueError` on :c:func:`localtime` failure.


.. classmethod:: date.fromordinal(ordinal)

Return the date corresponding to the proleptic Gregorian ordinal, where January
Expand Up @@ -793,6 +806,19 @@ Other constructors, all class methods:
:exc:`ValueError` on :c:func:`gmtime` failure.


.. classmethod:: datetime.fromordinal(ordinal)

Return the :class:`.datetime` corresponding to the proleptic Gregorian ordinal,
Expand Down Expand Up @@ -1394,6 +1420,22 @@ day, and subject to adjustment via a :class:`tzinfo` object.
If an argument outside those ranges is given, :exc:`ValueError` is raised. All
default to ``0`` except *tzinfo*, which defaults to :const:`None`.

Class attributes:


51 changes: 50 additions & 1 deletion Lib/_strptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import locale
import calendar
from re import compile as re_compile
from re import IGNORECASE
from re import escape as re_escape
from datetime import (date as datetime_date,
timedelta as datetime_timedelta,
Expand All @@ -27,6 +27,55 @@ def _getlang():
# Figure out what the current language is set to.
return locale.getlocale(locale.LC_TIME)

class LocaleTime(object):
"""Stores and handles locale-specific information related to time.

Expand Down
29 changes: 29 additions & 0 deletions Lib/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,15 @@ def fromordinal(cls, n):
y, m, d = _ord2ymd(n)
return cls(y, m, d)

# Conversions to string

def __repr__(self):
Expand Down Expand Up @@ -1075,6 +1084,16 @@ def __new__(cls, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold
self._fold = fold
return self

# Read-only field accessors
@property
def hour(self):
Expand Down Expand Up @@ -1472,6 +1491,16 @@ def utcfromtimestamp(cls, t):
"""Construct a naive UTC datetime from a POSIX timestamp."""
return cls._fromtimestamp(t, True, None)

@classmethod
def now(cls, tz=None):
"Construct a datetime from time.time() and optional time zone info."
Expand Down
61 changes: 61 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,19 @@ def test_fromtimestamp(self):
self.assertEqual(d.month, month)
self.assertEqual(d.day, day)

def test_insane_fromtimestamp(self):
# It's possible that some platform maps time_t to double,
# and that this test will fail there. This test should
Expand Down @@ -1976,6 +1989,18 @@ def test_utcfromtimestamp(self):
got = self.theclass.utcfromtimestamp(ts)
self.verify_field_equality(expected, got)

# Run with US-style DST rules: DST begins 2 a.m. on second Sunday in
# March (M3.2.0) and ends 2 a.m. on first Sunday in November (M11.1.0).
@support.run_with_tz('EST+05EDT,M3.2.0,M11.1.0')
Expand Down Expand Up @@ -2517,6 +2542,42 @@ def test_isoformat(self):
self.assertEqual(t.isoformat(timespec='microseconds'), "12:34:56.000000")
self.assertEqual(t.isoformat(timespec='auto'), "12:34:56")

def test_1653736(self):
# verify it doesn't accept extra keyword arguments
t = self.theclass(second=1)
Expand Down
76 changes: 76 additions & 0 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
@@ -2607,6 +2607,25 @@ date_fromtimestamp(PyObject *cls, PyObject *args)
return result;
}

/* Return new date from proleptic Gregorian ordinal. Raises ValueError if
* the ordinal is out of range.
*/
Expand Down Expand Up @@ -2920,6 +2939,11 @@ static PyMethodDef date_methods[] = {
PyDoc_STR("timestamp -> local date from a POSIX timestamp (like "
"time.time()).")},

{"fromordinal", (PyCFunction)date_fromordinal, METH_VARARGS |
METH_CLASS,
PyDoc_STR("int -> date corresponding to a proleptic Gregorian "
Expand Down Expand Up @@ -3711,6 +3735,26 @@ time_str(PyDateTime_Time *self)
return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, NULL);
}

static PyObject *
time_isoformat(PyDateTime_Time *self, PyObject *args, PyObject *kw)
{
Expand Down Expand Up @@ -4018,6 +4062,13 @@ time_reduce(PyDateTime_Time *self, PyObject *arg)

static PyMethodDef time_methods[] = {

{"isoformat", (PyCFunction)time_isoformat, METH_VARARGS | METH_KEYWORDS,
PyDoc_STR("Return string in ISO 8601 format, [HH[:MM[:SS[.mmm[uuu]]]]]"
"[+HH:MM].\n\n"
Expand Down Expand Up @@ -4726,6 +4777,25 @@ datetime_str(PyDateTime_DateTime *self)
return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, "s", " ");
}

static PyObject *
datetime_isoformat(PyDateTime_DateTime *self, PyObject *args, PyObject *kw)
{
Expand Down Expand Up @@ -5506,6 +5576,12 @@ static PyMethodDef datetime_methods[] = {
METH_VARARGS | METH_KEYWORDS | METH_CLASS,
PyDoc_STR("timestamp[, tz] -> tz's local time from POSIX timestamp.")},

{"utcfromtimestamp", (PyCFunction)datetime_utcfromtimestamp,
METH_VARARGS | METH_CLASS,
PyDoc_STR("Construct a naive UTC datetime from a POSIX timestamp.")},
Expand Down
Toggle all file notes Toggle all file annotations