Saturday, September 15, 2007

What's the Time around the world?


By scraping TimeAndDate website I could build the World Clock function. If you look on the page HTML source you can find the following for each place:


<a href="city.html?n=110">Jerusalem</a> *</td><td class="r">Sat 5:44 PM</td>

 
Based on that line we can build a regular expression to match each place and its local time (yellowed text).

 

Regex break through:

'href="city.html\?n=\d+">(?<place>[^/]*)</a>\s?\*?</td><td class="r">(?<time>[^/]*)</td>'

Starts with href="city.html
Followed by ?n=  (The ? sign is a meta character so escape it, \?n=)
Followed by one or more digits \d+"> with a quote and a closing tag bracket (110)
Followed by (?<place>[^/]*) (equals to Jerusalem, match and capture)
Followed by closing a tag </a>, and (?:\s\*)? (optional space and *, don't insert into $matches)
Followed by </td><td class="r">
Followed by (?<time>[^/]*) (equals to Sat 5:44 PM, match and capture)
Followed by </td>

Captures legend:
(exp)               Match exp and capture it in an automatically numbered group
(?<name>exp)  Match exp and capture it in a group named name
(?:exp)            Match exp, but do not capture it

 

#####################################

function Get-WorldClock{

    param (
        [string]$continent="",
        [string]$filter="*"
    )
   
$baseurl = http://www.timeanddate.com/worldclock/

# define values and shortcuts for -continent parameter

switch($continent){
    "as"                {$url+="$baseurl/custom.html?continent=asia"; break}
    "asia"             {$url+="$baseurl/custom.html?continent=asia"; break}
    "af"                {$url+="$baseurl/custom.html?continent=africa"; break}
    "africa"           {$url+="$baseurl/custom.html?continent=africa"; break}
    "na"                {$url+="$baseurl/custom.html?continent=namerica"; break}
    "namerica"      {$url+="$baseurl/custom.html?continent=namerica"; break}
    "sa"                {$url+="$baseurl/custom.html?continent=samerica"; break}
    "samerica"       {$url+="$baseurl/custom.html?continent=samerica"; break}
    "au"                {$url+="$baseurl/custom.html?continent=australasia"; break}
    "australasia"    {$url+="$baseurl/custom.html?continent=australasia"; break}
    "pa"                {$url+="$baseurl/custom.html?continent=australasia"; break}
    "pacific"          {$url+="$baseurl/custom.html?continent=australasia"; break}
    "eu"                {$url+="$baseurl/custom.html?continent=europe"; break}
    "europe"          {$url+="$baseurl/custom.html?continent=europe"; break}
    default            {$url=$baseurl; break}
}

trap{throw "Error: Can't connect to $url`n $_"; break}
    $wc=(new-object net.webclient).DownloadString($url);
    $regex=([regex]href="city.html\?n=\d+">(?<place>[^/]*)</a>(?:\s\*)?</td><td class="r">(?<time>[^/]*)</td>').matches($wc);
   
$times = $regex | select  @{n="Place";e={$_.groups['place'].value}}, @{n="Time";e={$_.groups['time'].value}}
   
if($filter -eq "*"){
        $times | sort place | format-table -autosize
    } else {
        $times | where {$_.place -like "*$filter*"} | sort place | format-table -autosize
    }
}

 

#################

You can also download the script file from here.

 

Examples:

1. Current local times around the world (Main list)

PS > Get-WorldClock

Place               Time
-----                ----
Addis Ababa    Sat 6:01 PM
Adelaide         Sun 12:31 AM
Aden              Sat 6:01 PM
Algiers           Sat 4:01 PM
Amman          Sat 6:01 PM
Amsterdam     Sat 5:01 PM
Anadyr           Sun 4:01 AM
Anchorage      Sat 7:01 AM
Ankara           Sat 6:01 PM
Antananarivo   Sat 6:01 PM
Asuncion         Sat 11:01 AM
Athens            Sat 6:01 PM
Atlanta           Sat 11:01 AM
Auckland        Sun 3:01 AM
...
...

 

2. Current local times in Europe (eu or europe)

PS > Get-WorldClock -continent eu

Place               Time
-----                ----

...
...
Beijing            Sat 11:32 PM
Beirut              Sat 6:32 PM
Belgrade          Sat 5:32 PM
Berlin              Sat 5:32 PM
Bogota            Sat 10:32 AM
Boston            Sat 11:32 AM
Brasilia           Sat 12:32 PM
Brisbane          Sun 1:32 AM
Brussels           Sat 5:32 PM
Bucharest        Sat 6:32 PM
Budapest         Sat 5:32 PM
Buenos Aires   Sat 12:32 PM
...
...

 

2. Current local time in Jerusalem (Asia)

PS > Get-WorldClock -continent asia -filter jeru


Place           Time
-----            ----
Jerusalem   Sat 6:42 PM

2 comments:

Anonymous said...

Looks like the owners of the timeanddate.com site don't like it being used in this way. When I run this function I get a message in the response string that says "scripts and programs that download content transparent to the user are not allowed without permission" :-(

Shay Levy said...

Hi

Sorry for the delay, I'm not experiencing your errors. Anyway, not sure if this helps, but try to add a new content header prior to sending the request, It may *fool* the website:

$wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;)")

This header is compatiable with WinXP+IE6

Hope that helps

Shay