【发布时间】:2014-07-17 15:53:32
【问题描述】:
我正在尝试为网页构建一个简单的时钟小部件。我想使用 YQL 来查询使用 Yahoo! 表之一的时间。我找不到允许我根据某个位置查询当前时间的表。
我认为 weather.forecast 表将包含时间,因为人们可以根据当前时间找到特定位置的天气。
谁能引导我到 Yahoo!包含指定位置时间的表?
【问题讨论】:
标签: javascript datetime yql
我正在尝试为网页构建一个简单的时钟小部件。我想使用 YQL 来查询使用 Yahoo! 表之一的时间。我找不到允许我根据某个位置查询当前时间的表。
我认为 weather.forecast 表将包含时间,因为人们可以根据当前时间找到特定位置的天气。
谁能引导我到 Yahoo!包含指定位置时间的表?
【问题讨论】:
标签: javascript datetime yql
没有内置的 YQL 表可以通过 Yahoo 提供时间。
作为替代方案(特别是因为您询问的是网站小部件),您可以在此处查看基于 HTML、JavaScript 和 GeoNames API 的答案:https://stackoverflow.com/a/12817706/9965
【讨论】:
可能最简单的解决方案是使用 YQL 直接获取本地时间,具体取决于您传递查询的时区代码。例如,这是获取新加坡 (SGT) 时间的服务调用:
https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fwww.worldtimeserver.com%2Ftime-zones%2Fsgt%2F%22%20and%20xpath%3D%22%2F%2Fspan%5B%40id%3D'theTime'%5D%22&format=json&callback=
这会产生以下查询:
select * from html
where url="http://www.worldtimeserver.com/time-zones/sgt/"
and xpath="//span[@id='theTime']"
您可以创建一个包装函数,该函数采用时区代码并将“sgt”替换为传递给它的任何参数以获取本地时间。
注意:如果您的网站获得大量流量,则在使用 jQuery 的 $.ajax() 方法请求 JSON 时要小心,因为您可能会超过 YQL 的速率限制,这可能会导致您被禁止。 More info here »
如果您不喜欢抓取网站,那么您可以创建自己的开放数据表并在 <execute> 块中使用 JavaScript 自己进行时间操作。例如,这是一个非常简单的表格,我只是拼凑在一起来演示这一点:
<?xml version="1.0" encoding="UTF-8"?>
<table xmlns="http://query.yahooapis.com/v1/schema/table.xsd">
<meta>
<sampleQuery>select * from {table} where timezone="SGT";</sampleQuery>
<description>Return the current date and time for a given time zone.</description>
</meta>
<bindings>
<select itemPath="" produces="XML">
<inputs>
<key id="timezone" type="xs:string" paramType="variable" required="true"/>
</inputs>
<execute>
<![CDATA[
/*** NOTE: This does NOT handle Daylight Saving Time (DST) ***/
var nOffset;
switch(timezone.toUpperCase()) {
case 'SGT':
nOffset = 8;
break;
/* Add a case for every time zone (see http://www.timeanddate.com/time/zones/) */
}
// Based on http://www.techrepublic.com/article/convert-the-local-time-to-another-time-zone-with-this-javascript/6016329/
function calcTime(offset) {
// Create Date object for current location
var d = new Date();
// Convert to ms, add local time zone offset, and get UTC time
var utc = d.getTime() + (d.getTimezoneOffset()*60000);
// Return date string for supplied time zone
return new Date(utc + (3600000*offset)).toLocaleString('en-US');
}
// This won't work in ECMAScript for XML (E4X):
// var sDateTime = new Date().toLocaleString('en-US', { timeZone:'Asia/Singapore' });
var sDateTime = calcTime(nOffset),
aDateTime = sDateTime.match(/(.+, \d+) (\d.+(?:AM|PM))/),
sDate = aDateTime[1],
sTime = aDateTime[2],
xml =
<datetime>
<date>{sDate}</date>
<time>{sTime}</time>
</datetime>;
// Add attribute
xml.@['timezone'] = timezone;
response.object = xml;
]]>
</execute>
</select>
</bindings>
</table>
如果你走这条路,那么你需要将所有时区映射到它们的 UTC 时差,并添加逻辑来处理夏令时 (DST)。
对于那些刚接触 YQL 的人,以下是如何使用开放数据表:
use "<table_execute_key>" as <table_name>; desc <table_name>.desc <table_name> 替换为以下语句:select * from <table_name> where timezone="SGT"
在您确认一切正常并且您对结果感到满意后,您可以使用 THE REST QUERY 下的 URL 发出 AJAX 请求以检索本地日期和时间(将“时区”值替换为本地time zone code你感兴趣)。
【讨论】: