Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
Timezone Coverter: Removed friendly "Noon" and "Midnight" times from …
Browse files Browse the repository at this point in the history
…TimezoneConverter IA (#4481)

* Removed friendly "Noon" and "Midnight" times from TimezoneConverter IA

* Add "Noon" and "Midnight" alongside numerical time
  • Loading branch information
tossj authored and moollaza committed Sep 12, 2017
1 parent 4adee0f commit 2786874
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
16 changes: 11 additions & 5 deletions lib/DDG/Goodie/TimezoneConverter.pm
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ sub to_time {

my $pm = "";
my $seconds = 3600 * fmod $hours, 1 / 60;
my $time_word = "";

# I'm using floating point numbers. They sometimes don't do what I want.
if ( sprintf( '%.5f', $seconds ) == 60 ) {
Expand All @@ -69,16 +70,21 @@ sub to_time {

my $seconds_format = int $seconds ? ':%02.0f' : "";
if ($american) {
# Special case certain hours
return 'midnight' if $hours == 0;
return 'noon' if $hours == 12;
if ($hours == 0) {
$time_word = 'Midnight - ';
} elsif ($hours == 12) {
$time_word = 'Noon - ';
}

$pm = ' AM';
if ($hours > 12) {
if ($hours >= 12) {
$pm = ' PM';
$hours -= 12 if (int($hours) > 12);
} elsif ($hours == 0) {
$hours = 12;
}
}
sprintf "%i:%02.0f$seconds_format$pm", $hours, $minutes, $seconds;
sprintf "$time_word%i:%02.0f$seconds_format$pm", $hours, $minutes, $seconds;
}

handle query => sub {
Expand Down
28 changes: 14 additions & 14 deletions t/TimezoneConverter.t
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ ddg_goodie_test(
'3:14 UTC in GMT' => build_test('3:14 GMT', '3:14 UTC to GMT'),
'8:10 AM AZOST into CAT' => build_test('11:10 AM CAT', '8:10 AM AZOST (UTC-1) to CAT (UTC+2)'),
'1pm EDT into UTC+2' => build_test('7:00 PM UTC+2', '1:00 PM EDT (UTC-4) to UTC+2'),
'0pm UTC into GMT' => build_test('Noon GMT', 'Noon UTC to GMT'),
'0am UTC into UTC' => build_test('Midnight UTC', 'Midnight UTC to UTC'),
'0pm UTC into GMT' => build_test('Noon - 12:00 PM GMT', 'Noon - 12:00 PM UTC to GMT'),
'0am UTC into UTC' => build_test('Midnight - 12:00 AM UTC', 'Midnight - 12:00 AM UTC to UTC'),
'1 UTC into UTC -2 ' => build_test('23:00 UTC-2 (1 day prior)', '1:00 UTC to UTC-2'),
' 1 UTC into UTC-1' => build_test('0:00 UTC-1', '1:00 UTC to UTC-1'),
'21 FNT into EET' => build_test('1:00 EET (1 day after)', '21:00 FNT (UTC-2) to EET (UTC+2)'),
Expand Down Expand Up @@ -71,12 +71,12 @@ ddg_goodie_test(
'11:22am cest in localtime' => build_test(re(qr/5:22 AM EDT/), qq/11:22 AM CEST (UTC+2) to $test_location_tz/),
'11:22am cest in my local timezone' => build_test(re(qr/5:22 AM EDT/), qq/11:22 AM CEST (UTC+2) to $test_location_tz/),
'11:22am cest' => build_test(re(qr/5:22 AM EDT/), qq/11:22 AM CEST (UTC+2) to $test_location_tz/),
'12pm my time in CEST' => build_test(q/6:00 PM CEST/, qq/Noon $test_location_tz to CEST (UTC+2)/),
'12pm local timezone in CEST' => build_test(q/6:00 PM CEST/, qq/Noon $test_location_tz to CEST (UTC+2)/),
'12pm in CEST' => build_test(q/6:00 PM CEST/, qq/Noon $test_location_tz to CEST (UTC+2)/),
'12am my timezone in UTC' => build_test(q/4:00 AM UTC/, qq/Midnight $test_location_tz to UTC/),
'12am local time in UTC' => build_test(q/4:00 AM UTC/, qq/Midnight $test_location_tz to UTC/),
'12am in UTC' => build_test(q/4:00 AM UTC/, qq/Midnight $test_location_tz to UTC/)
'12pm my time in CEST' => build_test(q/6:00 PM CEST/, qq/Noon - 12:00 PM $test_location_tz to CEST (UTC+2)/),
'12pm local timezone in CEST' => build_test(q/6:00 PM CEST/, qq/Noon - 12:00 PM $test_location_tz to CEST (UTC+2)/),
'12pm in CEST' => build_test(q/6:00 PM CEST/, qq/Noon - 12:00 PM $test_location_tz to CEST (UTC+2)/),
'12am my timezone in UTC' => build_test(q/4:00 AM UTC/, qq/Midnight - 12:00 AM $test_location_tz to UTC/),
'12am local time in UTC' => build_test(q/4:00 AM UTC/, qq/Midnight - 12:00 AM $test_location_tz to UTC/),
'12am in UTC' => build_test(q/4:00 AM UTC/, qq/Midnight - 12:00 AM $test_location_tz to UTC/)
);
restore_time();

Expand All @@ -91,12 +91,12 @@ ddg_goodie_test(
'11:22am cest in localtime' => build_test(re(qr/4:22 AM EST/), qq/11:22 AM CEST (UTC+2) to $test_location_tz/),
'11:22am cest in my local timezone' => build_test(q/4:22 AM EST/, qq/11:22 AM CEST (UTC+2) to $test_location_tz/),
'11:22am cest' => build_test(q/4:22 AM EST/, qq/11:22 AM CEST (UTC+2) to $test_location_tz/),
'12pm my time in CEST' => build_test(q/7:00 PM CEST/, qq/Noon $test_location_tz to CEST (UTC+2)/),
'12pm local timezone in CEST' => build_test(q/7:00 PM CEST/, qq/Noon $test_location_tz to CEST (UTC+2)/),
'12pm in CEST' => build_test(q/7:00 PM CEST/, qq/Noon $test_location_tz to CEST (UTC+2)/),
'12am my timezone in UTC' => build_test(re(qr/5:00 AM UTC/), qq/Midnight $test_location_tz to UTC/),
'12am local time in UTC' => build_test(re(qr/5:00 AM UTC/), qq/Midnight $test_location_tz to UTC/),
'12am in UTC' => build_test(re(qr/5:00 AM UTC/), qq/Midnight $test_location_tz to UTC/),
'12pm my time in CEST' => build_test(q/7:00 PM CEST/, qq/Noon - 12:00 PM $test_location_tz to CEST (UTC+2)/),
'12pm local timezone in CEST' => build_test(q/7:00 PM CEST/, qq/Noon - 12:00 PM $test_location_tz to CEST (UTC+2)/),
'12pm in CEST' => build_test(q/7:00 PM CEST/, qq/Noon - 12:00 PM $test_location_tz to CEST (UTC+2)/),
'12am my timezone in UTC' => build_test(re(qr/5:00 AM UTC/), qq/Midnight - 12:00 AM $test_location_tz to UTC/),
'12am local time in UTC' => build_test(re(qr/5:00 AM UTC/), qq/Midnight - 12:00 AM $test_location_tz to UTC/),
'12am in UTC' => build_test(re(qr/5:00 AM UTC/), qq/Midnight - 12:00 AM $test_location_tz to UTC/),
);

restore_time();
Expand Down

0 comments on commit 2786874

Please sign in to comment.