UnixTime: update output to include the epoch.
This is in service to some submarine commits which will be arriving soon to deal with Goodies templating. This makes UnixTime look "more like it will" in addition to supplying what we determined to be the supplied epoch. This is most useful in the "empty" epoch case where we will tell them the current epoch which may be of interest.master
parent
ae0ae15115
commit
a71a6132a9
|
@ -8,7 +8,7 @@ use DateTime;
|
|||
triggers startend => "unixtime", "time", "timestamp", "datetime", "epoch", "unix time", "unix timestamp", "unix time stamp", "unix epoch";
|
||||
|
||||
zci answer_type => "time_conversion";
|
||||
zci is_cached => 0;
|
||||
zci is_cached => 0;
|
||||
|
||||
attribution github => ['https://github.com/codejoust', 'codejoust'];
|
||||
|
||||
|
@ -19,21 +19,32 @@ code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DD
|
|||
category 'calculations';
|
||||
topics 'sysadmin';
|
||||
|
||||
my $default_tz = 'UTC';
|
||||
my $time_format = '%a %b %d %T %Y %Z';
|
||||
|
||||
handle remainder => sub {
|
||||
return unless defined $_;
|
||||
|
||||
my $time_input = shift;
|
||||
my $time_utc;
|
||||
$time_input = time if ($time_input eq ''); # Default to 'now' when empty.
|
||||
my $time_output;
|
||||
eval {
|
||||
$time_input = int(length($time_input) >= 13 ? ($time_input / 1000) : ($time_input + 0));
|
||||
$time_utc = DateTime->from_epoch(
|
||||
my $tz = $loc->time_zone || $default_tz; # Show them local time, if we know.
|
||||
my $dt = DateTime->from_epoch(
|
||||
epoch => $time_input,
|
||||
time_zone => "UTC"
|
||||
)->strftime("%a %b %d %T %Y %z");
|
||||
time_zone => $tz,
|
||||
);
|
||||
$time_output = $dt->strftime($time_format);
|
||||
if ($tz ne $default_tz) {
|
||||
# We'll show them both, then.
|
||||
$dt->set_time_zone($default_tz);
|
||||
$time_output .= ' / ' . $dt->strftime($time_format);
|
||||
}
|
||||
};
|
||||
|
||||
return unless $time_utc;
|
||||
return "Unix Time: " . $time_utc;
|
||||
return unless $time_output;
|
||||
return $time_input . ' (Unix time): ' . $time_output;
|
||||
};
|
||||
|
||||
1;
|
||||
|
|
17
t/UnixTime.t
17
t/UnixTime.t
|
@ -6,18 +6,21 @@ use Test::More;
|
|||
use DDG::Test::Goodie;
|
||||
|
||||
zci answer_type => 'time_conversion';
|
||||
zci is_cached => 0;
|
||||
zci is_cached => 0;
|
||||
|
||||
my $zero_re = qr/0 \(Unix time\):.+Thu Jan 01 00:00:00 1970 UTC/;
|
||||
my $now_re = qr/\d+ \(Unix time\):.+UTC/;
|
||||
|
||||
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.
|
||||
'unix time 0000000000000' => test_zci($zero_re),
|
||||
'epoch 0' => test_zci($zero_re),
|
||||
'epoch 2147483647' => test_zci(qr/2147483647 \(Unix time\):.+Tue Jan 19 03:14:07 2038 UTC/),
|
||||
'epoch' => test_zci($now_re),
|
||||
'timestamp' => test_zci($now_re),
|
||||
'datetime' => test_zci($now_re),
|
||||
);
|
||||
|
||||
done_testing;
|
||||
|
|
Loading…
Reference in New Issue