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

New Button for check if GCS Clock is delayed based on TimeAPi #47

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

DavideLentini
Copy link

Hi , @ntamas should already know the contents of this PR.
For now the icon of the new button is the same as the “Help” button but you can change it to your liking.
Obviously it is just a beta , and can be improved. Let me know if you may be interested in it ! thanks

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@ntamas
Copy link
Member

ntamas commented Nov 11, 2024

@DavideLentini Instead of trying to retrieve the current timezone from the computer and asking the API to provide the current time in that timezone, shouldn't we simply retrieve the current time from the API in UTC and compare that with Date.getTime(), which is always in UTC? Are there any objections to do so?

@DavideLentini
Copy link
Author

DavideLentini commented Nov 11, 2024

@ntamas
Yes , I think so .
But looking at the documentation of the API that I am using in particular :
https://timeapi.io/swagger/index.html
It seems that the json object is only returnable if a timezone or , coordinates , or an ip address is given as input .

@ntamas
Copy link
Member

ntamas commented Nov 11, 2024

True, but you can simply specify UTC as the timezone when calling the API, right?

@DavideLentini
Copy link
Author

@ntamas mmm , sorry I guess I don't really understand ,
Could you give me an example of a query with the API I shared with you?

@ntamas
Copy link
Member

ntamas commented Nov 11, 2024

$ curl -X 'GET' \
  'https://timeapi.io/api/time/current/zone?timeZone=UTC' \
  -H 'accept: application/json'

This gives you the current time in UTC. You can then convert the dateTime field of the response into a UNIX timestamp and compare that with new Date().getTIme().

@DavideLentini
Copy link
Author

DavideLentini commented Nov 11, 2024

@ntamas Ohh i get it , yes.
So basically , I could remove all the code on the timezone , acquire the datetime from the API with this request you wrote me,
convert it to UNIX format , and then compare it with new Date().getTIme()
I think its should return the delay in milliseconds

@DavideLentini
Copy link
Author

DavideLentini commented Nov 12, 2024

@ntamas

Then I was able to convert the API datetime to UNIX timestamps:

const response = await fetch('https://timeapi.io/api/time/current/zone?timeZone=UTC',{ signal: AbortSignal.timeout(5000) });
const data = await response.json();
store.dispatch(showNotification({message:data.dateTime.toString(),semantics:MessageSemantics.SUCCESS}))
const date_time=new Date(data.dateTime).getTime(); //UNIX TIMESTAMP
store.dispatch(showNotification({message:date_time.toString(),semantics:MessageSemantics.SUCCESS}))

However, if I compare it to new Date().getTime()

I get a delay of 1 hour , the reason is probably that I am in UTC+1 .
Maybe I am missing something , but shouldn't we compare our computer time with the corresponding timezone ? to see if there is any delay

If there is already a delay due to UTC , how can we tell if there is an additional delay?

@isti115
Copy link
Member

isti115 commented Nov 12, 2024

@DavideLentini The source of the mismatch you experience is the new Date(data.dateTime) constructor call, which interprets the provided datetime string in your local timezone by default (as far as I know), so it reintroduces the previously eliminated offset. To avoid this behavior, I would recommend adding a "Z" suffix to the datetime string to specify that it is meant to be in UTC.


P.S. I find it strange that a datetime API doesn't provide raw UNIX timestamps in its output. 🤨 Isn't there a query parameter to enable that, or a different service which includes this functionality? 🤔

@DavideLentini
Copy link
Author

DavideLentini commented Nov 13, 2024

@isti115
Okay so it seems to be working!

const date_time=new Date(data.dateTime+“Z”).getTime();
If I try to change time manually , I get a delay. If you would also like to try it to give me confirmation.

I tried to find an API that would return a direct timestamp but with no luck.

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

Successfully merging this pull request may close these issues.

5 participants