Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timezone problem with jQuery.timeago plugin

I am using timeago jQuery plugin for my blog, but there seems to be a problem with the timing itself, and I cant put my finger and what the cause of the problem is.

Its currently May 31st, 2011 02:30 local time here (GMT+DST). Now the example date I have used is... May 31st, 2011 02:01. The following tag for this would be

<abbr class="timeago" title="2011-05-31T02:01:44+00:00">May 31st, 2011</abbr>

Yet the jQuery outputs that the date is "32 minutes from now", Its one hour ahead for some reason.

Anyone know what I'm doing wrong here?

like image 610
Ian Avatar asked Dec 27 '22 19:12

Ian


1 Answers

Hazarding a guess here so I might be quite wrong.

The problem is that the test timesatmp you've specified has an offset of 0 so it's the same as UTC - but that's not the same as your time when following DST in the GMT timezone.

GMT is the same as UTC i.e. the offset is 0. However, you mention DST and when following Daylight Savings Time, your clock moves forward by 1 hour and so does your UTC offset. So your time right now, with respect to UTC, is actually UTC + 0100. That's where the extra hour is coming from.

It might help to convert all times to UTC first, after all, that's what it's for, right? 02:30 GMT while following DST is 01:30 UTC. So compared to this, the test timestamp of 02:01 UTC, is about a half hour in the future - hence the 32 minutes from now.

If you were expecting to see 30 minutes ago instead, your test date should have been 2011-05-31T01:01:44+00:00 in order to express a UTC timestamp that was equivalent to the time of 02:01 by your clock display.


The safest solution would be to use UTC timestamps always - this is how SO, Facebook and Twitter date their posts. The UTC timestamp can then be unambiguously interpreted and formatted for each user's timezone.

Your job, then, would be to make sure the timestamp you use is not simply what you see on your clock face, but the UTC equivalent of it - in your current timezone, the UTC time could be 1 hour behind the time displayed. And depending on what you're using to generate your HTML, you should be able to find a function, built-into the language/platform or as a third-party library, that will give you the UTC equivalent of the current time displayed on your clocks in your timezone considering DST.

like image 66
no.good.at.coding Avatar answered Jan 05 '23 17:01

no.good.at.coding