1

I am trying to build a simple clock widget for a webpage. I want to use YQL to query for the time using one of Yahoo!'s tables. I cannot find a table that allows me to query for the current time based off of a location.

I was thinking that the weather.forecast table would contain the time due to the fact that people can find the weather for specific locations based off of the current time.

Can someone direct me to a Yahoo! table that contains the time for a specified location?

Azor Ahai
  • 13
  • 3

2 Answers2

0

There is no built-in YQL table to provide the time via Yahoo.

As an alternative (especially since you're asking about a website widget), you could check out the answer here which is based on HTML, JavaScript, and the GeoNames API: https://stackoverflow.com/a/12817706/9965

Community
  • 1
  • 1
BrianC
  • 10,591
  • 2
  • 30
  • 50
0

Probably the simplest solution would be to use YQL to directly get the local time depending on what time zone code you pass the query. For example, here's the service call to get the time for Singapore (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=

This makes the following query:

select * from html
where url="http://www.worldtimeserver.com/time-zones/sgt/"
and xpath="//span[@id='theTime']"

You can make a wrapper function that takes a time zone code and replaces "sgt" with whatever argument that is passed to it to get the local time.

NOTE: Be careful when requesting the JSON using jQuery's $.ajax() method if your site gets tons of traffic because you may exceed YQL's rate limit, which may get you banned. More info here ยป

If you don't care for scraping websites, then you can create your own Open Data Table and use JavaScript in an <execute> block to do the time manipulations yourself. For example, here's a very simple table I just threw together to demonstrate this:

<?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>

If you go this route, then you'll need to map out all time zones to their UTC offets, and also add logic to handle Daylight Saving Time (DST).


For those new to YQL, here's how to use an Open Data Table:

  1. Go to the YQL Editor.
  2. Paste the code above into the editor and click Save.
  3. Click on the Console tab.
  4. Click to expand the My YQL section; under Tables, click the table you just saved.
  5. In the YOUR YQL STATEMENT box, you should see something like this:
    use "<table_execute_key>" as <table_name>; desc <table_name>.
    Replace desc <table_name> with this statement:
    select * from <table_name> where timezone="SGT"
  6. Choose the response format you want (XML or JSON) and click on the Test button to make the request.

After you have verified that everything works and you're happy with the results, you can use the URL under THE REST QUERY to make an AJAX request to retrieve the local date and time (replace the 'timezone' value with the local time zone code you're interested in).

thdoan
  • 18,421
  • 1
  • 62
  • 57