TimezoneConverter: support "local time" variations.

master
Matt Miller 2014-10-28 16:04:01 +02:00
parent b9083d30fe
commit e71e35c9cc
2 changed files with 20 additions and 4 deletions

View File

@ -57,13 +57,15 @@ my %timezones = qw(
YEKT 5
);
my $default_tz = 'UTC';
my $default_tz = 'UTC';
my $localtime_re = qr/(?:(?:my|local|my local)\s*time(?:zone)?)/i;
my $timezone_re = qr/(?:\w+(?:\s*[+-]0*[0-9]{1,5}(?::[0-5][0-9])?)?|$localtime_re)?/;
sub parse_timezone {
my $timezone = shift;
# They said "my timezone" or similar.
if ($timezone =~ /^my time/i) {
if ($timezone =~ /$localtime_re/i) {
my $dt = DateTime->now(time_zone => $loc->time_zone || $default_tz );
return ($dt->time_zone_short_name, $dt->offset / 3600);
}
@ -113,8 +115,6 @@ sub to_time {
sprintf "%i:%02.0f$seconds_format$pm", $hours, $minutes, $seconds;
}
my $timezone_re = qr/(?:\w+(?:\s*[+-]0*[0-9]{1,5}(?::[0-5][0-9])?)?|(?:my\s+time(?:zone)?))?/;
handle query => sub {
my $query = $_;
$query =~ m{

View File

@ -98,14 +98,30 @@ ddg_goodie_test(
test_zci(qr/11:22 AM \(CEST, UTC\+2\) is [54]:22 AM $test_location_tz/,
html => '-ANY-'
),
'11:22am cest in localtime' =>
test_zci(qr/11:22 AM \(CEST, UTC\+2\) is [54]:22 AM $test_location_tz/,
html => '-ANY-'
),
'11:22am cest in my local timezone' =>
test_zci(qr/11:22 AM \(CEST, UTC\+2\) is [54]:22 AM $test_location_tz/,
html => '-ANY-'
),
'12pm my time in CEST' =>
test_zci(qr/Noon $test_location_tz is [67]:00 PM \(CEST, UTC\+2\)./,
html => '-ANY-'
),
'12pm local timezone in CEST' =>
test_zci(qr/Noon $test_location_tz is [67]:00 PM \(CEST, UTC\+2\)./,
html => '-ANY-'
),
'12am my timezone in UTC' =>
test_zci(qr/Midnight $test_location_tz is [45]:00 AM \(UTC\)./,
html => '-ANY-'
),
'12am local time in UTC' =>
test_zci(qr/Midnight $test_location_tz is [45]:00 AM \(UTC\)./,
html => '-ANY-'
),
# Intentional non-answers
'12 in binary' => undef,
);