Created
May 16, 2014 09:52
-
-
Save tokuhirom/657c9e1d10628358d517 to your computer and use it in GitHub Desktop.
The epoch command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Time::Piece; | |
use Pod::Usage; | |
&main; exit; | |
sub show($) { | |
my $time = shift; | |
printf "%s %s\n", $time->strftime("%Y-%m-%d %H:%M:%S%z"), $time->epoch; | |
} | |
sub main { | |
my $stuff = shift @ARGV; | |
if (!defined $stuff) { | |
show(localtime()); | |
} elsif ($stuff =~ /^\d{4}-\d\d$/) { | |
my $first = localtime()->strptime($stuff, '%Y-%m'); | |
my $last = localtime()->strptime($stuff . '-' . $first->month_last_day . ' 23:59:59', '%Y-%m-%d %H:%M:%S'); | |
show($first); | |
show($last); | |
} elsif ($stuff =~ /^\d{4}-\d\d-\d\d$/) { | |
show(localtime()->strptime($stuff . ' 00:00:00', '%Y-%m-%d %H:%M:%S')); | |
show(localtime()->strptime($stuff . ' 23:59:59', '%Y-%m-%d %H:%M:%S')); | |
} elsif ($stuff =~ /^\d+$/) { | |
show(localtime($stuff)); | |
} else { | |
pod2usage(1); | |
} | |
} | |
__END__ | |
=head1 SYNOPSIS | |
% epoch 2013-05 | |
% epoch | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment