Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify what time to calculate difference from. #103

Open
dillongrove opened this issue Dec 27, 2012 · 4 comments
Open

Specify what time to calculate difference from. #103

dillongrove opened this issue Dec 27, 2012 · 4 comments

Comments

@dillongrove
Copy link

I noticed that this issue has been brought up before (see here), but I don't think a good justification was ever given, so I'd like to chime in with my own.

I'm writing a simple message/commenting system. Every time a user submits a comment, the comment is stored in the database along with the UTC time that it was posted. Then, when the comments are displayed, I output that time in the DOM, to be used by the timeago plugin.

The issue with this is the "current time" that the timeago plugin uses to compute the difference. It generates this current time by using javascript's new Date() function. This gives us the current time in the user's timezone. The problem with this is of course that the computed difference is only accurate if the user is in UTC time too.

So what I'd like to do is instead compute the difference using the current UTC time as the current time. As far as I can tell there's not a straightforward way to just get the current UTC time in javascript. Instead, we can use the getTimezoneOffset() function. I modified the distance function in the plugin as follows:

function distance(date) {
    var UTC_date_string = new Date().toUTCString(); // get current UTC Time in string form
    var millis_since_epoch = Date.parse(UTC_date_string); // convert it to milliseconds

    /* The date passed in comes to us in the client's timezone because it was created using the javascript
     * new Date() constructor when it was parsed form the DOM.  Unforunately, the date in the DOM is from
     * the server, and already in UTC.  We can account for this discrepancy by subtracting the client's 
     * timezone offset.
     */
    var date_millis = date.getTime(); // convert passed in date to milliseconds (still wrong timezone)
    var minutes_off = date.getTimezoneOffset(); // get the number of minutes this timezone differs from UTC time
    var millis_off = minutes_off*60*1000; // convert from minutes to milliseconds
    var date_millis_since_epoch = date_millis - millis_off; // subtract the discrepancy from the date milliseconds

    return (millis_since_epoch - date_millis_since_epoch); // finally, find the difference
}

Obviously the above is a very verbose implementation, but it communicates the essence of what I'm trying to do.

So, is there a simpler way to do what I described? And if not, any comments on adding this sort of functionality? I'd be happy to initiate a pull request.

@dillongrove
Copy link
Author

Hello?

@jacaetevha
Copy link

Added comment to issue #10.

@mits3
Copy link

mits3 commented Oct 1, 2013

@dillongrove This solved my issue..Great Work.

@dillongrove
Copy link
Author

Glad to hear @mits3 . If I may ask, what is your use case for needing this functionality? Is it similar to mine?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants