Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Doc/library/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,15 @@ Instance methods:
``date.fromordinal(d.toordinal()) == d``.


.. method:: date.timestamp()

Return POSIX timestamp corresponding to the :class:`.date`
instance. The return value is a :class:`float` similar to that
returned by :func:`time.time`.

.. versionadded:: next


.. method:: date.weekday()

Return the day of the week as an integer, where Monday is 0 and Sunday is 6.
Expand Down
5 changes: 5 additions & 0 deletions Lib/_pydatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,11 @@ def weekday(self):
"Return day of the week, where Monday == 0 ... Sunday == 6."
return (self.toordinal() + 6) % 7

def timestamp(self):
"""Return POSIX timestamp as float (midnight UTC)."""
epoch = date(1970, 1, 1)
return float((self.toordinal() - epoch.toordinal()) * 86400)

# Day-of-the-week and week-of-the-year, according to ISO

def isoweekday(self):
Expand Down
20 changes: 20 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,26 @@ def test_strptime_leap_year(self):
date.strptime('20-03-14', '%y-%m-%d')
date.strptime('02-29,2024', '%m-%d,%Y')

@support.run_with_tz('UTC+0')
def test_timestamp(self):
t = date(1970, 1, 1)
self.assertEqual(t.timestamp(), 0.0)

t = date(2000, 1, 1)
self.assertEqual(t.timestamp(),946684800.0)

t = date(2025, 3, 14)
self.assertEqual(t, date.fromtimestamp(t.timestamp()))

for t in [date(2, 1, 1), date(9998, 12, 12)]:
try:
s = t.timestamp()
except OverflowError:
pass
else:
self.assertEqual(date.fromtimestamp(s), t)


class SubclassDate(date):
sub_var = 1

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added :func:`datetime.date.timestamp` to :class:`datetime.date`.
17 changes: 17 additions & 0 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3868,6 +3868,20 @@ date_weekday(PyObject *self, PyObject *Py_UNUSED(dummy))
return PyLong_FromLong(dow);
}

static PyObject *
date_timestamp(PyObject *self, PyObject *Py_UNUSED(ignored))
{
PyObject *ordinal_obj = date_toordinal(self, NULL);
if (!ordinal_obj) {
return NULL;
}
int days_since_epoch = PyLong_AsLong(ordinal_obj) - 719163;
Py_DECREF(ordinal_obj);
double timestamp = days_since_epoch * 86400.0;

return PyFloat_FromDouble(timestamp);
}

/* Pickle support, a simple use of __reduce__. */

/* __getstate__ isn't exposed */
Expand Down Expand Up @@ -3946,6 +3960,9 @@ static PyMethodDef date_methods[] = {
PyDoc_STR("Return the day of the week represented by the date.\n"
"Monday == 0 ... Sunday == 6")},

{"timestamp", (PyCFunction)date_timestamp, METH_NOARGS,
PyDoc_STR("Return POSIX timestamp as float (midnight UTC).")},

DATETIME_DATE_REPLACE_METHODDEF

{"__replace__", _PyCFunction_CAST(datetime_date_replace), METH_FASTCALL | METH_KEYWORDS,
Expand Down
Loading