Linux 64位unix时间戳转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7914368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
64 bit unix timestamp conversion
提问by Adam Trhon
Is there any C++ implementation of 64 bit unix timestamp conversions for 32 bit systems? I need to convert struct tm
to 64 bit integer and vice versa, including leap years, timezones, UTC. Also need it portable, at least for GNU/Linux and Windows.
32 位系统是否有 64 位 unix 时间戳转换的 C++ 实现?我需要转换struct tm
为 64 位整数,反之亦然,包括闰年、时区、UTC。还需要它可移植,至少对于 GNU/Linux 和 Windows。
采纳答案by Alexei Khlebnikov
You need:
你需要:
typedef long long time64_t;
time64_t mktime64(struct tm *t);
struct tm* localtime64_r(const time64_t* t, struct tm* p);
Originally (in 2011) this answer contained links to 2038bug.com where it was possible to download the small pivotal_gmtime_r
library, containing the mentioned functions. The library has been removed from 2038bug.com back then, the links became broken and were removed from the answer by a moderator. Seems like that pivotal_gmtime_r
code can now be found here:
最初(2011 年)此答案包含指向 2038bug.com 的链接,可以在其中下载pivotal_gmtime_r
包含上述功能的小型库。该库当时已从 2038bug.com 中删除,链接已损坏并被版主从答案中删除。似乎pivotal_gmtime_r
现在可以在这里找到该代码:
https://github.com/franklin373/mortage/tree/master/time_pivotal
https://github.com/franklin373/mortage/tree/master/time_pivotal
Also, I've found another, more recent library, called y2038
, that also implements mktime64
and localtime64_r
:
另外,我发现了另一个更新的库,称为y2038
,它也实现了mktime64
和localtime64_r
:
回答by Basile Starynkevitch
The function converting a struct tm*
to a time_t
is mktime
. You can find many implementations of it, eg. in Glibc and in libvxc's mktime.c
file. You could take the code (assuming it is legal to you, so please respect licenses) and change time_t
to some 64 bits integer like int64_t
.
将 a 转换struct tm*
为 a的函数time_t
是mktime
。您可以找到它的许多实现,例如。在 Glibc 和libvxc 的mktime.c
文件中。你可以把代码(假设它对你合法,所以请尊重许可证)并更改time_t
为一些 64 位整数,如int64_t
.
The functions doing the other conversions from time_t
to struct tm*
are localtime
or gmtime
and you could do likewise.
执行其他从time_t
to转换的函数struct tm*
是localtime
or gmtime
,您也可以这样做。
However, you might have a more fundamental issue: your 32 bits machine running in the year 2040 should have some way of giving you the current time (as the time
system call does) appropriately in the 64 bits variant of time_t
, and that is much harder (it depends upon the kernel and the hardware).
但是,您可能有一个更基本的问题:在 2040 年运行的 32 位机器应该有某种方式time
在 的 64 位变体中适当地为您提供当前时间(如系统调用所做的那样)time_t
,而这要困难得多(这取决于内核和硬件)。
回答by Miguel
You seem to be making the assumption that time_t
is 32-bits on 32-bit systems, and this may or may not be true.
您似乎在假设time_t
32 位系统上是 32 位,这可能是也可能不是。
On Windows, starting with Visual Studio 2005 the size of time_t
is 64-bits, even when you compile for 32-bit Windows.
在 Windows 上,从 Visual Studio 2005 开始,大小time_t
为 64 位,即使您为 32 位 Windows 编译也是如此。
The unfortunate part is that glibc defines it as long int
, which on 32-bit systems is a 32-bit integer. That means that 32-bit Linux and other 32-bit platforms that are based on gcc/glibc (like Cygwin) will not be able to work with 64-bit timestamps.
不幸的是,glibc 将其定义为long int
,它在 32 位系统上是一个 32 位整数。这意味着 32 位 Linux 和其他基于 gcc/glibc(如 Cygwin)的 32 位平台将无法使用 64 位时间戳。
If your application must run on 32-bit glibc, then you should use your own conversion functions, which could be the same functions in the C library recompiled to use 64-bit timestamps.
如果您的应用程序必须在 32 位 glibc 上运行,那么您应该使用自己的转换函数,这些函数可以是重新编译为使用 64 位时间戳的 C 库中的相同函数。
If you need source code with a permissive license (BSD), then you can look at these functions in minix3. Hereis localtime. The source is hyperlinked, so you can find the others easily.
如果您需要具有许可许可证 (BSD) 的源代码,那么您可以在 minix3 中查看这些功能。这里是当地时间。来源是超链接的,因此您可以轻松找到其他来源。
回答by Alex Force
Yeah use stuct tm *_localtime64 ( const __time64_t *timer);
是的使用 stuct tm *_localtime64 ( const __time64_t *timer);
That's if your windows fan.
那是如果你的窗户风扇。