General Purpose Date Script

There must be thousands of one-off solutions scattered around this forum. GNU Date is so handy because it's general but if they're asking they probably don't have it. We have some nice scripts but they tend to need dates formatted in a very particular way.

This is a rough approximation which can handle -d "something - 3 days" kind of requests. It is not nearly so robust as GNU Date.

I'm trying to avoid needing nonstandard modules. Comments, criticism?

#!/usr/bin/perl

use POSIX;
use strict;
use warnings;

my $cmdstr="%Y-%m-%d %H:%M:%S";		# see strftime
my ($input, $arg, $sign, $ref,$offset,$quiet,$n)=("", undef, 0, time,0,0,0);
# $lt[SEC] is seconds, etc.  See perldoc -f localtime
use constant {	SEC=>0,	MIN=>1,	HOUR=>2,DAY=>3,	MON=>4,	YEAR=>5,WDAY=>6 };
# Times stored in the format of 'perldoc -f localtime'.
# In the case of @changed, only elements that were altered in @lt are 
# set,other values are undefined.
my (@lt, @changed);
# Lookups for mon/tues/wed jan/feb/mar names into day and month numbers
my (%month, %days);
# Lookup table to convert 'year' into a number of seconds, etc
my %mult=("second" => 1, "seconds" => 1, "minute" => 60, "minutes" => 60,
	"hour" => 3600, "hours" => 3600, "day" => 86400, "days" => 86400,
	"week" => 604800, "weeks" => 604800,
	"year" => 31536000, "years" => 31536000 );

# Parse commandline arguments
while(defined($arg=shift)) {
        if($arg =~ /^--date=(.*)/)  {       $input=$1;                  }
        elsif($arg eq "-d")         {       $input=shift;               }
        elsif($arg =~/^\+(.*)/)     {       $cmdstr=$1;                 }
	elsif($arg eq "-q")	    {	    $quiet=1;		        }
        elsif(($arg eq "-r") || ($arg =~ /^--reference=(.*)$/))
	{
		if(defined($1))	{ $arg=$1; }
		else		{ $arg=shift; }
	        my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
        	       $atime,$mtime,$ctime,$blksize,$blocks)
	               = stat($arg);

	        defined($dev) || die("No such file $arg");

		$ref=$mtime;
	}
        elsif($arg =~/^(-h|--help|--version)$/) {
                print STDERR <<"EOT";
date.pl v0.1.0, Tyler Montbriand, 2016.  Free PERL date calc/converter.
 -d "time string"       string like "YYYYMMDD", "YYYY/MM/DD",
                        "HHMMSS", "HH:MM:SS", "\@epoch", "- 3 days",
			"Mar 3 2016 1:16:09 AM", 
                        etc.  You can string them together, like
                        "\@1343322750 - 3 days".

 -r /path/to/file       Show mtime of the given file, not current time.

 +"formatstring"        Give strftime this format string instead of the
                        default "%Y-%m-%d %H:%M:%S".  See 'man strftime'

 -q			date.pl warns you when given conflicting
			information, i.e. feb 29 not on a leap year.
			It also warns you when input isn't understood.
			-q suppesses this.
Examples:
date.pl                         # current time in YYYY-MM-DD HH:MM:SS
TZ="UTC" ./date.pl              # Use an alternate time zone
date.pl +"%a %b %d %Y %r"       # Like Thu Jan 16 2014 12:58:59 PM
date.pl -d "+ 3 days"           # Current time plus three days
date.pl -d "\@1343322750"       # exact time in epoch seconds
date.pl -d "2013/01/02 12:00:00"# exact time in YYYYMMDD HHMMSS
date.pl -d "9am"                # Today at 9am
date.pl -d "last week + 5 minutes"
date.pl -r /etc/passwd          # display mtime of /etc/passwd
date.pl -r /etc/passwd -d "12:00:00" # date of /etc/passwd, time of noon
EOT
                exit(1);
        }
        else {       die("unknown argument $arg, try --help");   }
}

# Load hashes full of day and month names

#loadhash(\%month, `locale mon`, `locale abmon`);
loadhash(\%month, "January;February;March;April;May;June;July;August;September;October;November;December",
	"Jan;Feb;Mar;Apr;May;Jun;Jul;Aug;Sep;Oct;Nov;Dec");
#loadhash(\%days, `locale day`, `locale abday`);
loadhash(\%days, "Sunday;Monday;Tuesday;Wednesday;Thursday;Friday;Saturday",
	"Sun;Mon;Tues;Wed;Thurs;Fri;Sat");

@lt=localtime($ref);

# Lowercase input so 'tues', etc can be reliably found in tables
$input=lc($input); 
# Separate strings and numbers into their own tokens, like "9am" => "9 am"
# : still belongs with numbers for HH:MM:SS etc.
$input =~ s/([0-9:])([a-z])/$1 $2/g;
$input =~ s/([a-z])([0-9:])/$1 $2/g;
# Split +/- into their own tokens
$input =~ s/([+-])/ $1 /g;

# Split input on whitespace and commas and jam back into ARGV
unshift(@ARGV, split(/[ \r\n\t,]+/, $input));

while(defined($arg=shift))
{
        if(length($arg) == 0){ next; } # Empty string?  Ignore

	# Handle these in offset section
	if(($arg eq "+")||($arg eq "plus")||($arg eq "-")||($arg eq "minus"))
	{	unshift(@ARGV, $arg);	last;	}

        ################## DATE FORMAT DETECTION ########################

	if(exists($month{$arg})) # Dates like "Jan" "17"
	{
		set(MON, $month{$arg});
		if(($#ARGV >= 0) && ($ARGV[0] =~ /^[0-9]+$/))
		{	set(DAY, $ARGV[0] + 0);	shift;	}
		next;
	}

	if(exists($days{$arg}))	# mon/monday/etc
        {
		set(WDAY, $days{$arg} );

		# If it's followed by a numeral, i.e. Monday 7,
		# the numeral is the day of the month
		if(($#ARGV >= 0) && ($ARGV[0] =~ /^[0-9]+$/))
		{ set(DAY, $ARGV[0] + 0); shift; }

		next;
	}

	# a bare 4-digit number beginning with 19 or 20 is probably a year
	if($arg =~ /^(19[0-9][0-9])|^(2[0-9][0-9][0-9])$/)
	{	set(YEAR, $arg - 1900);	next;	}

        # @1234 means seconds in epoch time
        if($arg =~ /^@([0-9]+)$/)      {
		@lt=localtime($1+0);
		# Date has been replaced, prev changes are now irrelevant
		for($n=0; $n<8; $n++) { $changed[$n]=undef; }
		next;
	}

        # Checks for YYYYMMDD or YYYY/MM/DD time
        if(($arg =~ /^([0-9]{4})([\/-])([0-9]{1,2})\2([0-9]{1,2})$/) ||
		($arg =~ /^([0-9]{4})()([0-9]{2})([0-9]{2})$/))
        {
		set(YEAR, $1-1900); set(MON, $3-1); set(DAY, $4+0);

		# Set time variables which haven't been set already
		if(!defined($changed[HOUR]))
		{ set(SEC,0); set(MIN,0); set(HOUR,0); }

                next;
        }

        # HH:MM:SS times.  Sub-second times are allowed but ignored
        if($arg =~ /([0-2]?[0-9]):([0-5][0-9])(:([0-5][0-9])(.[0-9]+)?)?$/)
        {
		if(defined($4)) { set(SEC, $4+0); }
		else		{ set(SEC, 0); }

		set(MIN, $2+0);	set(HOUR, $1+0);

		# Handle time with PM in it
		if($#ARGV < 0) { }
		elsif($ARGV[0] eq "pm")  { set(HOUR, pm($lt[HOUR])); shift; }
		elsif($ARGV[0] eq "am")  { set(HOUR, am($lt[HOUR])); shift; }
                next;
        }

        # Times like 9 AM
        if(($arg =~ /^([0-9]+)$/) && ($#ARGV>=0) && ($ARGV[0] =~ /^(am|pm)$/)) {
		$arg=$arg + 0;

		set(SEC, 0); set(MIN,0); set(HOUR, $arg);
		if($ARGV[0] eq "am") { set(HOUR, am($lt[HOUR])); }
		else                 { set(HOUR, pm($lt[HOUR])); }
		shift;
		next;
	}

	# Redundant, but whatever
        if($arg =~ /^now$/)  {
		@lt=localtime(time);
		# Date has been replaced, prev changes are now irrelevant
		for($n=0; $n<8; $n++) { $changed[$n]=undef; }
		next
	}

	# last second/minute/hour/week/year
	if($arg =~ /^(last)|(next)$/)
	{
		if(defined($1)) { $sign=-1; } else { $sign=1; }

		if($#ARGV < 0)
		{
			if(!$quiet)
			{	print STDERR "next what, exactly?\n"	}

			next;
		}

		if(defined($mult{$ARGV[0]}))
		{
			@lt=localtime(time);
			# Date has been replaced, prev changes now irrelevant
			for($n=0; $n<8; $n++) { $changed[$n]=undef; }
			$offset *= $mult{$ARGV[0]};
		}
		# Adding months can't be handled in offset sadly
		elsif(($ARGV[0] eq "month") || ($ARGV[0] eq "months"))
		{	add_month($sign);	}
		elsif(!$quiet)
		{
			print STDERR $ARGV[0]." not a valid option for next\n";
		}

		shift;
		next;
	}

        if(! $quiet) { print STDERR "Unknown argument $arg\n"; }
}

# If there are any arguments left, we found a +/- and need to process
# that time offset.
while(defined($arg=shift))
{
        if(length($arg) == 0){ next; } # Empty string?  Ignore

	if(($arg eq "plus") || ($arg eq "+"))	{ $sign=1; next; }
	if(($arg eq "minus") ||($arg eq "-"))	{ $sign=-1; next; }

	# A number followed by a type, "9" "years"
	if(($arg =~ /^[0-9]+$/) && ($#ARGV >= 0))
	{
		my $arg2=shift;

		if((! $sign)&&(!$quiet))
		{
			print STDERR "Warning, no sign for numeric value\n"
		}

		# second/minute/hour/day/week/year are just multiplication
		if(defined($mult{$arg2}))
		{ $offset += $mult{$arg2} * $arg * $sign; next; }
		# No exact number of seconds per month, just count
		elsif(($arg2 eq "month")||($arg2 eq "months"))
		{ add_month($sign * $arg); next; }

		# Leave for error handler below to find
		$arg=$arg2;
	}

	# If we get here, something went wrong.
	if(!$quiet) {
		print STDERR "Unknown syntax ".$ARGV[0]."\n";
	}
}

my $nref=mktime(@lt); # Convert the altered @lt values back into epoch time

# Sanity checking.  If localtime(mktime(@lt)) produces different values
# from what was in @lt, we must have given it a nonsensical value which
# mktime corrected.
my @san=localtime($nref);

# Titles for localtime() array elements
my @title=("Seconds","Minutes","Hours","Day","Month","Year","Weekday");

for($n=0; $n<=6; $n++)
{
	if(($quiet == 0) && defined($changed[$n]) && ($changed[$n] != $san[$n]))
	{
		printf STDERR "%s changed, inconsistent input?", $title[$n];
		printf STDERR "\t%s in %s out\n", $changed[$n], $san[$n];
	}
}

# Print the calculated time plus offset
print strftime($cmdstr."\n", localtime($nref + ($offset)));
exit(0);

#########################################################################
################################ SUBROUTINES ############################
#########################################################################

# Adds or subtracts a number of months to the time in @lt,
# accounting for year wraparound when the number goes above 11 or below 0
sub add_month {
	$lt[MON] += shift;
	while($lt[MON] >= 12) { $lt[YEAR]++; $lt[MON] -= 12; }
	while($lt[MON] < 0) { $lt[MON] += 12; $lt[YEAR] --; }

	set(MON, $lt[MON]);
	set(YEAR, $lt[YEAR]);
}

# Alter a value in @lt, and mark that index as 'changed' by altering
# the value in @changed too
sub set { my ($i,$v)=(shift,shift); $lt[$i]=$changed[$i]=$v; }

# Takes a numeric AM hour, returns a number in 24-hour time
sub am { if($_[0] == 12) { return($_[0] - 12); } return($_[0]); }
# Takes a numeric PM hour, returns an hour in 24-hour time
sub pm { if($_[0] >= 1) { return($_[0] + 12); } return($_[0]); }

# loadhash(\%hash, "A;B", "a;b")
# sets $hash{A}=0, $hash{B}=1, $hash{a}=0, $hash{b}=1
sub loadhash {
        my ($h,$n)=(shift,0);
	foreach(@_)	{
		$n=0; foreach(split('[;\n]', lc($_))) { ${$h}{$_}=$n++; }
	}
}
3 Likes

Ok, I'm using Linux Slackware64 but...

perl date.pl 
Can't locate Switch.pm in @INC (you may need to install the Switch module) (@INC contains: /usr/local/lib64/perl5
 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at date.pl line 3.
BEGIN failed--compilation aborted at date.pl line 3.

it seems that Switch it's no so standard :).

Is it a bug?

# 1)
$> date --date="2013/10/20 - 203 days" '+%Y-%m-%d %H-%M-%S'
2013-03-31 00-00-00

$> perl date.pl -d '2013/10/20 - 203 days'
2013-03-30 23:00:00

# 2)
$> date --date="2013/10/25 + 3 days" '+%Y-%m-%d %H-%M-%S'
2013-10-28 00-00-00

$> perl date.pl -d '2013/10/25 + 3 days'
2013-10-27 23:00:00

# 3)
$> perl date.pl -d 'now + 3 days'
Unknown argument now

$> perl date.pl -d 'today + 3 days'
Unknown argument today

I think it's very hard what you're trying to do.

Emanuele

1 Like

Bug noted, thank you a lot, I had --date "something" when I should have done --date="something".

Thanks for reminding me about the +syntax feature, which I'd planned but forgotten.

Date math is not hard here, I am letting mktime handle absolutely everything tricky. I do not need to know when the next leap year is, I subtract 1 from the 'year' value and let mktime decide what that is.

Yes yes I know that Perl has about 37 different date modules I could be using. But if I'm going to tell someone to install 'Perl::MyFavoriteDateModule' I might as well just tell them to install GNU date. The whole point is to not do that, (and to show elegant Perl code is possible without including the kitchen sink).

Some of those tests are just weird though. I have no idea how you got 23 hours from subtracting days, it certainly doesn't happen here. I may have done something odd with a last-minute fix when I posted. [edit] Now I know. It happened because of the --date vs --date= problem. Perl does strange things when you do arithmetic on strings.

Anyway, I'll incorporate your suggestions and repost. Thanks again. [edit] Version 2 is now in the OP.

Updated. switch() removed, known bugs fixed, +formatting added.

I think the problem is with daylight saving time. But, see this:

# 1) correct
$> perl date.pl -d 'now + 3 days'
2013-10-28 23:24:14
$> date -d 'now + 3 days'
Mon Oct 28 23:24:20 CET 2013

#2) error:
$> perl date.pl -d '2013/10/25 + 3 days'
2013-10-27 23:00:00
$> date -d '2013/10/25 + 3 days'
Mon Oct 28 00:00:00 CET 2013

I hope this can help you,
Emanuele

1 Like

I get no such errors here, even when fiddling with the TZ variable (which perl appears to support). What version of perl do you use?

The real error was that the string 'now', which caused no errors, wasn't supported and should have been flagged.

Error detection has been made more robust as a result and support for 'now' added:

#!/usr/bin/perl

use POSIX;

my $cmdstr="%Y-%m-%d %H:%M:%S";
my ($input, $arg, $sign)=(undef, undef, 0);
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime();

# Commandline parsing stuff
while(defined($arg=shift)) {
        if($arg =~ /^--date=/)  {       $input=substr($arg, 7);         }
        elsif($arg =~ /^-d$/)   {       $input=shift;                   }
        elsif($arg =~/^\+/)     {       $cmdstr=substr($arg,1);         }
        else                    {       print STDERR "unknown $arg\n";  }
}

# Put the date string back into argv, split on spaces
unshift(@ARGV, split(/[ \t]+/, $input));

while(defined($arg=shift))
{
        # Need to split +1 into +1
        if($arg =~ /^[+]/) {    $sign=1;        $arg=substr($arg,1);    }
        elsif($arg =~ /^-/) {   $sign=-1;       $arg=substr($arg,1);    }

        ################## DATE FORMAT DETECTION ########################

        # @1234 means seconds in epoch time
        if($arg =~ /^@[0-9]+$/)
        {
                ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($1+0);
                next;
        }

        # Checks for YYYYMMDD or YYYY/MM/DD time
        # TODO:  Check for YYMMDD dates
        # TODO:  Check for YYYYDDMM dates (ugh)
        if($arg =~ /^([0-9]{4})(\/?)([0-9]{2})\2([0-9]{2})/)
        {
                ($year,$mon,$mday)=($1-1900,$3-1,$4+0);
                ($sec,$min,$hour)=(0,0,0);
                next;
        }

        # HH:MM:SS times
        if($arg =~ /([0-2][0-9]):([0-5][0-9]):([0-5][0-9])(.[0-9]+)?$/)
        {
                ($sec,$min,$hour)=($3+0, $2+0, $1+0);
                next;
        }

        # As last resort, assume its a pure number.
        if($arg =~ /^([0-9]+)$/) {
                if($sign == 0)
                {
                        print STDERR "offset without unit";
                        exit(1);
                }

                $offset=$1+0;
                next;
        }

        if($arg =~ /^seconds?$/)        {       }
        elsif ($arg =~ /^years?$/) {
                                        $year += ($offset*$sign);
                                        $sign=0;
                                        $offset=0;
                                }
        elsif($arg =~ /^minutes?$/)     {       $offset *= 60;          }
        elsif($arg =~ /^hours?/)        {       $offset *= 60*60;       }
        elsif($arg =~ /^days?/)         {       $offset *= 60*60*24;    }
        elsif($arg =~ /^weeks?/)        {       $offset *= 60*60*24*7;  }
        elsif($arg =~ /^months?$/)      {

                                        $mon += ($offset*$sign);

                                        while($mon > 12)
                                        {
                                                $mon-=12;
                                                $year++;
                                        }

                                        while($mon < 0)
                                        {
                                                $mon+=12;
                                                $year--;
                                        }

                                        $sign=0;
                                        $offset=0;
                                }
        elsif($arg =~ /^now$/)  {
                ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime();
        }
        elsif(length($arg) == 0){       } # Empty string?  Ignore
        else                    {
                                        print STDERR "Unknown argument $arg\n";
                                        exit(1);
        }
}

# Convert the altered year, month, etc back into epoch time.
my $ref=mktime($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
# Add the seconds offset calculated above.
$ref += ($sign * $offset);
# Convert back into list.
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($ref);

print strftime($cmdstr."\n",
        $sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);

An updated version which supports --reference/-r:

#!/usr/bin/perl

use POSIX;

my $cmdstr="%Y-%m-%d %H:%M:%S";
my ($input, $arg, $sign, $file)=(undef, undef, 0, undef);
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime();

# Commandline parsing stuff
while(defined($arg=shift)) {
        if($arg =~ /^--date=/)  {       $input=substr($arg, 7);         }
        elsif($arg =~ /^-d$/)   {       $input=shift;                   }
        elsif($arg =~ /^-r$/)   {       $file=shift;                    }
        elsif($arg =~ /^--reference=/) {$file=substr($arg, 12);         }
        elsif($arg =~/^\+/)     {       $cmdstr=substr($arg,1);         }
        else                    {       die("unknown argument $arg");   }
}

if(defined($file)) {
        my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
               $atime,$mtime,$ctime,$blksize,$blocks)
               = stat($file);

        defined($dev) || die("No such file $file");


        ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
                = localtime($mtime);
}

# Put the date string back into argv, split on spaces
unshift(@ARGV, split(/[ \t]+/, $input));

while(defined($arg=shift))
{
        # Need to split +1 into +1
        if($arg =~ /^[+]/) {    $sign=1;        $arg=substr($arg,1);    }
        elsif($arg =~ /^-/) {   $sign=-1;       $arg=substr($arg,1);    }

        ################## DATE FORMAT DETECTION ########################

        # @1234 means seconds in epoch time
        if($arg =~ /^@[0-9]+$/)
        {
                ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($1+0);
                next;
        }

        # Checks for YYYYMMDD or YYYY/MM/DD time
        # TODO:  Check for YYMMDD dates
        # TODO:  Check for YYYYDDMM dates (ugh)
        if($arg =~ /^([0-9]{4})(\/?)([0-9]{2})\2([0-9]{2})/)
        {
                ($year,$mon,$mday)=($1-1900,$3-1,$4+0);
                ($sec,$min,$hour)=(0,0,0);
                next;
        }

        # HH:MM:SS times
        if($arg =~ /([0-2][0-9]):([0-5][0-9]):([0-5][0-9])(.[0-9]+)?$/)
        {
                ($sec,$min,$hour)=($3+0, $2+0, $1+0);
                next;
        }

        # As last resort, assume its a pure number.
        if($arg =~ /^([0-9]+)$/) {
                if($sign == 0)
                {
                        print STDERR "offset without unit";
                        exit(1);
                }

                $offset=$1+0;
                next;
        }

        if($arg =~ /^seconds?$/)        {       }
        elsif ($arg =~ /^years?$/) {
                                        $year += ($offset*$sign);
                                        $sign=0;
                                        $offset=0;
                                }
        elsif($arg =~ /^minutes?$/)     {       $offset *= 60;          }
        elsif($arg =~ /^hours?/)        {       $offset *= 60*60;       }
        elsif($arg =~ /^days?/)         {       $offset *= 60*60*24;    }
        elsif($arg =~ /^weeks?/)        {       $offset *= 60*60*24*7;  }
        elsif($arg =~ /^months?$/)      {

                                        $mon += ($offset*$sign);

                                        while($mon > 12)
                                        {
                                                $mon-=12;
                                                $year++;
                                        }

                                        while($mon < 0)
                                        {
                                                $mon+=12;
                                                $year--;
                                        }

                                        $sign=0;
                                        $offset=0;
                                }
        elsif($arg =~ /^now$/)  {
                ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime();
        }
        elsif(length($arg) == 0){       } # Empty string?  Ignore
        else                    {
                                        print STDERR "Unknown argument $arg\n";
                                        exit(1);
        }
}

# Convert the altered year, month, etc back into epoch time.
my $ref=mktime($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
# Add the seconds offset calculated above.
$ref += ($sign * $offset);
# Convert back into list.
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($ref);

print strftime($cmdstr."\n",
        $sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
2 Likes

Another update, with more bugfixes, some code simplifications, and a --help option. I'm keeping the most up-to-date code in the OP now.

Hi.

Thanks for taking the time to do this.

I usually add warnings and strict to help avoid errors. Adding those yields:

Global symbol "$offset" requires explicit package name at ./date.pl line 99.  (about 10 times).

and

$ ./date.pl 
Use of uninitialized value $input in split at ./date.pl line 65.
Use of uninitialized value $offset in multiplication (*) at ./date.pl line 144.

The line numbers reflect the addition of lines for use strict; and use warnings;

Best wishes ... cheers, drl

1 Like

warnings and strict added, errors corrected. v0.0.6 now.

Here is v0.0.7, with a little more refined input detection. It can handle more informal dates and time, like:

$ ./gdate.pl -d "Mar 10, 2016 1:51:09 PM CST"

2016-03-10 13:51:09

$ ./date.pl -d "mon 2"

2016-05-02 10:43:46

$

Unknown tokens in the -d string are no longer a fatal error, it skips over them and only prints warnings given -v.

It's possible -- even likely -- that loosening the syntax has introduced unintended bugs, so I'm not putting it in the OP just yet. Any comments or suggestions?

#!/usr/bin/perl

use POSIX;
use strict;
use warnings;

my $cmdstr="%Y-%m-%d %H:%M:%S";
my ($input, $arg, $sign, $file,$offset)=("", undef, 0, undef,0);
my $verbose=0;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);

my %days = ( "sun" => 0, "sunday" => 0,
        "mon" => 1, "monday" => 1,
        "tue" => 2, "tues" => 2, "tuesday" => 2,
        "wed" => 3, "wednesday" => 3,
        "thu" => 4, "thurs" => 4, "thursday" => 4,
        "fri" => 5, "friday" => 5,
        "sat" => 6, "saturday" => 6 );

my %month = (   "jan" => 0, "january" => 0,"feb" => 1, "february" => 1,
                "mar" => 2, "march" => 2,"apr" => 3, "april" => 3,
                "may" => 4,"jun" => 5, "june" => 5,
                "jul" => 6, "july" => 6,"aug" => 7, "august" => 7,
                "sep" => 8, "sept" => 8, "september" => 8,
                "oct" => 9, "october" => 9,"nov" => 10, "november" => 10,
                "dec" => 11, "december" => 11 );

sub set { # Sets the big mess of time variables from epoch input
        ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(shift);
}

set(time);

# Commandline parsing stuff
while(defined($arg=shift)) {
        if($arg =~ /^--date=/)  {       $input=substr($arg, 7);         }
        elsif($arg eq "-v")     {       $verbose=1;                     }
        elsif($arg eq "-d")   {       $input=shift;                   }
        elsif($arg eq "-r")   {       $file=shift;                    }
        elsif($arg =~ /^--reference=/) {$file=substr($arg, 12);         }
        elsif($arg =~/^\+/)     {       $cmdstr=substr($arg,1);         }
        elsif($arg =~/^-h|--help|--version$/) {
                print STDERR <<"EOT";
date.pl v0.0.7, Tyler Montbriand, 2016.  Free PERL date calc/converter.

Usage:
 -d "time string"       string like "YYYYMMDD", "YYYY/MM/DD",
                        "HHMMSS", "HH:MM:SS", "\@epoch", "- 3 days",
                        "Mar 3 2016 1:16:09 AM",
                        etc.  You can string them together, like
                        "\@1343322750 - 3 days".

 -r /path/to/file       Show mtime of the given file, not current time.

 +"formatstring"        Give strftime this format string instead of the
                        default "%Y-%m-%d %H:%M:%S".  See 'man strftime'

 -v                     Verbose, warn when input formatting is ignored.

Examples:
 date.pl                        # current time in YYYY-MM-DD HH:MM:SS
 TZ="UTC" ./date.pl             # Use an alternate time zone
 date.pl +"%a %b %d %Y %r"      # Like Thu Jan 16 2014 12:58:59 PM
 date.pl -d "+ 3 days"          # Current time plus three days
 date.pl -d "\@1343322750"      # exact time in epoch seconds
 date.pl -d "2013/01/02 12:00:00"# exact time in YYYYMMDD HHMMSS
 date.pl -r /etc/passwd         # display mtime of /etc/passwd
 date.pl -r /etc/passwd -d "12:00:00" # date of /etc/passwd, time of noon
EOT
                exit(1);
        }
        else {       die("unknown argument $arg, try --help");   }
}

if(defined($file)) { # stat file to get mtime
        my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
               $atime,$mtime,$ctime,$blksize,$blocks)
               = stat($file);

        defined($dev) || die("No such file $file");

        set($mtime);
}

# Put the date string back into argv, split on any whitespace or grammar
unshift(@ARGV, split(/[ \r\n\t,]+/, $input));

while(defined($arg=shift))
{
        # Need to split +1 into +1
        if($arg =~ /^[+]/) {    $sign=1;        $arg=substr($arg,1);    }
        elsif($arg =~ /^-/) {   $sign=-1;       $arg=substr($arg,1);    }

        ################## DATE FORMAT DETECTION ########################

        # Month and date
        if(exists($month{tolower($arg)}) && ($ARGV[0] =~ /^[0-9]+$/) )
        {
                $mon=$month{tolower($arg)};
                $mday=shift;
                $mday = $mday + 0;
                next;
        }

        # Things like "Mon 12" are day of month
        if(exists($days{tolower($arg)})) {
                if($ARGV[0] =~ /^[0-9]+$/)
                {
                        $mday=$ARGV[0] + 0;
                        shift;
                }
                next;
        }

        # A bare four digit number beginning with 19 or 20 is probably a year
        if($arg =~ /^(19[0-9][0-9])|(2[0-9][0-9][0-9])$/)
        {
                $year=$arg - 1900;
                next;
        }

        # @1234 means seconds in epoch time
        if($arg =~ /^@([0-9]+)$/)      {        set($1+0);  next;        }

        # Checks for YYYYMMDD or YYYY/MM/DD time
        # TODO:  Check for YYMMDD dates
        # TODO:  Check for YYYYDDMM dates (ugh)
        if($arg =~ /^([0-9]{4})([\/-]?)([0-9]{2})\2([0-9]{2})/)
        {
                ($year,$mon,$mday)=($1-1900,$3-1,$4+0);
                ($sec,$min,$hour)=(0,0,0);
                next;
        }

        # HH:MM:SS times
        if($arg =~ /([0-2]?[0-9]):([0-5][0-9]):([0-5][0-9])(.[0-9]+)?$/)
        {
                ($sec,$min,$hour)=($3+0, $2+0, $1+0);

                # Handle time with PM in it
                if(tolower($ARGV[0]) eq "pm")
                {
                        shift;
                        if($hour >= 1) { $hour += 12; }
                }
                elsif(tolower($ARGV[0]) == "am")
                {
                        shift;
                        if($hour == 12) { $hour -= 12;  }
                }
                next;
        }

        # As last resort, assume its a pure number.
        if($arg =~ /^([0-9]+)$/) {
                ($sign != 0) ||
                die("offset without unit -- probably unrecognized format");

                $offset=$1+0;
                next;
        }

        if($arg =~ /^seconds?$/)   { } # Just take seconds at face value
        elsif($arg =~ /^minutes?$/)     {       $offset *= 60;          }
        elsif($arg =~ /^hours?/)        {       $offset *= 60*60;       }
        elsif($arg =~ /^days?/)         {       $offset *= 60*60*24;    }
        elsif($arg =~ /^weeks?/)        {       $offset *= 60*60*24*7;  }
        elsif($arg =~ /^months?$/)      {

                                        $mon += ($offset*$sign);

                                        while($mon > 12)
                                        {
                                                $mon-=12;
                                                $year++;
                                        }

                                        while($mon < 0)
                                        {
                                                $mon+=12;
                                                $year--;
                                        }

                                        $sign=0;
                                        $offset=0;
                                }
        elsif ($arg =~ /^years?$/) {
                                        $year += ($offset*$sign);
                                        $sign=0;
                                        $offset=0;
                                }
        elsif($arg =~ /^now$/)  {       set(time);      }
        elsif(length($arg) == 0){       } # Empty string?  Ignore
        elsif($verbose) {
                print STDERR "Unknown argument $arg\n";
        }
}

# Convert the altered year, month, etc back into epoch time.
my $ref=mktime($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);

# Print the calculated time plus offset
print strftime($cmdstr."\n", localtime($ref + ($sign*$offset)));
exit(0);
1 Like

Brilliant and versatile!

One thought - how about considering/including the locale s? Like

locale mon
Januar;Februar;M�rz;April;Mai;Juni;Juli;August;September;Oktober;November;Dezember
locale abmon
Jan;Feb;M�r;Apr;Mai;Jun;Jul;Aug;Sep;Okt;Nov;Dez

?

I'm not certain whether I want to depend on outside locale or even the Perl locale module; this script sees the most use as a kludge on old systems, and the only time locale generally comes up on unix.com is when people are fighting its problems. Parsing lists is a lot shorter than the hash syntax either way, though, so I'm happy to leave that choice to the user.

# loadhash(\%hash, "A;B", "a;b")
# sets $hash{A}=0, $hash{B}=1, $hash{a}=0, $hash{b}=1
sub loadhash {
        my ($h,$n)=(shift,0);
        foreach(@_)     { $n=0; foreach(split('[;\n]', lc($_))) { ${$h}{$_}=$n++; } }
}

#loadhash(\%month, `locale mon`, `locale abmon`);
loadhash(\%month, "January;February;March;April;May;June;July;August;September;October;November;December",
        "Jan;Feb;Mar;Apr;May;Jun;Jul;Aug;Sep;Oct;Nov;Dec");
#loadhash(\%days, `locale day`, `locale abday`);
loadhash(\%days, "Sunday;Monday;Tuesday;Wednesday;Thursday;Friday;Saturday",
        "Sun;Mon;Tues;Wed;Thurs;Fri;Sat");

Beginning to understand why people play 'perl golf' now, if the goal is to write a subroutine shorter than the 10 lines of repetition its to replace...!

Hi.

I think Corona688 and I have had a discussion about perl modules -- I tend to prefer to use them to make code shorter, and I think Corona688 likes his code to be as independent as possible.

I usually recommend this perl date code when the user (typically on older Solaris or AIX) cannot use GNU date or the dateuitls suite, so I definitely agree with Corona688's position in such situations.

Thanks again to Corona688 for taking the time to provide this.

Best wishes ... cheers, drl

A few bugfixes and improvements, such as the ability to ask it for "last week". It removes a lot of undefined behavior that happened whenever it had partial arguments. Base time and offsets are now calculated in different loops, and you can specify more than one offset now, like "last week + 5 hours - 3 minutes"

#!/usr/bin/perl

use POSIX;
use strict;
use warnings;

my $cmdstr="%Y-%m-%d %H:%M:%S";		# see strftime
my ($input, $arg, $sign, $ref,$offset,$quiet,$n)=("", undef, 0, time,0,0,0);
# $lt[SEC] is seconds, etc.  See perldoc -f localtime
use constant {	SEC=>0,	MIN=>1,	HOUR=>2,DAY=>3,	MON=>4,	YEAR=>5,WDAY=>6 };
# Titles for localtime() array elements
my @title=("Seconds","Minutes","Hours","Day","Month","Year","Weekday");
# Times stored in the format of 'perldoc -f localtime'.
# In the case of @changed, only elements that were altered in @lt are 
# set,other values are undefined.
my (@lt, @changed);
# Lookups for mon/tues/wed jan/feb/mar names into day and month numbers
my (%month, %days);
my %mult=("second" => 1, "minute" => 60, "hour" => 60*60, "day" => 60*60*24,
	"week"=>60*60*24*7, "year" => 31536000 );
# Alter a value in @lt and copy that value into the same index of @changed
sub set {
	my ($i,$v)=(shift,shift);
	$lt[$i]=$changed[$i]=$v;
}

# Takes an hour in AM, returns a number in 24-hour time
sub am { if($_[0] == 12) { return($_[0] - 12); } return($_[0]); }
# Takes an hour in PM, returns an hour in 24-hour time
sub pm { if($_[0] >= 1) { return($_[0] + 12); } return($_[0]); }

# loadhash(\%hash, "A;B", "a;b")
# sets $hash{A}=0, $hash{B}=1, $hash{a}=0, $hash{b}=1
sub loadhash {
        my ($h,$n)=(shift,0);
	foreach(@_)	{
		$n=0; foreach(split('[;\n]', lc($_))) { ${$h}{$_}=$n++; }
	}
}

# Parse commandline arguments
while(defined($arg=shift)) {
        if($arg =~ /^--date=/)  {       $input=substr($arg, 7);         }
        elsif($arg =~/^\+/)     {       $cmdstr=substr($arg,1);         }
	elsif($arg eq "-q")	{	$quiet=1;			}
        elsif($arg eq "-d")   {       $input=shift;                   }
        elsif(($arg eq "-r") || ($arg =~ /^--reference=(.*)$/))
	{
		if(defined($1))	{ $arg=$1; }
		else		{ $arg=shift; }
	        my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
        	       $atime,$mtime,$ctime,$blksize,$blocks)
	               = stat($arg);

	        defined($dev) || die("No such file $arg");

		$ref=$mtime;
	}
        elsif($arg =~/^-h|--help|--version$/) {
                print STDERR <<"EOT";
date.pl v0.0.8, Tyler Montbriand, 2016.  Free PERL date calc/converter.
 -d "time string"       string like "YYYYMMDD", "YYYY/MM/DD",
                        "HHMMSS", "HH:MM:SS", "\@epoch", "- 3 days",
			"Mar 3 2016 1:16:09 AM", 
                        etc.  You can string them together, like
                        "\@1343322750 - 3 days".

 -r /path/to/file       Show mtime of the given file, not current time.

 +"formatstring"        Give strftime this format string instead of the
                        default "%Y-%m-%d %H:%M:%S".  See 'man strftime'

 -q			date.pl warns you when given conflicting
			information, i.e. feb 29 not on a leap year.
			It also warns you when input isn't understood.
			-q suppesses this.
Examples:
date.pl                         # current time in YYYY-MM-DD HH:MM:SS
TZ="UTC" ./date.pl              # Use an alternate time zone
date.pl +"%a %b %d %Y %r"       # Like Thu Jan 16 2014 12:58:59 PM
date.pl -d "+ 3 days"           # Current time plus three days
date.pl -d "\@1343322750"       # exact time in epoch seconds
date.pl -d "2013/01/02 12:00:00"# exact time in YYYYMMDD HHMMSS
date.pl -d "9am"                # Today at 9am
date.pl -d "last week + 5 minutes"
date.pl -r /etc/passwd          # display mtime of /etc/passwd
date.pl -r /etc/passwd -d "12:00:00" # date of /etc/passwd, time of noon
EOT
                exit(1);
        }
        else {       die("unknown argument $arg, try --help");   }
}

# Load hashes full of day and month names

#loadhash(\%month, `locale mon`, `locale abmon`);
loadhash(\%month, "January;February;March;April;May;June;July;August;September;October;November;December",
	"Jan;Feb;Mar;Apr;May;Jun;Jul;Aug;Sep;Oct;Nov;Dec");
#loadhash(\%days, `locale day`, `locale abday`);
loadhash(\%days, "Sunday;Monday;Tuesday;Wednesday;Thursday;Friday;Saturday",
	"Sun;Mon;Tues;Wed;Thurs;Fri;Sat");

@lt=localtime($ref);

# Lowercase input so we can look up tokens like 'monday' in hashes
$input=lc($input);
# Separate strings and numbers into their own tokens, like "9am" => "9 am"
# : still belongs with numbers for HH:MM:SS etc.
$input =~ s/([0-9:])([a-z])/$1 $2/g;
$input =~ s/([a-z])([0-9:])/$1 $2/g;

# Split input on whitespace and commas and jam back into ARGV
unshift(@ARGV, split(/[ \r\n\t,]+/, $input));

while(!$sign && defined($arg=shift))
{
        # Need to split +1 into +, 1
        if($arg =~ /^[+]/) {
		$sign=1;
		if(length($arg) > 1) { unshift(@ARGV, substr($arg,1)); }
		next;
	}
        elsif($arg =~ /^-/) {
		$sign=-1;
		if(length($arg) > 1) { unshift(@ARGV, substr($arg,1)); }
		next;
	}

	if($arg eq "plus")	{	$sign=1;	next;	}
	if($arg eq "minus")	{	$sign=-1;	next;	}

        if(length($arg) == 0){ next } # Empty string?  Ignore

        ################## DATE FORMAT DETECTION ########################

        # Dates like "Jan" "17"
	if(exists($month{$arg}))
	{
		set(MON, $month{$arg});
		if(($#ARGV >= 0) && ($ARGV[0] =~ /^[0-9]+$/))
		{
			set(DAY, $ARGV[0] + 0);
			shift;
		}
		next;
	}

	# mon/monday/etc
	if(exists($days{$arg})) {
		set(WDAY, $days{$arg} );

		# If it's followed by a numeral, i.e. Monday 7,
		# the numeral is the day of the month
		if(($#ARGV >= 0) && ($ARGV[0] =~ /^[0-9]+$/))
		{ set(DAY, $ARGV[0] + 0); shift; }

		next;
	}

	# a bare 4-digit number beginning with 19 or 20 is probably a year
	if($arg =~ /^(19[0-9][0-9])|^(2[0-9][0-9][0-9])$/)
	{	set(YEAR, $arg - 1900);	next;	}

        # @1234 means seconds in epoch time
        if($arg =~ /^@([0-9]+)$/)      {
		@lt=localtime($1+0);
		# Date has been replaced, prev changes are now irrelevant
		for($n=0; $n<8; $n++) { $changed[$n]=undef; }
		next;
	}

        # Checks for YYYYMMDD or YYYY/MM/DD time
        if(($arg =~ /^([0-9]{4})([\/-])([0-9]{1,2})\2([0-9]{1,2})$/) ||
		($arg =~ /^([0-9]{4})()([0-9]{2})([0-9]{2})$/))
        {
		set(YEAR, $1-1900); set(MON, $3-1); set(DAY, $4+0);

		# Set time variables which haven't been set already
		if(!defined($changed[HOUR]))
		{ set(SEC,0); set(MIN,0); set(HOUR,0); }

                next;
        }

        # HH:MM:SS times.  Sub-second times are allowed but ignored
        if($arg =~ /([0-2]?[0-9]):([0-5][0-9])(:([0-5][0-9])(.[0-9]+)?)?$/)
        {
		if(defined($4)) { set(SEC, $4+0); }
		else		{ set(SEC, 0); }

		set(MIN, $2+0);	set(HOUR, $1+0);

		# Handle time with PM in it
		if($#ARGV < 0) { }
		elsif($ARGV[0] eq "pm")  { set(HOUR, pm($lt[HOUR])); shift; }
		elsif($ARGV[0] eq "am")  { set(HOUR, am($lt[HOUR])); shift; }
                next;
        }

        # Times like 9 AM
        if(($arg =~ /^([0-9]+)$/) && ($#ARGV>=0) && ($ARGV[0] =~ /^(am|pm)$/)) {
		$arg=$arg + 0;

		set(SEC, 0); set(MIN,0); set(HOUR, $arg);
		if($ARGV[0] eq "am") { set(HOUR, am($lt[HOUR])); }
		else                 { set(HOUR, pm($lt[HOUR])); }
		shift;
		next;
	}

	# Redundant, but whatever
        if($arg =~ /^now$/)  {
		@lt=localtime(time);
		# Date has been replaced, prev changes are now irrelevant
		for($n=0; $n<8; $n++) { $changed[$n]=undef; }
		next
	}

	# last second/minute/hour/week/year
	if($arg =~ /^(last)|(next)$/)
	{
		if(defined($1)) { $offset=-1; } else { $offset=1; }

		if($#ARGV < 0)
		{
			if(!$quiet)
			{	print STDERR "next what, exactly?\n"	}

			next;
		}

		$ARGV[0] =~ s/s$//;

		if(defined($mult{$ARGV[0]}))
		{
			@lt=localtime(time);
			# Date has been replaced, prev changes are now irrelevant
			for($n=0; $n<8; $n++) { $changed[$n]=undef; }
			$offset *= $mult{$ARGV[0]};
		}
		elsif($ARGV[0] eq "month")
		{
			$offset=0;
			$lt[MON] += $sign;

			if($lt[MON] >= 12) { $lt[YEAR]++; $lt[MON] -= 12; }
			if($lt[MON] < 0) { $lt[MON] += 12; $lt[YEAR] --; }

			set(MON, $lt[MON]);
			set(YEAR, $lt[YEAR]);
		}
		elsif(!$quiet)
		{
			print STDERR $ARGV[0]." not a valid option for next\n";
		}

		shift;
		next;
	}

        if(! $quiet) {
                print STDERR "Unknown argument $arg\n";
        }
}

# If a + / - sign was found, process the next arguments as the offset
while($sign && defined($arg=shift))
{
	# Might find another sign.
        if($arg =~ /^[+]/) {
		$sign=1;
		if(length($arg) > 1) { unshift(@ARGV, substr($arg,1)); }
		next;
	}
        elsif($arg =~ /^-/) {
		$sign=-1;
		if(length($arg) > 1) { unshift(@ARGV, substr($arg,1)); }
		next;
	}

	if($arg eq "plus")	{	$sign=1;	next;	}
	if($arg eq "minus")	{	$sign=-1;	next;	}

	# A number followed by a type, "9" "years"
	$arg =~ s/s$//;
	if(($arg =~ /^[0-9]+$/) && ($#ARGV >= 0))
	{
		# second/minute/hour/day/week/year are just multiplication
		$ARGV[0] =~ s/s$//g;

		if(defined($mult{$ARGV[0]}))
		{
			$offset += $mult{$ARGV[0]} * $arg * $sign;
			shift;
		}
		elsif($ARGV[0] =~ /months?/)	# months means counting
		{
			while($arg >= 12)
			{
				$arg -= 12;
				$offset += $sign * $mult{"year"};
			}

			$lt[MON] += $arg * $sign;
                        while($lt[MON] >= 12) { $lt[MON]-=12; $lt[YEAR]++; }
			while($lt[MON] < 0)  { $lt[MON]+=12; $lt[YEAR]--; }

			set(MON, $lt[MON]); set(YEAR, $lt[YEAR]);
			shift;
		}
		else
		{
			if(!$quiet) {
				print STDERR "Unknown element ".$ARGV[0]."\n";
			}
			shift;
		}

	}
}

# Convert the altered year, month, etc back into epoch time.
my $nref=mktime(@lt);
# Convert back into year, month, etc, to see if the input made sense.
my @san=localtime($nref);

for($n=0; $n<=6; $n++)
{
	if(($quiet == 0) && defined($changed[$n]) && ($changed[$n] != $san[$n]))
	{
		printf STDERR "%s changed, inconsistent input?", $title[$n];
		printf STDERR "\t%s in %s out\n", $changed[$n], $san[$n];
	}
}

# Print the calculated time plus offset
print strftime($cmdstr."\n", localtime($nref + ($offset)));
exit(0);

Here it is trimmed down and made far less ugly, with some bits moved into subs, subs moved down out of the way, and processing is now far less of a kludge in general. This is the next "official" version.

#!/usr/bin/perl

use POSIX;
use strict;
use warnings;

my $cmdstr="%Y-%m-%d %H:%M:%S";		# see strftime
my ($input, $arg, $sign, $ref,$offset,$quiet,$n)=("", undef, 0, time,0,0,0);
# $lt[SEC] is seconds, etc.  See perldoc -f localtime
use constant {	SEC=>0,	MIN=>1,	HOUR=>2,DAY=>3,	MON=>4,	YEAR=>5,WDAY=>6 };
# Times stored in the format of 'perldoc -f localtime'.
# In the case of @changed, only elements that were altered in @lt are 
# set,other values are undefined.
my (@lt, @changed);
# Lookups for mon/tues/wed jan/feb/mar names into day and month numbers
my (%month, %days);
# Lookup table to convert 'year' into a number of seconds, etc
my %mult=("second" => 1, "seconds" => 1, "minute" => 60, "minutes" => 60,
	"hour" => 3600, "hours" => 3600, "day" => 86400, "days" => 86400,
	"week" => 604800, "weeks" => 604800,
	"year" => 31536000, "years" => 31536000 );

# Parse commandline arguments
while(defined($arg=shift)) {
        if($arg =~ /^--date=(.*)/)  {       $input=$1;                  }
        elsif($arg eq "-d")         {       $input=shift;               }
        elsif($arg =~/^\+(.*)/)     {       $cmdstr=$1;                 }
	elsif($arg eq "-q")	    {	    $quiet=1;		        }
        elsif(($arg eq "-r") || ($arg =~ /^--reference=(.*)$/))
	{
		if(defined($1))	{ $arg=$1; }
		else		{ $arg=shift; }
	        my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
        	       $atime,$mtime,$ctime,$blksize,$blocks)
	               = stat($arg);

	        defined($dev) || die("No such file $arg");

		$ref=$mtime;
	}
        elsif($arg =~/^(-h|--help|--version)$/) {
                print STDERR <<"EOT";
date.pl v0.1.0, Tyler Montbriand, 2016.  Free PERL date calc/converter.
 -d "time string"       string like "YYYYMMDD", "YYYY/MM/DD",
                        "HHMMSS", "HH:MM:SS", "\@epoch", "- 3 days",
			"Mar 3 2016 1:16:09 AM", 
                        etc.  You can string them together, like
                        "\@1343322750 - 3 days".

 -r /path/to/file       Show mtime of the given file, not current time.

 +"formatstring"        Give strftime this format string instead of the
                        default "%Y-%m-%d %H:%M:%S".  See 'man strftime'

 -q			date.pl warns you when given conflicting
			information, i.e. feb 29 not on a leap year.
			It also warns you when input isn't understood.
			-q suppesses this.
Examples:
date.pl                         # current time in YYYY-MM-DD HH:MM:SS
TZ="UTC" ./date.pl              # Use an alternate time zone
date.pl +"%a %b %d %Y %r"       # Like Thu Jan 16 2014 12:58:59 PM
date.pl -d "+ 3 days"           # Current time plus three days
date.pl -d "\@1343322750"       # exact time in epoch seconds
date.pl -d "2013/01/02 12:00:00"# exact time in YYYYMMDD HHMMSS
date.pl -d "9am"                # Today at 9am
date.pl -d "last week + 5 minutes"
date.pl -r /etc/passwd          # display mtime of /etc/passwd
date.pl -r /etc/passwd -d "12:00:00" # date of /etc/passwd, time of noon
EOT
                exit(1);
        }
        else {       die("unknown argument $arg, try --help");   }
}

# Load hashes full of day and month names

#loadhash(\%month, `locale mon`, `locale abmon`);
loadhash(\%month, "January;February;March;April;May;June;July;August;September;October;November;December",
	"Jan;Feb;Mar;Apr;May;Jun;Jul;Aug;Sep;Oct;Nov;Dec");
#loadhash(\%days, `locale day`, `locale abday`);
loadhash(\%days, "Sunday;Monday;Tuesday;Wednesday;Thursday;Friday;Saturday",
	"Sun;Mon;Tues;Wed;Thurs;Fri;Sat");

@lt=localtime($ref);

# Lowercase input so 'tues', etc can be reliably found in tables
$input=lc($input); 
# Separate strings and numbers into their own tokens, like "9am" => "9 am"
# : still belongs with numbers for HH:MM:SS etc.
$input =~ s/([0-9:])([a-z])/$1 $2/g;
$input =~ s/([a-z])([0-9:])/$1 $2/g;
# Split +/- into their own tokens
$input =~ s/([+-])/ $1 /g;

# Split input on whitespace and commas and jam back into ARGV
unshift(@ARGV, split(/[ \r\n\t,]+/, $input));

while(defined($arg=shift))
{
        if(length($arg) == 0){ next; } # Empty string?  Ignore

	# Handle these in offset section
	if(($arg eq "+")||($arg eq "plus")||($arg eq "-")||($arg eq "minus"))
	{	unshift(@ARGV, $arg);	last;	}

        ################## DATE FORMAT DETECTION ########################

	if(exists($month{$arg})) # Dates like "Jan" "17"
	{
		set(MON, $month{$arg});
		if(($#ARGV >= 0) && ($ARGV[0] =~ /^[0-9]+$/))
		{	set(DAY, $ARGV[0] + 0);	shift;	}
		next;
	}

	if(exists($days{$arg}))	# mon/monday/etc
        {
		set(WDAY, $days{$arg} );

		# If it's followed by a numeral, i.e. Monday 7,
		# the numeral is the day of the month
		if(($#ARGV >= 0) && ($ARGV[0] =~ /^[0-9]+$/))
		{ set(DAY, $ARGV[0] + 0); shift; }

		next;
	}

	# a bare 4-digit number beginning with 19 or 20 is probably a year
	if($arg =~ /^(19[0-9][0-9])|^(2[0-9][0-9][0-9])$/)
	{	set(YEAR, $arg - 1900);	next;	}

        # @1234 means seconds in epoch time
        if($arg =~ /^@([0-9]+)$/)      {
		@lt=localtime($1+0);
		# Date has been replaced, prev changes are now irrelevant
		for($n=0; $n<8; $n++) { $changed[$n]=undef; }
		next;
	}

        # Checks for YYYYMMDD or YYYY/MM/DD time
        if(($arg =~ /^([0-9]{4})([\/-])([0-9]{1,2})\2([0-9]{1,2})$/) ||
		($arg =~ /^([0-9]{4})()([0-9]{2})([0-9]{2})$/))
        {
		set(YEAR, $1-1900); set(MON, $3-1); set(DAY, $4+0);

		# Set time variables which haven't been set already
		if(!defined($changed[HOUR]))
		{ set(SEC,0); set(MIN,0); set(HOUR,0); }

                next;
        }

        # HH:MM:SS times.  Sub-second times are allowed but ignored
        if($arg =~ /([0-2]?[0-9]):([0-5][0-9])(:([0-5][0-9])(.[0-9]+)?)?$/)
        {
		if(defined($4)) { set(SEC, $4+0); }
		else		{ set(SEC, 0); }

		set(MIN, $2+0);	set(HOUR, $1+0);

		# Handle time with PM in it
		if($#ARGV < 0) { }
		elsif($ARGV[0] eq "pm")  { set(HOUR, pm($lt[HOUR])); shift; }
		elsif($ARGV[0] eq "am")  { set(HOUR, am($lt[HOUR])); shift; }
                next;
        }

        # Times like 9 AM
        if(($arg =~ /^([0-9]+)$/) && ($#ARGV>=0) && ($ARGV[0] =~ /^(am|pm)$/)) {
		$arg=$arg + 0;

		set(SEC, 0); set(MIN,0); set(HOUR, $arg);
		if($ARGV[0] eq "am") { set(HOUR, am($lt[HOUR])); }
		else                 { set(HOUR, pm($lt[HOUR])); }
		shift;
		next;
	}

	# Redundant, but whatever
        if($arg =~ /^now$/)  {
		@lt=localtime(time);
		# Date has been replaced, prev changes are now irrelevant
		for($n=0; $n<8; $n++) { $changed[$n]=undef; }
		next
	}

	# last second/minute/hour/week/year
	if($arg =~ /^(last)|(next)$/)
	{
		if(defined($1)) { $sign=-1; } else { $sign=1; }

		if($#ARGV < 0)
		{
			if(!$quiet)
			{	print STDERR "next what, exactly?\n"	}

			next;
		}

		if(defined($mult{$ARGV[0]}))
		{
			@lt=localtime(time);
			# Date has been replaced, prev changes now irrelevant
			for($n=0; $n<8; $n++) { $changed[$n]=undef; }
			$offset *= $mult{$ARGV[0]};
		}
		# Adding months can't be handled in offset sadly
		elsif(($ARGV[0] eq "month") || ($ARGV[0] eq "months"))
		{	add_month($sign);	}
		elsif(!$quiet)
		{
			print STDERR $ARGV[0]." not a valid option for next\n";
		}

		shift;
		next;
	}

        if(! $quiet) { print STDERR "Unknown argument $arg\n"; }
}

# If there are any arguments left, we found a +/- and need to process
# that time offset.
while(defined($arg=shift))
{
        if(length($arg) == 0){ next; } # Empty string?  Ignore

	if(($arg eq "plus") || ($arg eq "+"))	{ $sign=1; next; }
	if(($arg eq "minus") ||($arg eq "-"))	{ $sign=-1; next; }

	# A number followed by a type, "9" "years"
	if(($arg =~ /^[0-9]+$/) && ($#ARGV >= 0))
	{
		my $arg2=shift;

		if((! $sign)&&(!$quiet))
		{
			print STDERR "Warning, no sign for numeric value\n"
		}

		# second/minute/hour/day/week/year are just multiplication
		if(defined($mult{$arg2}))
		{ $offset += $mult{$arg2} * $arg * $sign; next; }
		# No exact number of seconds per month, just count
		elsif(($arg2 eq "month")||($arg2 eq "months"))
		{ add_month($sign * $arg); next; }

		# Leave for error handler below to find
		$arg=$arg2;
	}

	# If we get here, something went wrong.
	if(!$quiet) {
		print STDERR "Unknown syntax ".$ARGV[0]."\n";
	}
}

my $nref=mktime(@lt); # Convert the altered @lt values back into epoch time

# Sanity checking.  If localtime(mktime(@lt)) produces different values
# from what was in @lt, we must have given it a nonsensical value which
# mktime corrected.
my @san=localtime($nref);

# Titles for localtime() array elements
my @title=("Seconds","Minutes","Hours","Day","Month","Year","Weekday");

for($n=0; $n<=6; $n++)
{
	if(($quiet == 0) && defined($changed[$n]) && ($changed[$n] != $san[$n]))
	{
		printf STDERR "%s changed, inconsistent input?", $title[$n];
		printf STDERR "\t%s in %s out\n", $changed[$n], $san[$n];
	}
}

# Print the calculated time plus offset
print strftime($cmdstr."\n", localtime($nref + ($offset)));
exit(0);

#########################################################################
################################ SUBROUTINES ############################
#########################################################################

# Adds or subtracts a number of months to the time in @lt,
# accounting for year wraparound when the number goes above 11 or below 0
sub add_month {
	$lt[MON] += shift;
	while($lt[MON] >= 12) { $lt[YEAR]++; $lt[MON] -= 12; }
	while($lt[MON] < 0) { $lt[MON] += 12; $lt[YEAR] --; }

	set(MON, $lt[MON]);
	set(YEAR, $lt[YEAR]);
}

# Alter a value in @lt, and mark that index as 'changed' by altering
# the value in @changed too
sub set { my ($i,$v)=(shift,shift); $lt[$i]=$changed[$i]=$v; }

# Takes a numeric AM hour, returns a number in 24-hour time
sub am { if($_[0] == 12) { return($_[0] - 12); } return($_[0]); }
# Takes a numeric PM hour, returns an hour in 24-hour time
sub pm { if($_[0] >= 1) { return($_[0] + 12); } return($_[0]); }

# loadhash(\%hash, "A;B", "a;b")
# sets $hash{A}=0, $hash{B}=1, $hash{a}=0, $hash{b}=1
sub loadhash {
        my ($h,$n)=(shift,0);
	foreach(@_)	{
		$n=0; foreach(split('[;\n]', lc($_))) { ${$h}{$_}=$n++; }
	}
}
1 Like

Hooray! I found a bug! add_months vs add_month. Fixed in v0.1.0.

1 Like

In another thread you say that strftime() should handle %s - but it does not in Solaris 10 and HP-UX 11.31!
I suggest to add a line in the perl code:

# Parse commandline arguments
while(defined($arg=shift)) {
        if($arg =~ /^--date=(.*)/)  {       $input=$1;                  }
        elsif($arg eq "-d")         {       $input=shift;               }
        elsif($arg =~/^\+(.*)/)     {       $cmdstr=$1;
           if ($cmdstr=~/(?<!%)%s/) { my $t=time; $cmdstr=~s/(?<!%)%s/$t/g; } }
        elsif($arg eq "-q")         {       $quiet=1;                   }
1 Like

Thanks for catching that.