#! /usr/local/bin/perl
#---------------------------------------------------------------------
# $Id: mktime.pl 2775 2014-05-17 00:55:07Z cjm $
# Copyright 2007 Christopher J. Madsen <perl@cjmweb.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See either the
# GNU General Public License or the Artistic License for more details.
#
# Convert between UNIX time format and calendar dates
#---------------------------------------------------------------------

use strict;
use warnings;
use POSIX 'strftime';

die "Usage: mktime DATE\n" unless @ARGV;
$_ = join(' ',@ARGV);

my ($time,$date);

if (/^[0-9]+$/i) {
    $time = $_;
} elsif (/^(?:0x)?[0-9A-F]+$/i) {
    $time = hex $_;
} else {
    require Time::ParseDate;
    $time = Time::ParseDate::parsedate($_, PREFER_FUTURE => 1);
    die "`$_' is not a valid date" unless $time;
}

$_ = sprintf("0x%X", $time);
my $spaces = ' ' x length $_;

printf("%s => %d\n%s => %s     => %s\n%s => %s GMT => %s\n",
       $_, $time, $spaces, scalar localtime $time,
       strftime('%Y-%m-%d %H:%M:%S %z', localtime $time),
       $spaces, scalar gmtime $time,
       strftime('%Y-%m-%d %H:%M:%S +0000', gmtime $time));
