获得当前的GMT世界时间 [英] Getting the current GMT world time

查看:230
本文介绍了获得当前的GMT世界时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何获得当前时间? (在JavaScript中)

How can i get the current time? (in JavaScript)

不是你电脑的时间:

now = new Date;
now_string = addZero(now.getHours()) + ":" + addZero(now.getMinutes()) + ":" + addZero(now.getSeconds());

但真正准确的世界时间?

But the real accurate world time?

我是否需要连接到服务器(很可能是,哪一个?我如何从中检索时间?)

Do i need to connect to to a server (most likely yes, which one? and how can i retrieve time from it?)

我从谷歌搜索的所有搜索返回(新日期).getHours()

All the searches I do from google return the (new Date).getHours().

编辑:

如果用户的计算机时间错误,我想避免显示错误的时间。

I want to avoid showing an incorrect time if the user has a wrong time in his computer.

推荐答案

首先,要获得准确的GMT时间,您需要一个您信任的来源。这意味着某处服务器。 Javascript通常只能进行HTTP调用,而且只能访问托管相关页面的服务器(相同的源策略)。因此,该服务器必须是您的GMT时间来源。

First, to get the accurate GMT time you need a source that you trust. This means some server somewhere. Javascript can generally only make HTTP calls, and only to the server hosting the page in question (same origin policy). Thus that server has to be your source for GMT time.

我会将您的网络服务器配置为使用NTP将其时钟与GMT同步,并让网络服务器告诉脚本什么通过向页面写入变量,它是时间。或者,当您需要知道时间时,将make和XmlHttpRequest返回给服务器。缺点是由于所涉及的延迟,这将是不准确的:服务器确定时间,将其写入响应,响应通过网络传播,并且只要客户端的cpu给它一个时间片,javascript就会执行,等等。慢速链接,如果页面很大,你可以预期延迟几秒钟。您可以通过确定用户时钟与GMT的距离来节省一些时间,并且只需通过该偏移调整所有时间计算。当然,如果用户的时钟速度很慢或很快(不仅仅是迟到或早期),或者如果用户在PC上更改时间,那么您的偏移量就会被烧断。

I would configure your webserver to use NTP to synchronize its clock with GMT, and have the webserver tell the script what time it is, by writing a variable to the page. Or else make and XmlHttpRequest back to the server when you need to know the time. The downside is that this will be inaccurate due to the latency involved: the server determines the time, writes it to the response, the response travels over the network, and the javascript executes whenever the client's cpu gives it a timeslice, etc. On a slow link you can expect seconds of delay if the page is big. You might be able to save some time by determining how far off from GMT the user's clock is, and just adjusting all the time calculations by that offset. Of course if the user's clock is slow or fast (not just late or early) or if the user changes the time on their PC then your offset is blown.

同时保持请记住,客户可以更改数据,因此不要相信他们发送给您的任何时间戳。

Also keep in mind that the client can change the data so don't trust any timestamps they send you.

编辑
JimmyP的答案非常简单易用:使用Javascript添加一个< script> 元素,它调用一个url,例如 http://json-time.appspot.com/time.json?tz=GMT 。这比自己更容易,因为json-time.appspot.com服务器作为GMT时间的来源,并以允许您解决同源策略的方式提供此数据。对于简单的网站我会建议。但是它有一个主要缺点:json-time.appspot.com站点可以在用户的​​页面上执行任意代码。这意味着,如果该网站的运营商想要分析您的用户或劫持他们的数据,他们可以做到这一点。即使您信任运营商,您也需要相信他们没有被黑客入侵或受到攻击。对于商业网站或任何具有高可靠性问题的网站,我建议您自己托管时间解决方案。

Edit: JimmyP's answer is very simple and easy to use: use Javascript to add a <script> element which calls a url such as http://json-time.appspot.com/time.json?tz=GMT. This is easier than doing this yourself because the json-time.appspot.com server works as a source of GMT time, and provides this data in a way that lets you work around the same-origin policy. I would recommend that for simple sites. However it has one major drawback: the json-time.appspot.com site can execute arbitrary code on your user's pages. This means that if the operators of that site want to profile your users, or hijack their data, they can do that trivially. Even if you trust the operators you need to also trust that they have not been hacked or compromised. For a business site or any site with high reliability concerns I'd recommend hosting the time solution yourself.

编辑2
JimmyP的回答有一条评论表明json-time app在它可以支持的请求数量方面有一些限制。这意味着如果您需要可靠性,您应该自己托管时间服务器。但是,在服务器上添加一个响应相同格式数据的页面应该很容易。基本上你的服务器需要查询,例如

Edit 2: JimmyP's answer has a comment which suggests that the json-time app has some limitations in terms of the number of requests it can support. This means if you need reliability you should host the time server yourself. However, it should be easy enough to add a page on your server which responds with the same format of data. Basically your server takes a query such as

http://json-time.appspot.com/time.json?tz=America/Chicago&callback=foo

并返回一个字符串,例如

and returns a string such as

foo({
 "tz": "America\/Chicago", 
 "hour": 15, 
 "datetime": "Thu, 09 Apr 2009 15:07:01 -0500", 
 "second": 1, 
 "error": false, 
 "minute": 7
})

注意 foo()它包装了JSON对象;这对应于查询中的callback = foo。这意味着当脚本加载到页面中时,它将调用你的foo函数,它可以随时随地执行任何操作。此示例的服务器端编程是一个单独的问题。

Note the foo() which wraps the JSON object; this corresponds to the callback=foo in the query. This means when the script is loaded into the page it will call your foo function, which can do whatever it wants with the time. Server-side programming for this example is a separate question.

这篇关于获得当前的GMT世界时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆