Merge branch 'timezone-converter' of git://github.com/GlitchMr/zeroclickinfo-goodies
commit
85cbf7cb4d
|
@ -60,8 +60,9 @@ sub parse_timezone(_) {
|
||||||
return $timezones{$name} + $modifier + $minutes / 60;
|
return $timezones{$name} + $modifier + $minutes / 60;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub to_time(_) {
|
sub to_time {
|
||||||
my $hours = shift;
|
my ($hours, $american) = @_;
|
||||||
|
my $pm = "";
|
||||||
my $seconds = 3600 * fmod $hours, 1 / 60;
|
my $seconds = 3600 * fmod $hours, 1 / 60;
|
||||||
|
|
||||||
# I'm using floating point numbers. They sometimes don't do what I want.
|
# I'm using floating point numbers. They sometimes don't do what I want.
|
||||||
|
@ -71,14 +72,21 @@ sub to_time(_) {
|
||||||
my $minutes
|
my $minutes
|
||||||
= ( $hours - int $hours ) * 60 - sprintf( '%.4f', $seconds ) / 60;
|
= ( $hours - int $hours ) * 60 - sprintf( '%.4f', $seconds ) / 60;
|
||||||
my $seconds_format = int $seconds ? ':%02.0f' : "";
|
my $seconds_format = int $seconds ? ':%02.0f' : "";
|
||||||
sprintf "%i:%02.0f$seconds_format", $hours, $minutes, $seconds;
|
if ($american) {
|
||||||
|
$pm = ' A.M.';
|
||||||
|
if ($hours >= 12) {
|
||||||
|
$pm = ' P.M.';
|
||||||
|
$hours -= 12;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sprintf "%i:%02.0f$seconds_format$pm", $hours, $minutes, $seconds;
|
||||||
}
|
}
|
||||||
|
|
||||||
handle query => sub {
|
handle query => sub {
|
||||||
my $timezone = qr/(\w+(?:\s*[+-]0*[0-9]{1,5}(?::[0-5][0-9])?)?)?/;
|
my $timezone = qr/(\w+(?:\s*[+-]0*[0-9]{1,5}(?::[0-5][0-9])?)?)?/;
|
||||||
my (
|
my (
|
||||||
# Time
|
# Time
|
||||||
$hour, $minutes, $seconds, $pm,
|
$hour, $minutes, $seconds, $american, $pm,
|
||||||
|
|
||||||
# Timezones
|
# Timezones
|
||||||
$input_timezone, $output_timezone,
|
$input_timezone, $output_timezone,
|
||||||
|
@ -99,7 +107,7 @@ handle query => sub {
|
||||||
# Optional spaces between tokens
|
# Optional spaces between tokens
|
||||||
\s*
|
\s*
|
||||||
# AM/PM
|
# AM/PM
|
||||||
(?:(?:A|(P))\.?M\.?)?
|
((?:A|(P))\.?M\.?)?
|
||||||
# Spaces between tokens
|
# Spaces between tokens
|
||||||
\s* \b
|
\s* \b
|
||||||
# Optional input timezone
|
# Optional input timezone
|
||||||
|
@ -128,7 +136,7 @@ handle query => sub {
|
||||||
my $gmt_output_timezone = parse_timezone $output_timezone;
|
my $gmt_output_timezone = parse_timezone $output_timezone;
|
||||||
$modifier += $gmt_output_timezone - $gmt_input_timezone;
|
$modifier += $gmt_output_timezone - $gmt_input_timezone;
|
||||||
for ( $gmt_input_timezone, $gmt_output_timezone ) {
|
for ( $gmt_input_timezone, $gmt_output_timezone ) {
|
||||||
$_ = to_time;
|
$_ = to_time $_;
|
||||||
s/\A\b/+/;
|
s/\A\b/+/;
|
||||||
s/:00\z//;
|
s/:00\z//;
|
||||||
}
|
}
|
||||||
|
@ -151,7 +159,7 @@ handle query => sub {
|
||||||
$days = sprintf ', %i day%s after', $_ / 24, $s;
|
$days = sprintf ', %i day%s after', $_ / 24, $s;
|
||||||
}
|
}
|
||||||
$_ = fmod $_, 24;
|
$_ = fmod $_, 24;
|
||||||
$_ = to_time . $days;
|
$_ = to_time($_, $american) . $days;
|
||||||
}
|
}
|
||||||
|
|
||||||
my ( $input_format, $output_format ) = ('%s, UTC%s') x 2;
|
my ( $input_format, $output_format ) = ('%s, UTC%s') x 2;
|
||||||
|
|
|
@ -12,10 +12,10 @@ ddg_goodie_test(
|
||||||
['DDG::Goodie::TimezoneConverter'],
|
['DDG::Goodie::TimezoneConverter'],
|
||||||
'3:14 in GMT' =>
|
'3:14 in GMT' =>
|
||||||
test_zci('3:14 (UTC) is 3:14 (GMT).'),
|
test_zci('3:14 (UTC) is 3:14 (GMT).'),
|
||||||
'8:10 A.M. AZOST to CAT' =>
|
'8:10 A.M. AZOST into CAT' =>
|
||||||
test_zci('8:10 (AZOST, UTC-1) is 11:10 (CAT, UTC+2).'),
|
test_zci('8:10 A.M. (AZOST, UTC-1) is 11:10 A.M. (CAT, UTC+2).'),
|
||||||
'1pm EDT into UTC+2' =>
|
'1pm EDT into UTC+2' =>
|
||||||
test_zci('13:00 (EDT, UTC-4) is 19:00 (UTC+2).'),
|
test_zci('1:00 P.M. (EDT, UTC-4) is 7:00 P.M. (UTC+2).'),
|
||||||
'1 into UTC -2 ' =>
|
'1 into UTC -2 ' =>
|
||||||
test_zci('1:00 (UTC) is 23:00, 1 day prior (UTC-2).'),
|
test_zci('1:00 (UTC) is 23:00, 1 day prior (UTC-2).'),
|
||||||
' 1 into UTC-1' =>
|
' 1 into UTC-1' =>
|
||||||
|
|
Loading…
Reference in New Issue