Skip to content

Commit a847785

Browse files
authored
bpo-43869: Time Epoch is the same on all platforms (GH-30664)
1 parent 7c0914d commit a847785

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

Doc/library/time.rst

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ An explanation of some terminology and conventions is in order.
2121

2222
.. index:: single: epoch
2323

24-
* The :dfn:`epoch` is the point where the time starts, and is platform
25-
dependent. For Unix and Windows, the epoch is January 1, 1970, 00:00:00 (UTC).
26-
To find out what the epoch is on a given platform, look at
27-
``time.gmtime(0)``.
24+
* The :dfn:`epoch` is the point where the time starts, the return value of
25+
``time.gmtime(0)``. It is January 1, 1970, 00:00:00 (UTC) on all platforms.
2826

2927
.. _leap seconds: https://en.wikipedia.org/wiki/Leap_second
3028

@@ -37,7 +35,7 @@ An explanation of some terminology and conventions is in order.
3735

3836
.. index:: single: Year 2038
3937

40-
* The functions in this module may not handle dates and times before the epoch or
38+
* The functions in this module may not handle dates and times before the epoch_ or
4139
far in the future. The cut-off point in the future is determined by the C
4240
library; for 32-bit systems, it is typically in 2038.
4341

@@ -207,7 +205,7 @@ Functions
207205

208206
.. function:: ctime([secs])
209207

210-
Convert a time expressed in seconds since the epoch to a string of a form:
208+
Convert a time expressed in seconds since the epoch_ to a string of a form:
211209
``'Sun Jun 20 23:21:05 1993'`` representing local time. The day field
212210
is two characters long and is space padded if the day is a single digit,
213211
e.g.: ``'Wed Jun 9 04:26:40 1993'``.
@@ -245,7 +243,7 @@ Functions
245243

246244
.. function:: gmtime([secs])
247245

248-
Convert a time expressed in seconds since the epoch to a :class:`struct_time` in
246+
Convert a time expressed in seconds since the epoch_ to a :class:`struct_time` in
249247
UTC in which the dst flag is always zero. If *secs* is not provided or
250248
:const:`None`, the current time as returned by :func:`.time` is used. Fractions
251249
of a second are ignored. See above for a description of the
@@ -601,14 +599,10 @@ Functions
601599
.. function:: time() -> float
602600

603601
Return the time in seconds since the epoch_ as a floating point
604-
number. The specific date of the epoch and the handling of
605-
`leap seconds`_ is platform dependent.
606-
On Windows and most Unix systems, the epoch is January 1, 1970,
607-
00:00:00 (UTC) and leap seconds are not counted towards the time
608-
in seconds since the epoch. This is commonly referred to as
609-
`Unix time <https://en.wikipedia.org/wiki/Unix_time>`_.
610-
To find out what the epoch is on a given platform, look at
611-
``gmtime(0)``.
602+
number. The handling of `leap seconds`_ is platform dependent.
603+
On Windows and most Unix systems, the leap seconds are not counted towards
604+
the time in seconds since the epoch_. This is commonly referred to as `Unix
605+
time <https://en.wikipedia.org/wiki/Unix_time>`_.
612606

613607
Note that even though the time is always returned as a floating point
614608
number, not all systems provide time with a better precision than 1 second.
@@ -629,8 +623,8 @@ Functions
629623

630624
.. function:: time_ns() -> int
631625

632-
Similar to :func:`~time.time` but returns time as an integer number of nanoseconds
633-
since the epoch_.
626+
Similar to :func:`~time.time` but returns time as an integer number of
627+
nanoseconds since the epoch_.
634628

635629
.. versionadded:: 3.7
636630

Lib/test/test_time.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ def test_sleep(self):
159159
self.assertRaises(ValueError, time.sleep, -1)
160160
time.sleep(1.2)
161161

162+
def test_epoch(self):
163+
# bpo-43869: Make sure that Python use the same Epoch on all platforms:
164+
# January 1, 1970, 00:00:00 (UTC).
165+
epoch = time.gmtime(0)
166+
# Only test the date and time, ignore other gmtime() members
167+
self.assertEqual(tuple(epoch)[:6], (1970, 1, 1, 0, 0, 0), epoch)
168+
162169
def test_strftime(self):
163170
tt = time.gmtime(self.t)
164171
for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Python uses the same time Epoch on all platforms. Add an explicit unit test
2+
to ensure that it's the case. Patch by Victor Stinner.

0 commit comments

Comments
 (0)