Skip to content

Instantly share code, notes, and snippets.

@tokuhirom

tokuhirom/epoch

Created May 16, 2014
Embed
What would you like to do?
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