RelativeTime & GMT

Hi.

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…

Is there a standard library that I can use?

Thanks

I don’t understand… If you’ve got two absolute times, you can subtract them to get a RelativeTime and then use that to get the description, right?

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

I think, maybe I got it wrong…

Eh? If you have a millisecondsSinceEpoch time, that’s independent of the timezone.

(“I’m an old man and have known a great many troubles, but most of them never happened” (Mark Twain) ).replace(“troubles”, “bugs”);

Thanks Jules

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;

Eh?

The definition of the “epoch” is that it’s a fixed time, in UTC. So the number of milliseconds since that time has nothing to do with time zones.

The time-zone only enters the equation if you try to convert these numbers to a string form.

And the OP was asking about a time difference, between two “milliseconds since the epoch” numbers, so that has even less to do with time-zones!

Well, I disagree with you. The OP said:

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:

AlertWindow::showMessageBox(AlertWindow::WarningIcon, "Current time", String::empty << (int)(Time::getCurrentTime().millisecondsSinceEpoch() / 1000));

Then go to any website displaying number of second since Epoch like:

To check that the returned value of Time::millis is 3600 seconds off.

Edit: You’re right about Epoch being independant of time zone. I’m mixing the start time offset used in Time from Epoch definition.