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

Commit

Permalink
UnixTime now returns Coordinated Universal Time
Browse files Browse the repository at this point in the history
Updated tests. Added time zone as hour offset from UTC to avoid
confusion between acronyms.
  • Loading branch information
majuscule committed May 14, 2012
1 parent 701446e commit c9ef973
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
1 change: 1 addition & 0 deletions dist.ini
Expand Up @@ -17,6 +17,7 @@ File::ShareDir::ProjectDistDir = 0.2.0
HTML::Entities = 3.69
Text::Unidecode = 0.04
Date::Calc = 6.3
DateTime = 0.74
Lingua::EN::Numericalize = 1.52
Locale::SubCountry = 1.50
; causing problems because not pp: HTML::Barcode::QRCode = 0.09
Expand Down
18 changes: 14 additions & 4 deletions lib/DDG/Goodie/UnixTime.pm
@@ -1,20 +1,30 @@
package DDG::Goodie::UnixTime;

use DDG::Goodie;
use DateTime;

zci answer_type => "time_conversion";
zci is_cached => 1;
triggers startend => "unixtime", "time", "timestamp", "datetime", "epoch";

handle remainder => sub {

my $time_input = int(length ($_) >= 13 ? ($_ / 1000) : ($_ + 0));
my $time_input = 0;
eval {
$time_input = int(length ($_) >= 13 ? ($_ / 1000) : ($_ + 0));
};
if ($@) { return; }

if ($time_input >= 0){

my $my_time = localtime($time_input);

return "Unix Time Conversion: " . $my_time if $my_time;
my $my_time = DateTime->from_epoch(
epoch => $time_input,
time_zone => "UTC"
);

my $time_utc = $my_time->strftime("%a %b %m %T %Y %z");

return "Unix Time Conversion: " . $time_utc if $time_utc;

}

Expand Down
7 changes: 2 additions & 5 deletions t/UnixTime.t
Expand Up @@ -12,11 +12,8 @@ ddg_goodie_test(
[qw(
DDG::Goodie::UnixTime
)],
'time 0' => test_zci('Unix Time Conversion: Wed Dec 31 19:00:00 1969'),
'time 1335233773453' => test_zci('Unix Time Conversion: Mon Apr 23 22:16:13 2012'),
'time 1335233773' => test_zci('Unix Time Conversion: Mon Apr 23 22:16:13 2012'),
'time 5325423' => test_zci('Unix Time Conversion: Tue Mar 3 10:17:03 1970'),
'time 53492399294' => test_zci('Unix Time Conversion: Sat Feb 7 18:48:14 3665')
'time 0' => test_zci('Unix Time Conversion: Thu Jan 01 00:00:00 1970 +0000'),
'time 0000000000000' => test_zci('Unix Time Conversion: Thu Jan 01 00:00:00 1970 +0000')
);

done_testing;

1 comment on commit c9ef973

@codejoust
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating the goodie to use DateTime.
I was trying to find a solution that didn't require another package earlier, but this seems to be the best solution.

Please sign in to comment.