Skip to content

Instantly share code, notes, and snippets.

@tokuhirom
Created May 16, 2014 09:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tokuhirom/657c9e1d10628358d517 to your computer and use it in GitHub Desktop.
Save tokuhirom/657c9e1d10628358d517 to your computer and use it in GitHub Desktop.
The epoch command
#!/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