parent
4d62fd1731
commit
ede57fceda
|
@ -4,6 +4,7 @@ package DDG::Goodie::UnixTime;
|
|||
use DDG::Goodie;
|
||||
|
||||
use DateTime;
|
||||
use List::MoreUtils qw( uniq );
|
||||
use Try::Tiny;
|
||||
|
||||
triggers query => qr/^(?:(?:unixtime|datetime|unix time|unix timestamp|unix time stamp|unix epoch)( \d+)?|(?:timestamp|epoch) (\d+))$/;
|
||||
|
@ -22,26 +23,30 @@ topics 'sysadmin';
|
|||
|
||||
my $default_tz = 'UTC';
|
||||
my $time_format = '%a %b %d %T %Y %Z';
|
||||
my $header_format = "Time (%s)";
|
||||
|
||||
handle matches => sub {
|
||||
|
||||
my @matches = grep { defined $_ } @_;
|
||||
my $time_input = $matches[0] // time; # If there was nothing in there, we must want now.
|
||||
$time_input = 0 + $time_input; # Force to number.
|
||||
my @headers = ('Unix Epoch');
|
||||
my @data = ($time_input);
|
||||
|
||||
my $time_output;
|
||||
my $tz = $loc->time_zone || $default_tz; # Show them local time, otherwise the default.
|
||||
my $dt = try { DateTime->from_epoch(epoch => $time_input, time_zone => $tz,) };
|
||||
my $dt = try { DateTime->from_epoch(epoch => $time_input) };
|
||||
return unless $dt;
|
||||
$time_output = $dt->strftime($time_format);
|
||||
if ($tz ne $default_tz) {
|
||||
# They get the default TZ (UTC) regardless. Either we already did it or we do it now.
|
||||
$dt->set_time_zone($default_tz);
|
||||
$time_output .= ' / ' . $dt->strftime($time_format);
|
||||
foreach my $tz (uniq grep { $_ } ($loc->time_zone, $default_tz)) {
|
||||
$dt->set_time_zone($tz);
|
||||
push @headers, sprintf($header_format, $tz);
|
||||
push @data, $dt->strftime($time_format);
|
||||
}
|
||||
|
||||
return unless $time_output;
|
||||
return $time_input . ' (Unix time): ' . $time_output;
|
||||
return unless @data;
|
||||
my $html_table = '<table class="unixtime">';
|
||||
$html_table .= '<tr><th>' . join('</th><th>', @headers) . '</th></tr>';
|
||||
$html_table .= '<tr><td>' . join('</td><td>', @data) . '</td></tr>';
|
||||
$html_table .= '</table>';
|
||||
return $time_input . ' (Unix epoch): ' . join(' / ', @data), html => $html_table;
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
19
t/UnixTime.t
19
t/UnixTime.t
|
@ -8,20 +8,21 @@ use DDG::Test::Goodie;
|
|||
zci answer_type => 'time_conversion';
|
||||
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/;
|
||||
my @zero = (qr/0 \(Unix epoch\):.+Thu Jan 01 00:00:00 1970 UTC/, html => qr/Thu Jan 01 00:00:00 1970 UTC/);
|
||||
my @now = (qr/\d+ \(Unix epoch\):.+UTC/, html => qr/UTC/);
|
||||
my @later = (qr/2147483647 \(Unix epoch\):.+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($zero_re),
|
||||
'epoch 0' => test_zci($zero_re),
|
||||
'epoch 2147483647' => test_zci(qr/2147483647 \(Unix time\):.+Tue Jan 19 03:14:07 2038 UTC/),
|
||||
'timestamp 2147483647' => test_zci(qr/2147483647 \(Unix time\):.+Tue Jan 19 03:14:07 2038 UTC/),
|
||||
'datetime' => test_zci($now_re),
|
||||
'unix time' => test_zci($now_re),
|
||||
'unix epoch' => test_zci($now_re),
|
||||
'unix time 0000000000000' => test_zci(@zero),
|
||||
'epoch 0' => test_zci(@zero),
|
||||
'epoch 2147483647' => test_zci(@later),
|
||||
'timestamp 2147483647' => test_zci(@later),
|
||||
'datetime' => test_zci(@now),
|
||||
'unix time' => test_zci(@now),
|
||||
'unix epoch' => test_zci(@now),
|
||||
'timestamp' => undef,
|
||||
'time' => undef,
|
||||
'epoch' => undef,
|
||||
|
|
Loading…
Reference in New Issue