#!/usr/bin/perl -w
#
#  Use:
#    date2second hour:min:sec [ monthday [ month [year]]]
#  converts a date ($hour,$min,$sec,$monthday,$month,$year)
#  into the number of seconds elapsed since 1.1.1970.
#  (For further help call without arguments)
#
#  This does the inverse:
#    perl -e 'print scalar(localtime(<time_t value>)), "\\n";
# ---------------------------------------------------------------------
# File = Tools/date2second,
# part of tcharge, version 1.3, 11MAY98
# Copyright (C) 1997,98 Volker B"orchers (boercher@physik.uni-bremen.de)
# See file `tcharge.c' for legal affairs.

require "timelocal.pl";

usage("-> Missing Argument!\n") if($#ARGV < 0);

# Get default values: now
($sec,$min,$hour,$monthday,$month,$year) = localtime;
sub usage {
    warn @_, "Use:\ndate2second hour:min:sec [ monthday [ month [year]]]\n",
    " Args in () are optional and default to current value (this moment).\n",
    " Converts a date (sec,min,hour,monthday,month,year) into the\n",
    " number of seconds elapsed since 1.1.1970:\n",
    " Ranges: year[0,99], month[1,12], monthday[1,31], hour[0,23]\n";
    exit 1;
}

$year	= $ARGV[3] if(defined $ARGV[3]);
$month	= $ARGV[2] -1 if(defined $ARGV[2]);  # month[1,12]!
$monthday = $ARGV[1] if(defined $ARGV[1]);
($hour,$min,$sec) = ($ARGV[0] =~ m;(\d+)[:/.,](\d+)[:/.,](\d+););
		
usage("@ARGV; date2second: Month \"", $month+1, "\" out of range 1..12\n")
    if $month > 11 || $month < 0; # note: $month decremented
usage("@ARGV; date2second: Day \"$monthday\" out of range 1..31\n")
    if $monthday > 31 || $monthday < 1;
usage("@ARGV; date2second: Hour \"$hour\" out of range 0..23\n")
    if $hour > 23 || $hour < 0;
usage("@ARGV; date2second: Minute \"$min\" out of range 0..59\n")
    if $min > 59 || $min < 0;
usage("@ARGV; date2second: Second \"$sec\" out of range 0..59\n")
    if $sec > 59 || $sec < 0;

print timelocal($sec,$min,$hour,$monthday,$month,$year), "\n";
