timestamp out of range for platform localtime()/gmtime() function

发表于:
来源:
作者:

在python中调用datetime.fromtimestamp()时出现报错:

timestamp out of range for platform localtime()/gmtime() function

经查阅文档得知:

Return the local date and time corresponding to the POSIX timestamp, such as is returned by time.time(). If optional argument tz is None or not specified, the timestamp is converted to the platform’s local date and time, and the returned datetime object is naive.

Else tz must be an instance of a class tzinfo subclass, and the timestamp is converted to tz‘s time zone. In this case the result is equivalent to tz.fromutc(datetime.utcfromtimestamp(timestamp).replace(tzinfo=tz)).

fromtimestamp() may raise ValueError, if the timestamp is out of the range of values supported by the platform C localtime() or gmtime() functions. It’s common for this to be restricted to years in 1970 through 2038. Note that on non-POSIX systems that include leap seconds in their notion of a timestamp, leap seconds are ignored by fromtimestamp(), and then it’s possible to have two timestamps differing by a second that yield identical datetime objects. See also utcfromtimestamp().

也就是说返回local date and time,特别注意是to the posix timestamp.因为posix timestamp是秒级,秒级的时间戳。那么传的参数st,也要是posix timestamp。否则报错。