简体   繁体   English

C ++ 64位时间戳到32位time_t

[英]C++ 64bit timestamp to 32bit time_t

I want to convert a 64bit timestamp (in us) to a date string in order to set system date via system command "date". 我想将64位时间戳(以美国为单位)转换为日期字符串,以便通过系统命令“ date”设置系统日期。 I found/implemented this solution: 我发现/实现了此解决方案:

char loc_ach_buf[25];
uint64_t loc_ui64_TimeStamp_us;
time_t loc_t_TimeStamp_s;
...         
loc_t_TimeStamp_s = loc_ui64_TimeStamp_us / 1000000;
strftime(loc_ach_buf, sizeof(loc_ach_buf), "date %Y%m%d%H%M.%S", gmtime(&loc_t_TimeStamp_s));
system(loc_ach_buf);

This works fine, but my problem is, that time_t is a 32bit value on my system and so there will be problems Year 2038 problem . 这很好用,但是我的问题是,time_t是系统上的32bit值,因此2038年将出现问题 Any ideas how to solve this? 任何想法如何解决这个问题? Or are there alternatives to strftime. 还是strftime的替代品。

Why use the system command 'date'? 为什么要使用系统命令“日期”? Use the SetSystemTime API call. 使用SetSystemTime API调用。

I'm not sure which toolset you're working with but the Microsoft compiler supports a function _localtime64_s which will convert a 64-bit time value to the tm structure that you can then use with strftime . 我不确定您使用的是哪个工具集,但是Microsoft编译器支持_localtime64_s函数,该函数会将64位时间值转换为可用于strftime的tm结构。

If your CRT doesn't support 64-bit time functions have a look at this: A version of gmtime() that works around the 2038 bug 如果您的CRT不支持64位时间功能,请查看以下内容: 可以解决2038错误的gmtime()版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM