简体   繁体   中英

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". 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 . Any ideas how to solve this? Or are there alternatives to strftime.

Why use the system command 'date'? Use the SetSystemTime API call.

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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