Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

64-bit Unix timestamp conversion

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, time zones, UTC. Also need it portable, at least for GNU/Linux and Windows.

like image 632
Adam Trhon Avatar asked Oct 27 '11 10:10

Adam Trhon


People also ask

How do I convert timestamp to time in Unix?

The getTime method returns the number of milliseconds since the Unix Epoch (1st of January, 1970 00:00:00). To get a Unix timestamp, we have to divide the result from calling the getTime() method by 1000 to convert the milliseconds to seconds. What is this?

How many bits is a Unix timestamp?

UNIX data represents different points in time as signed integers, traditionally of 32 bits, by encoding the UNIX timestamp. Because it uses 32 bits, UNIX time can only cover approximately 136 years in total.

How do you convert UTC to time in Unix?

=(A1 / 86400) + 25569 Format the result cell for date/time, the result will be in GMT time (A1 is the cell with the epoch number). For other time zones: =((A1 +/- time zone adjustment) / 86400) + 25569.

Why is 1970 the Unix epoch?

January 1st, 1970 at 00:00:00 UTC is referred to as the Unix epoch. Early Unix engineers picked that date arbitrarily because they needed to set a uniform date for the start of time, and New Year's Day, 1970, seemed most convenient.


2 Answers

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:

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:

https://github.com/evalEmpire/y2038

like image 90
Alexei Khlebnikov Avatar answered Sep 18 '22 14:09

Alexei Khlebnikov


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.

The functions doing the other conversions from time_t to struct tm* are localtime or gmtime and you could do likewise.

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).

like image 38
Basile Starynkevitch Avatar answered Sep 20 '22 14:09

Basile Starynkevitch