#!/usr/bin/perl

use strict;
use Web::Scraper;
use URI;
#use utf8;

# init
my $in_tr = scraper {
    process '//td[1]'  , 'district'  => 'TEXT';
    process '//td[2]'  , 'tzname'    => 'TEXT';
    process '//td[3]'  , 'zonetime'  => 'TEXT';
    process '//td[4]'  , 'timename'  => 'TEXT';
    process '//td[5]'  , 'dstrule'   => 'TEXT';
    process '//td[6]'  , 'lmt'       => 'TEXT';
    process '//td[7]'  , 'remark'    => 'TEXT';
    result qw(district tzname zonetime timename dstrule lmt remark);
};

my $tables = scraper {
    process '//tr' , 'tr[]'=> $in_tr;
    result qw(tr);
};

my $in_p = scraper {
    process '//table', 'table[]' => $tables;
    result qw(table);
};

# scrape
my $url_string = "http://home.tiscali.nl/~t876506/TZworld.html";
my @result= ();
my $scraped = $in_p->scrape(URI->new($url_string));
push (@result,@{$scraped}) if($scraped);

# output
binmode(STDOUT, ":utf8");

my $count=0;
foreach my $table (@result){
    next if($table->[0]->{'district'} =~ /^DST rule name/);
    foreach my $tr (@{$table}){
	next if($tr->{'district'} =~ /^District/);
	next if($tr->{'tzname'} !~ m{/});
#	next if($table->{'remark'} eq '');
	my @lines = (
		     $tr->{'district'},
		     $tr->{'tzname'},
		     $tr->{'zonetime'},
		     $tr->{'timename'},
		     $tr->{'dstrule'},
		     $tr->{'lmt'},
		     $tr->{'remark'}
		    );
	print join("\t",@lines),"\n";
    }
    print "\n";
}

exit;

