-
-
Notifications
You must be signed in to change notification settings - Fork 411
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
How to handle differing server and client times #122
Comments
That is realy a problem, the relative date can not solve the realtime render. May be a time differ can solve it, but eveny client has different time differ. |
@jshah4517 if something is wrong with a client's time, it's his problem. The library shows relative diff to the clients time, not to UTC or GMT or something else. As for ux you can show an alert to a client, that his clock is out of date and if the user doesn't fix it, the timers could work wrong. |
Ok, to solve the auto render problem you should return correct time from server, compare it with the client's time and add the difference to relative time which you use instantiating timeago. Will it help?
|
I had a go at a solution which makes the use of a relative date real-time too, the changes are hopefully fairly minimal. It seems to work quite nicely. What do you think? I would be happy to submit a PR if it would seem like a worthwhile addition. Edit: I should just mention, I only aimed to get it working for this case and haven't considered that perhaps people might want to use the relative date option without real-time updates, but perhaps this could be added as a third parameter? |
@jshah4517 thank you for trying solve this issue 😄 I think that function And you are right. If we want this feature then the changes should work both for real-time and for format. @hustcc what do you think? |
@likerRr Yes, you're right, especially if wanting to maintain how it currently works for relative dates. I would probably add a new function I'll wait for @hustcc, but I'm happy to give it a go if it seems like a good suggestion. |
@likerRr @jshah4517 I think add one config namd
The above is what I'm thinking, how do you think? The name of var and API need to confirm. |
This works, but I think the offset could be calculated automatically. Would there ever be a need to have an offset which is different to what the difference between the relative date and client time is? My thought was a boolean for the third param |
It could be automatically calculated, but i think a manual value is more "user" friendly way. Imagine the app that renders how much time until sale starts in some online shop for different countries. Here we need to set offset manually for each of the country. So i think the solution proposed by @hustcc is well enought. |
OK I agree, that solution will be the most flexible. 👍 @hustcc would you like a PR? I might have time later this week. |
Waiting for your PR ^_^ |
Aw, man. I got all excited someone else had solved this and then... no pull request :( |
Please update the readme to include instructions as to how to use this feature. |
No PR still? 😢 |
Is this already available or still an issue? |
I have this working, but it relies on both a server-side passing of data and a modification to how one calls timeAgo (by passing in an element ID, though one could easily modify the below for a class or multiple elements at once: In the Javascript (and this is jQuery specific): function timeAgo(elementID) {
$("#"+elementID).each(function() {
var newDate = new Date();
var newLastUpdateDate = new Date((newDate.getTime() - ($(this).data('now')*1000)) + $(this).data('lastupdate')*1000);
var d = new Date(newLastUpdateDate),
dformat = [(d.getMonth()+1).padLeft(),
d.getDate().padLeft(),
d.getFullYear()].join('-') +' ' +
[d.getHours().padLeft(),
d.getMinutes().padLeft(),
d.getSeconds().padLeft()].join(':')
$(this).attr('datetime',dformat);
timeago().render($(this));
});
} The server passes along a unix timestamp of the object you're looking to track's last updated date ( The element on the client side ends up looking like this: <span class='lastUpdated'>last updated <span id='timeAgo' data-now='"+now+"' data-lastupdate='"+lastUpdateTime+"'></span></span>
<script>timeAgo("timeAgo");</script> |
You can design it into v4.0.0, code is on I think
|
@hustcc or maybe what we can do is provide it a |
Just remember, offset will change. If I activate the app on GMT-5 (NY) and fly to LA my GMT will now be -8. Really what you want to do is to get the offset on init. If you need a parameter, it should be a flag to ignore the offset.
|
What I'd be interested in (and might solve some of the other use cases) would be the functionality to set a flag that indicates "treat all dates as UTC". (To be clear - I'm only concerned about timezone differences, not the out-of-sync client problem) The best pattern here is for the server to provide all times in UTC - and allow the clients to handle the conversion from their local timezone. This solves ppetree's comment (once his device updates its timezone). I'm assuming this already works out-of-the-box when times are expressed with a specific offset or Zulu from the server. However, my native time format is seconds since epoch which javascript interprets as being in the local timezone. |
this will be related to network, I think it will not be considered in this module. sorry~ |
I had the same problem. The solution for me was just adding // Given a client timezone of EDT (GMT -4:00) and local time of 12:00 (noon)
const utcTimeFromServer = '2020-06-01 13:02:15';
timeago.format(utcTimeFromServer); // in 1 hour (bad)
timeago.format(utcTimeFromServer + ' UTC'); // 3 hours ago (good) |
FYI |
What's the best way of handling when the server and client times differ? For example, if the clients clock is 5 mins behind, it would show 'in 5 minutes' instead of 'just now' which looks wrong.
I tried to enter a relative date like
timeago('2016-06-10 12:12:12')
, but after doing so it seems that the real-time updating no longer works. I believe this is because the diffSec function uses that date as the base to compare against rather than new Date().If this isn't possible currently, perhaps if a relative date is entered, it should instead store an offset against
new Date()
and use that when working out the difference?The text was updated successfully, but these errors were encountered: