time.gmtime() takes a float, so its input is limited to sys.float_info.max or int in the C long range (or long long if available) .
To find the “highest possible date,” we could use a binary search, for example in @BlackJack's answer :
#!/usr/bin/env python import ctypes import sys import time MAX_TIME = max(int(sys.float_info.max), 2**(8*ctypes.sizeof(getattr(ctypes, 'c_longlong', ctypes.c_long)))) BOUNDARY = 0.5 assert False < BOUNDARY < True
where binary_search() is a custom function that is used to accept input outside the range of bisect.bisect() :
def binary_search(haystack, needle, lo, hi):
Results on my machine:
| Python version | max gmtime timestamp | |----------------------+----------------------| | Python 2.7.9 | 67768036191676795 | | Python 3.4.3 | 67768036191676799 | | Pypy (Python 2.7.9) | 67768036191676795 | | Pypy3 (Python 3.2) | 67768036191676795 | | Jython 2.7.0 | 9223372036854777 |
67768036191676799 Python 3 max gmtime() timestamp corresponds to the maximum 32-bit int year:
>>> import time; time.gmtime(67768036191676799) time.struct_time(tm_year=2147485547, tm_mon=12, tm_mday=31, tm_hour=23, tm_min=59, tm_sec=59, tm_wday=2, tm_yday=365, tm_isdst=0) >>> 2147485547-1900 2147483647 >>> 2**31-1 2147483647
In general, Python time.gmtime() delegates to the C platform gmtime() function :
Most of the functions defined in this platform C platform are platform calling functions with the same name. It can sometimes be helpful to consult platform documentation, since the semantics of these functions vary between platforms.
Corresponding function signature in C11 :
struct tm *gmtime(const time_t *timer);
time_t limits are defined in C :
The range and accuracy of the time displayed in clock_t and time_t are determined by the implementation.
time_t must be a real type on c11 :
real types integer types char sίgned integer types standard sίgned integer types signed char, short int, int, long int, long long int extended sίgned integer types unsίgned integer types standard unsίgned integer types _Bool, unsigned char, unsigned short int, unsigned int, unsigned long int, unsigned long long int extended unsίgned integer types enumeration types real floating types float, double, long double
ie, in principle time_t can be an extended integer type or, for example, a long double.
time_t - integer type on POSIX
max time_t may be greater than sys.maxsize , for example, time_t may be a 64-bit type in a 32-bit system.
See also:
- Maximum values for time_t (struct timespec)
- What is the greatest useful value of time_t?
You can find the timestamp max gmtime() without knowing the time_t limit:
def find_max_gmtime_timestamp(): ts = 1 overflow = GmtimeOverflowTable() assert overflow[float('+inf')] and not overflow[ts] while not overflow[ts]: ts *= 2 ts = binary_search(overflow, BOUNDARY, ts//2, ts) max_ts = ts - 1 assert overflow[max_ts+1] and not overflow[max_ts] return max_ts
The result is the same.
If TZ=right/UTC , then the result 67768036191676825 corresponds to the same maximum time 2147485547-12-31 23:59:59 UTC . right/UTC timestamp is longer since it includes seconds of jump ( 26 from 2015-07-01 ).