commit
66adaeff0f
|
@ -4,11 +4,14 @@ package DDG::Goodie::UnixTime;
|
|||
use DDG::Goodie;
|
||||
|
||||
use DateTime;
|
||||
use List::MoreUtils qw( uniq );
|
||||
use Try::Tiny;
|
||||
|
||||
triggers startend => "unixtime", "time", "timestamp", "datetime", "epoch", "unix time", "unix timestamp", "unix time stamp", "unix epoch";
|
||||
my @trigger_words = ("unixtime", "datetime", "unix timestamp", "unix time stamp", "unix epoch", "epoch", "timestamp", "unix time");
|
||||
triggers startend => @trigger_words;
|
||||
|
||||
zci answer_type => "time_conversion";
|
||||
zci is_cached => 0;
|
||||
zci is_cached => 0;
|
||||
|
||||
attribution github => ['https://github.com/codejoust', 'codejoust'];
|
||||
|
||||
|
@ -19,21 +22,46 @@ code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DD
|
|||
category 'calculations';
|
||||
topics 'sysadmin';
|
||||
|
||||
handle remainder => sub {
|
||||
return unless defined $_;
|
||||
my $default_tz = 'UTC';
|
||||
my $time_format = '%a %b %d %T %Y %Z';
|
||||
my $header_format = "Time (%s)";
|
||||
my %no_default_triggers = map { $_ => 1 } qw(timestamp epoch);
|
||||
my $triggers = join('|', @trigger_words);
|
||||
my $extract_qr = qr/^(?<trigger>$triggers)?\s*(?<epoch>-?\d+)?\s*(?<trigger>$triggers)?$/;
|
||||
|
||||
my $time_input = shift;
|
||||
my $time_utc;
|
||||
eval {
|
||||
$time_input = int(length($time_input) >= 13 ? ($time_input / 1000) : ($time_input + 0));
|
||||
$time_utc = DateTime->from_epoch(
|
||||
epoch => $time_input,
|
||||
time_zone => "UTC"
|
||||
)->strftime("%a %b %d %T %Y %z");
|
||||
};
|
||||
handle query => sub {
|
||||
|
||||
return unless $time_utc;
|
||||
return "Unix Time: " . $time_utc;
|
||||
my $query = shift;
|
||||
$query =~ $extract_qr;
|
||||
my $time_input = $+{'epoch'};
|
||||
# If there was nothing in there, we must want now... unless we're not supposed to default for this trigger.
|
||||
$time_input //= time unless ($no_default_triggers{$+{'trigger'}});
|
||||
return unless defined $time_input;
|
||||
|
||||
my $dt = try { DateTime->from_epoch(epoch => $time_input) };
|
||||
return unless $dt;
|
||||
|
||||
my $time_output;
|
||||
my @table_data = (['Unix Epoch', $time_input]);
|
||||
|
||||
foreach my $tz (uniq grep { $_ } ($loc->time_zone, $default_tz)) {
|
||||
$dt->set_time_zone($tz);
|
||||
push @table_data, [sprintf($header_format, $tz), $dt->strftime($time_format)];
|
||||
}
|
||||
|
||||
my $text = join(' | ', (map { join(' => ', @{$_}) } @table_data));
|
||||
return $text, html => to_html(@table_data);
|
||||
};
|
||||
|
||||
sub to_html {
|
||||
my $results = "";
|
||||
my $minwidth = "90px";
|
||||
foreach my $result (@_) {
|
||||
$results .=
|
||||
"<div><span class=\"unixtime__label text--secondary\">$result->[0]: </span><span class=\"text--primary\">$result->[1]</span></div>";
|
||||
$minwidth = "180px" if length($result->[0]) > 10;
|
||||
}
|
||||
return $results . "<style> .zci--answer .unixtime__label {display: inline-block; min-width: $minwidth}</style>";
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
.zci--answer table.unixtime {
|
||||
font-family: Consolas,"Anonymous Pro",Anonymous,"Courier New",monospace;
|
||||
font-size: 12px;
|
||||
line-height: 14px;
|
||||
}
|
||||
|
||||
.zci--answer table.unixtime td {
|
||||
padding: 4px 10px 4px 10px;
|
||||
border: 0px;
|
||||
}
|
||||
|
||||
.zci--answer table.unixtime th {
|
||||
font-weight: bold;
|
||||
text-align: left;
|
||||
background-color: #aaa;
|
||||
color: #fff;
|
||||
border: 0px;
|
||||
padding: 4px 10px;
|
||||
}
|
27
t/UnixTime.t
27
t/UnixTime.t
|
@ -6,18 +6,31 @@ use Test::More;
|
|||
use DDG::Test::Goodie;
|
||||
|
||||
zci answer_type => 'time_conversion';
|
||||
zci is_cached => 0;
|
||||
zci is_cached => 0;
|
||||
|
||||
my @zero = (qr/Thu Jan 01 00:00:00 1970 UTC/, html => qr/Thu Jan 01 00:00:00 1970 UTC/);
|
||||
my @now = (qr/Unix Epoch.+UTC/, html => qr/UTC/);
|
||||
my @then = (qr/Tue Nov 18 00:28:30 1930 UTC/, html => qr/Tue Nov 18 00:28:30 1930 UTC/);
|
||||
my @later = (qr/Tue Jan 19 03:14:07 2038 UTC/, html => qr/Tue Jan 19 03:14:07 2038 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),
|
||||
'epoch 0' => test_zci(@zero),
|
||||
'epoch 2147483647' => test_zci(@later),
|
||||
'2147483647 epoch' => test_zci(@later),
|
||||
'timestamp 2147483647' => test_zci(@later),
|
||||
'datetime' => test_zci(@now),
|
||||
'unix time' => test_zci(@now),
|
||||
'unix epoch' => test_zci(@now),
|
||||
'epoch -1234567890' => test_zci(@then),
|
||||
'-1234567890 epoch' => test_zci(@then),
|
||||
'timestamp -1234567890' => test_zci(@then),
|
||||
'timestamp' => undef,
|
||||
'time' => undef,
|
||||
'epoch' => undef,
|
||||
);
|
||||
|
||||
done_testing;
|
||||
|
|
Loading…
Reference in New Issue