React Native Get Time In Seconds From API - javascript

I am running a chat application and want to implement correct timestamps on client side, even if the devices' times might be different.
For example, I have iOS device which has current time 14:00 and Android device 14:05 (while actual time might be 14:03). They are both set to manual device times so I would need to calculate their offset from server's time.
Date would not work in this case because it returns client time.
I couldn't find any time APIs which could help me in this case. Is there any other approach?

Related

How NOT to get Timestamp from local machine, but from somewhere else with MomentJS

I am using MomentJs to get the unix timestamp to manage a datetime message property.
Date: moment().valueOf()
the problem is that if someone uses a computer that has the machine's time a little ahead, that time will be recorded and a message that was sent before, will look like it was sent later.
At the moment I'm using a react application that is running on my network "localhost:300" and one of my computers has a different time from the other. So I have to prevent this time difference from being recorded by MomentJS
how can I configure MomentJS to NOT use the local machine's timestamp but from another centralized place?

How can i get accurate date time according to the current time zone in angular

I want to get the exact date and time when user opens a page in his browser. I am trying to achieve this by using 'new Date()'.
console.log(new Date());
Now, the issue is that it is picking user's machine time by default. For example, if I change my machine time manually, the output will be changed accordingly.
There are several ways to achieve this, 2 are listed below:
Use a timeserver, for instance via ntp-time package
https://www.npmjs.com/package/ntp-time
Use a server request (requires that you host your Angular SPA on a server):
Right way to get Web Server time and display it on Web Pages

Record user sessions using javascript to analyize it later using google analytics (offline)

I am wondering if you can give me advice of whether it is possible to capture the user-seesions and interactions (buttons clicked hovered etc).
Later I want to evaluate the results using google analytics.
Only in a very limited fashion.
There is a feature designed for apps that are sometimes (but not always) offline. It's called queue time, and allows you to send and offset between the time the tracked event occured and the time you sent the hit to GA. This works reliably (per documentation) for up to an 4 hour offset - might work for longer offsets (has something to do with the timezone rollover in your account, a Google developer once said on SO that it might work for up to 24 hours), but less reliably. So if you are connected at least ever four hours you could store your hits somewhere and then send then via the measurement protocol with the queue time parameter set. You would however have to have send each request individually (well, almost - you can batch up to 20 requests), there is no real bulk upload for hit data.
Apart from queue time you cannot set time or date information for hits, so there is no way to upload data outside the queue time scope and have it recorded for the time it actually occurred (and again there is not upload for hot data, you would need to send it request by request).

timezones in notification application

I write a notification based web app. I have to set notification times in the client side, send to the server, save to mysql database, continuously check for notifications, when the time is ready, send alert to the client. Simple as pie. Problem is, i'm pretty noob with timezones stuff. My server is in GMT-4, I am currently GMT+2, but in a few month i'll be in GMT +1 because of winter time. I can't get my head around the solution.
So far, i use moment.js on the server side and javascript built in Date object on the client side and i save times in unix timestamp.
My question in short: How to keep track on timezones and stuff, if i don't want any problem if i move my server from gmt-4 to lets say gmt-5 and don't want any problem with seasonal timesone change.
Thanks to spend time on my problem!
Best regards.
The issue of storing timestamps is simple: Store them in UTC.
As for displaying them in client again, Take the device (client) timezone and convert utc to device time zone.

Display accurate local time on web site?

I've been asked to display the 'correct' time on our website which I frankly feel is rather pointless as 'correct' can be interpretted in such a variety of ways.
Our current method definately results in an inaccurate time as it uses a server control rendering JavaScript that runs onload using the datetime from the server as a parameter to create a clock object in JavaScript that finally renders on the page and then starts incrementing the clock. Between the server processing, network latency and client-side performance (there's plenty other stuff running onload) the clock ends up way off the actual server time and who knows compared to the client PC.
So to get the 'correct' time shown I could;
Use local PC time and pass new Date() to the JavaScript clock object. Pros: Should be as close to the PC clock as possible. Cons: Not sure how accurate the PC clock is let alone in which timezone.
Use web service for TCP request to NTP server to update clock on web page. Pros: If local PC also sync'd to NTP will be accurate and best possible match. Cons: Will have to handle all the timezone adjustments relative to our servers. If PC clock is out will still have mismatch.
Do I implement my own web service or use something like; Earth Tools or World Time Web Service (EDIT: link removed - now 404)
Here's a blog post from Jon Galloway on Atomic Clock Web Service which is pretty old and yet ranks high when I google and he doesn't reach a conclusion.
Hoepfully I can win the argument with management why syncing to our server clock (GMT) doesn't makes sense if your not in that timezone and why we even need to match a local PC.
Any angles on this I'm missing?
I needed to show the accurate time to clients in an auction web app. You can send the current server time with the page and have the javascript initialize right away without waiting for the rest of the page to load. So the, you only are dealing with network latency which in the worst case is not likely to be more than a couple of seconds.
After that, you're pretty darn close to accurate time. As long as your Javascript timer code is written properly, you're not going to far out of sync before the next page load. But I have seen a lot of bad JS clock code. (Hint: Date() good, setTimeout() bad.)
If you have an application that users are going to be sitting on for a long time, just refresh your time sync either by reloading the page or Ajax.
I wouldn't worry about time zones, just use UTC time so there is no confusion about what time things will happen.
First, be certain that your client is aware that Windows, Linux, and OSX all have built-in clocks that are almost always visible to the users (or made visible very easily). Also, be certain that your client is aware of physical clocks that are often located near any kiosks that might be setup to hide the built in clock from the operating system.
If you know that, and your client still wants a clock on your website, have your client define "correct" time, then implement the solution that matches their definition (both of your solutions seem like they would take care of the two most-likely definitions).
you can use geo targeting to know the physical location of a website visitor and in your database stored the (GMT - XX:XX) of the zone and then calculate the time based on the location of the request. that is going to save the long trip to any third party web service.
Another way you can implement it is using IP Geolocation. There are services that can tell you where your user is connecting from based on it's ip (usually including their timezone) and combining that information with your server's realtime clock you can show the user it's local time.
It's far from perfect, specially with corporate users that might seem to be connecting from somewhere they are not (I live in Argentina, but my work internet connection is trough my employeer that is an American company, so every website assumes I'm located in the US)
Handle time in UTC.
Have users tell you what zone they want to use.
If your users have persistent profiles, persist the choice.
Always display UTC and Local Time side by side and clearly labelled.
You can also display an arbitrary number of user specified zone clocks. Vista
does this and I remain surprised at how
handy it is.

Categories

Resources