python中gmtime的hour错误_为任何架构师获取最高的gmtime

time.gmtime()接受一个float,因此它的输入受到sys.float_info.max或intin the range of C ^{} (or ^{} if available)的限制。在

为了找到“最高日期”,我们可以使用类似于

@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 # necessary for the binary search to work

class GmtimeOverflowTable:

def __getitem__(self, timestamp):

assert timestamp >= 0

try:

time.gmtime(timestamp)

except (OSError, OverflowError, ValueError): # ValueError for Python <3.3

return True # overflow

return False

def find_max_gmtime_timestamp():

overflow = GmtimeOverflowTable()

assert overflow[float('+inf')] and not overflow[0]

if overflow[MAX_TIME]:

ts = binary_search(overflow, BOUNDARY, 0, MAX_TIME)

assert overflow[ts] and not overflow[ts - 1]

return ts - 1

raise OverflowError("Max gmtime timestamp is larger than " + str(MAX_TIME))

print(find_max_gmtime_timestamp())

其中binary_search()是一个自定义函数,用于接受bisect.bisect()范围之外的输入:

^{pr2}$

我的机器上的结果:| 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 |

67768036191676799Python 3 maxgmtime()时间戳对应于max 32位int年:>>> 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

2147483647Most of the functions defined in this module call platform C library

functions with the same name. It may sometimes be helpful to consult

the platform documentation, because the semantics of these functions

varies among platforms.struct tm *gmtime(const time_t *timer);The range and precision of times representable in clock_t and time_t

are implementation-defined.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

也就是说,原则上time_t可以是扩展整数类型或例如,long double。在

maxtime_t可以大于sys.maxsize,例如,time_t可以是32位系统上的64位类型。在

另请参见:

可以在不知道gmtime()限制的情况下找到maxgmtime()时间戳: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

结果是一样的。在

如果TZ=right/UTC,那么结果是67768036191676825,它对应于相同的max time2147485547-12-31 23:59:59 UTC。right/UTC时间戳较大,因为它包含闰秒(26从2015-07-01开始)。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值