查找要查询当前时间的YQL表
Looking for YQL table to query for the current time
我正在尝试为网页构建一个简单的时钟小部件。我想用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的速率限制,这可能会让你被禁止。更多信息在这里»
如果你不关心抓取网站,那么你可以创建自己的开放数据表,并在<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时间,并添加逻辑来处理夏令时。
对于那些刚接触YQL的人,下面是如何使用开放数据表:
- 进入YQL编辑器 将上述代码粘贴到编辑器中并单击Save。
- 单击控制台选项卡。
- 单击展开My YQL部分;在"Tables"下,单击刚刚保存的表。
- 在YOUR YQL STATEMENT框中,您应该看到如下内容:
use "<table_execute_key>" as <table_name>; desc <table_name>
。
用下面的语句替换desc <table_name>
:select * from <table_name> where timezone="SGT"
- 选择您想要的响应格式(XML或JSON)并单击Test按钮发出请求。
在您验证了一切正常并且您对结果感到满意之后,您可以使用REST QUERY下的URL来发出AJAX请求以检索本地日期和时间(将'timezone'值替换为您感兴趣的本地时区代码)。
- Java脚本时间添加
- Json数据包含日期和时间格式
- 如何根据时间运行不同的脚本
- 用每小时的差值填充数组/列表-从下拉列表中给定两个时间值
- 在wordpress一定时间后更改自定义字段
- 更改angularjs中的日期-时间格式
- 将当前时间添加到我的页面上的特定部分
- 门户网站:当地时间有多有用
- 显示时间的脚本
- 如何在窗体打开时从javascript倒计时计时器值中节省时间
- 通过JSON&比较时间
- Javascript时间轨迹
- 将日期时间作为 JSON 发送将无法在我的视图中正确显示
- 将日期时间从json转换为可读格式
- 高库存时期时间与时区不匹配
- Jquery如何检查今天的时间大于使用给定时间
- jquerydatetimepicker日期和时间应在框默认值中,而无需单击框
- primefaces日历可以禁用过去的日期和时间吗
- YQL推特时间线超出速率限制.解决方法
- 查找要查询当前时间的YQL表