If I have some int64 millisecondsSinceEpoch which represents time in GMT
and I want to show the relative time that has passed (4 days and 3 hours ago…), how can I do that?
I need a module that knows how to convert my local timezone to GMT or something like that…
If you’re in London (GMT +1) and you post a replay now (18:44 your time) and I read it in Tel-Aviv (GMT +2) 10 minutes after you posted it (19:54 my time)
I will get “Posted by Jules 1 hour and 10 minutes ago”
I need to save the post time after converting to GMT (17:44) and then when I read it in Tel-Aviv, convert it back to my time zone (add 2 hours).
millisecondsSinceEpoch is not independant of the timezone. Specifically since you have DST going on.
(Checked and validate with latest juce tip as 10/26/11)
If you want to have an absolute UTC time in seconds since Epoch, you need to to something like this:
// Do this once, to query the system about the actual diff in time.
time now = time(NULL);
struct tm utc = *gmtime(&now);
struct tm local = *localtime(&now);
int offsetInSecond = (utc.tm_hour - local.tm_hour) * 3600 + (utc.tm_min - local.tm_min) * 60;
// Then any time you need a UTC time in seconds since Epoch, you need to perform
int64 utcTimeInMilli = Time::millisecondsSinceEpoch() - offsetInSecond * 1000;
There is an inconsistency here. Either the millisecondSinceEpoch is in UTC timezone (that is GMT-1) either it’s in GMT. There is an offset of 3600000 ms between both.
I was too lazy to actually check Juce’s Time code, but I wonder the result is from 1/1/1970 0:0:0 GMT and not 0:0:0 UTC since I’ve found multiple time from the past needing to fix the offset when doing calculation with time coming from other software / network.
Clearly, time() C function returns number of seconds since Epoch UTC. If you compare both, you have 3600 seconds offset between both.
The second OP’s question was:
And this is what I’ve written in my post.
BTW, you can check what I’m saying here by simply doing this in the Juce demo: