- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个开始的问题 here .我发现了原因并且没有尝试解决其他问题。
我需要将 CherryPy 设置为在不同平台上可用的最长可能 session 时间。为此 CherryPy 使用 time.gmtime()
。在我的 Windows 64 位上,我可以毫无问题地将 session 超时设置为 future 100 年,但这在 armhf 平台上不起作用。 armhf 允许我将 session 设置为 22 年。
不是 我正在寻找一种根据体系结构动态设置超时的方法。
在 armhf 上,我尝试使用 time.gmtime(sys.maxsize)
返回 2038 年的日期。time.gmtime(sys.maxsize+1)
返回一个 OverflowError: timestamp out of range for platform time_t
错误。所以我想这是可能的最晚日期。
问题是在我的 64 位 Windows 机器上执行相同操作(其中 sys.maxsize = 9223372036854775807
)time.gmtime(sys.maxsize)
返回 OSError:[Errno 22] 无效参数
。有没有办法跨任何架构/平台执行此操作?
编辑:这个问题不仅是由我在 CherryPy 中的代码引起的,其中 session 的超时值对于某些平台/架构(主要是 arm)来说太高了,但在其中一些平台/架构(Arm7)上它也是由 CherryPy 内部引起的。
最佳答案
time.gmtime()
接受一个 float ,因此它的输入受限于 sys.float_info.max
或 int
in the range of C long
(or long long
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()
范围之外的输入:
def binary_search(haystack, needle, lo, hi): # avoid bisect() range limitation
while lo < hi:
mid = (lo + hi) // 2
if haystack[mid] > needle:
hi = mid
elif haystack[mid] < needle:
lo = mid + 1
else:
return mid
return hi
我机器上的结果:
| 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 最大gmtime()
时间戳对应最大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
2147483647
In general, Python time.gmtime()
delegates to the platform C gmtime()
function :
Most 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.
The corresponding function signature in C11 :
struct tm *gmtime(const time_t *timer);
time_t
limits are implementation-defined in C :
The range and precision of times representable in clock_t and time_t are implementation-defined.
time_t
is required to 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
也就是说,原则上 time_t
可以是扩展整数类型或例如 long double。
time_t
is an integer type on POSIX
max time_t
可能大于 sys.maxsize
例如,time_t
在 32 位系统上可能是 64 位类型。
另见:
可以在不知道time_t
限制的情况下找到最大gmtime()
时间戳:
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
对应于相同的最大时间 2147485547-12-31 23:59:59 UTC
。 right/UTC
时间戳更大,因为它包括闰秒(26
as of 2015-07-01
)。
关于python - 为任何架构获得尽可能高的 gmtime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32045725/
我有一个开始的问题 here .我发现了原因并且没有尝试解决其他问题。 我需要将 CherryPy 设置为在不同平台上可用的最长可能 session 时间。为此 CherryPy 使用 time.gm
我正在用 c++/c 检索 GMT 时间。但它返回不正确的时间。例如,现在的时间应该是上午 9 点,但 struct tm 对象返回凌晨 3 点。知道为什么或我做错了什么吗? time_t rawti
我在嵌入式 Linux 上工作,自 1970 年 1 月 1 日起,系统/ Controller 时间以毫秒的形式保持。我正在尝试使用 gmtime 但无法获得准确的结果。将此时间(毫秒)转换为实时小
我在多线程环境中打印事件,控制台是所有线程共享的静态互斥锁。 问题是随机发生的每一百或一千个事件,我得到的时间是本地时间而不是 UTC。 我只在 Linux 机器(使用 g++ 构建)中看到过这个错误
我有这个代码: time_t tt = time(NULL); tm* currentTime = gmtime(&tt); tm* storedTime = gmtime(&m_time); 其中m
我有一个在 Cubox(armhf 平台)上运行的网络服务器(CherryPy),启动服务器后出现以下错误: [14/Aug/2015:09:33:40] HTTP Traceback (most r
是否有一种预定义的方法可以将小时和分钟添加到通过 time.h 获取的 gmtime 中?我想编写一个程序来显示各个国家的时间,那么我必须自己编写逻辑还是有预定义的方法? 最佳答案 如果您有时区名称(
如果我的服务器未配置为使用格林威治标准时间,我不明白为什么 gmdate() 和 date() 返回相同的值。 这是为什么? echo time(); // 13110
将 C 代码迁移到 Swift,对应的等效代码是什么? time(&CurrentTime); tm = gmtime(&CurrentTime); dayOfYear = tm->tm_yday +
根据documentation struct tm *gmtime(const time_t *timer); 应该将 timer 指向的 time_t 转换为分解时间。 现在他们决定让函数接受指向
import time print time.strftime("%a, %d %b %Y %I:%M %p %Z", time.gmtime()) 我住在加利福尼亚。出于某种原因,此代码以 GMT
在谷歌应用引擎 ( http://shell.appspot.com/ ) 上: >>> time.gmtime(1000*365*24*60*60) (2969, 5, 3, 0, 0, 0, 2,
我正在尝试使用 epoch 时间在 STM32L051T6 微 Controller 上设置 RTC 时间。下面是我的代码。 void Rtc_SetTime(uint32_t time_) {
有两个标准的 C 库函数 gmtime() and localtime() .他们都返回这样的结构(一个在 GMT,一个在本地 TZ 和夏令时): struct tm { int tm_sec
我尝试: ts = -216345600000 datetime.datetime.fromtimestamp(ts/1000) ValueError:时间戳超出平台 localtime()/gmti
我有一个在 TZ=UTC 运行的服务器,我有这样的代码: time_t t = time(NULL); struct tm tm; gmtime_r(&t, &tm); 问题是 tm.tm_sec =
如何逆向gmtime(),把时间+日期放在哪里,得到秒数? 我有类似 'Jul 9, 2009 @ 20:02:58 UTC' 之类的字符串,我想返回 epoch 和 2009 年 7 月 9 日之间
我有以下测试代码来查看 gmtime 和 localtime 之间的差异。但他们给了我相同的结果: UTC:2013-05-02T13:59:58 Local:2013-05-02T13:59:58
我有一个 C 程序,我在其中使用遍历目录结构的递归函数。它接受一个 time_t 变量并从中创建一个 gmtime 结构。 递归dir函数来自How to recursively list direc
我一直在搜索谷歌机器,但没有想出任何答案。调用电话时: time.gmtime() 这最终会返回一个时间,正如主题行所说,比我的系统时间早 5 小时。我无法弄清楚发生了什么。 time.tzname
我是一名优秀的程序员,十分优秀!