Merge pull request #606 from mwmiller/epic_epoch

UnixTime: subsume Epoch.pm functions.
master
Rob Emery 2014-08-22 18:50:12 +01:00
commit 8dcb55792c
4 changed files with 21 additions and 79 deletions

View File

@ -1,32 +0,0 @@
package DDG::Goodie::Epoch;
# ABSTRACT: UNIX epoch <-> human time
use DDG::Goodie;
use Date::Calc qw(Today_and_Now Mktime);
zci is_cached => 0;
zci answer_type => "epoch";
primary_example_queries 'epoch';
description 'Time since the Unix epoch';
name 'epoch';
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/Epoch.pm';
category 'computing_tools';
topics 'sysadmin';
attribution web => [ 'https://www.duckduckgo.com', 'DuckDuckGo' ],
github => [ 'https://github.com/duckduckgo', 'duckduckgo'],
twitter => ['http://twitter.com/duckduckgo', 'duckduckgo'];
triggers query_lc => qr/^epoch$/i;
handle query => sub {
my ($year, $month, $day, $hour, $min, $sec) = Today_and_Now();
my $epoch = Mktime($year, $month, $day, $hour, $min, $sec);
$sec = '0' . $sec if length($sec) == 1;
$sec = '0' . $min if length($min) == 1;
$sec = '0' . $hour if length($hour) == 1;
return qq(Unix time: $epoch (for $month/$day/$year $hour:$min:$sec));
};
1;

View File

@ -2,6 +2,7 @@ package DDG::Goodie::UnixTime;
# ABSTRACT: epoch -> human readable time
use DDG::Goodie;
use DateTime;
triggers startend => "unixtime", "time", "timestamp", "datetime", "epoch", "unix time", "unix timestamp", "unix time stamp", "unix epoch";
@ -20,27 +21,19 @@ topics 'sysadmin';
handle remainder => sub {
my $time_input = 0;
my $time_input = shift;
$time_input = time if ($time_input eq ''); # Default to 'now' when empty.
my $time_utc;
eval {
$time_input = int(length ($_) >= 13 ? ($_ / 1000) : ($_ + 0));
};
if ($@) { return; }
if ($time_input >= 0){
my $my_time = DateTime->from_epoch(
epoch => $time_input,
$time_input = int(length($time_input) >= 13 ? ($time_input / 1000) : ($time_input + 0));
$time_utc = DateTime->from_epoch(
epoch => $time_input,
time_zone => "UTC"
);
my $time_utc = $my_time->strftime("%a %b %d %T %Y %z");
return "Unix Time Conversion: " . $time_utc if $time_utc;
}
return;
)->strftime("%a %b %d %T %Y %z");
};
return unless $time_utc;
return "Unix Time: " . $time_utc;
};
1;

View File

@ -1,19 +0,0 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use DDG::Test::Goodie;
zci answer_type => 'epoch';
zci is_cached => 0;
ddg_goodie_test(
[qw( DDG::Goodie::Epoch )],
'epoch' => test_zci(
qr/Unix time/,
),
);
done_testing;

View File

@ -8,16 +8,16 @@ use DDG::Test::Goodie;
zci answer_type => 'time_conversion';
zci is_cached => 0;
ddg_goodie_test(
[qw(
DDG::Goodie::UnixTime
)],
'unix time 0000000000000' => test_zci('Unix Time Conversion: Thu Jan 01 00:00:00 1970 +0000'),
'epoch 0' => test_zci('Unix Time Conversion: Thu Jan 01 00:00:00 1970 +0000'),
'epoch 2147483647' => test_zci('Unix Time Conversion: Tue Jan 19 03:14:07 2038 +0000'),
map {
"$_ 0" => test_zci('Unix Time Conversion: Thu Jan 01 00:00:00 1970 +0000'),
}, [ 'unixtime', 'time', 'timestamp', 'datetime', 'epoch', 'unix time', 'unix timestamp', 'unix epoch' ]
ddg_goodie_test([qw(
DDG::Goodie::UnixTime
)
],
'unix time 0000000000000' => test_zci('Unix Time: Thu Jan 01 00:00:00 1970 +0000'),
'epoch 0' => test_zci('Unix Time: Thu Jan 01 00:00:00 1970 +0000'),
'epoch 2147483647' => test_zci('Unix Time: Tue Jan 19 03:14:07 2038 +0000'),
'epoch' => test_zci(qr/Unix Time: [^\+]+\+0000/), # Now in UTC.
'timestamp' => test_zci(qr/Unix Time: [^\+]+\+0000/), # Now in UTC.
'datetime' => test_zci(qr/Unix Time: [^\+]+\+0000/), # Now in UTC.
);
done_testing;