#!/opt/perl/bin/perl -w
use strict;
use File::Basename;
use Getopt::Long;
use POSIX qw(strftime);
use GPS::DATES ; #Dates and Time Extremely Simple
#get this program name
my $progname = basename ($0);
my $now =strftime "%Y-%m-%d:%H:%M:%S", localtime;
my $usage=<<EOT;

$progname calculates and return a formated string with a date.

The date formats that are recognized are listed below.
if $progname can't find a match for the date given it will try
to guess based on the string format. If the date is ambiguous,
let say 3 digit numbers that are less than 12, the $progname
will warn that is guessing based on the order month/day/year.


Usage:
$progname [-in DATE[:H:M:S]] [-out TYPE,MOD] [-doy] [-cal ] [-gwd ]
	  [-today|-now]
	  [-help]
DATE: YYYY-MM-DD; YYMMMDD; YYYY,DOY; WWWWD	  
TYPE: cal,doy,epoch,gwd,gpsw,gdow,gwsec,sj2000,jd,mjd,wday 
MOD : num,int,sp,time

Examples: 
	$progname -today -o gwd        or $progname -today -gwd  
	$progname -i 2002-7-29 -o gwd      
	$progname -i 2002,210:12  -o jd 
	$progname -year 2002 -doy 210 -o dy 
	$progname -i 2002-07-29 -o doy  or $progname -i 2002-07-29 -doy 

EOT
my $author =<<FIN;
TODAY $now

Marcelo Santillan, Aug. 2005
Geodesy Lab & PANGA
Central Washington University

FIN

my $usage_options = qq {
Options
-------
-in : $progname will recognize the following date formats:

DATE          | Example
--------------------------
year,doy      : 2005,192
decimal_year  : 2005.52498
calendar      : 2005-07-11
gpsweek_dow   : 13311
epoch         : 05JUL11
julian_date   : 2453563.0  (must be a real number) 
modified_jdate: 53562.5    (must be a real number and less than MJD0=2400000.5)  

Each of the previous formats can be appended using
':' to provide the hours,minutes and seconds.

hours:min:sec : 2005,192:11:30:45


-out  TYPE,MOD 
$progname will output a single line given the
following types 
TYPE  | Output      
-------------------------------------
cal   : YYYY-MM-DD , Gregorian calendar
doy   : YYYY DDD   , Year DOY
snx   : YY:DOY:SEC , SINEX DATE
wday  : Week_day   , (Monday,Tuesday...)
jd    : JD         , Julian date	
mjd   : MJD        , Modified Julian date	
dy    : YYYY.FFFF  , decimal year
epoch : YYMMMDD    , gipsy epoch (for stacov files )
gpsw  : WWWW       , GPS WEEK
gdow  : D          , GPS day of week
gwd   : WWWWD      , gpsw+gdow
gwsec : SSSSS      , GPS WEEK seconds
sj2000: SSSSS      , Seconds since J2000
dd    : delta_days , DATE_now - DATE (requested)
dh    : delta_hours, 
dm    : delta_mins , 
ds    : delta_secs ,

TYPE can be modified by using one or more of 
the following modifieres:

MOD   | Action
-------------------------------------
yesterday : show today - 1 day time as cal:h:m:s
today     : show current time as cal:h:m:s
tomorrow  : show today + 1 day time as cal:h:m:s
now       : same as today
sp        : separate fields with 1 space
time      : append h:m:s to the output date.
num       : display DATE as an unique integer when 
            possible (except: today,now and snx)
int       : format seconds as 2 digit integers 
short     : format seconds with 2 decimal places

In addition $progname recognizes the following shortcuts:

sta = cal,time,sp,short  ( sta_svec date )

Example: 
	this gives the cal format,appended with h:m:s but 
	separated with spaces:
	$progname -i 2002,192:12:30:45  -o cal,time,sp 

	make an integer out of cal and h:m:s
	$progname -i 2002,192:12:30:45  -o cal,time,int 

};




my $help='';
my $date = '';  # generic date
my ($doy,$cal,$gwd)=('')x3;
my $sec = undef;
my $today=0;
my $yesterday=0;
my $tomorrow=0;
my $out='';
my $delta = 0;
my $args = @ARGV;


#Get command line options
GetOptions( "today"		=> \$today,
	    "now"		=> \$today,
        "yesterday" => \$yesterday,
        "tomorrow"  => \$tomorrow,
	    "out=s"		=> \$out,
	    "in=s"		=> \$date,
        "sec=s"     => \$sec,
        "doy"       => \$doy,
        "cal"       => \$cal,
        "gwd"       => \$gwd,
        'delta=s'   => \$delta, 
        '<>'        => \&select_output,
	    "help"		=> \$help
	   ) || die "error in options";


if ( ! $args )    { die "$usage\n$author\n" };
if (  $help )     {  print "$usage\n$usage_options\n$author\n";exit; }
if ( $today )     { $date=$now; }
if ( $date =~ /yesterday/i || $yesterday ) { $date = $now;  $delta=-1; }
if ( $date =~ /tomorrow/i  || $tomorrow  ) { $date = $now;  $delta=1;  }
$out = 'doy' if $doy;
$out = 'cal' if $cal;
$out = 'gwd' if $gwd;
if ( $sec ) {
  $date=join ' ',GPS::DATES::sec2cal($sec);
}
if ( $date )  {
 $date = get_date($date,$out,$delta);   	
 print "$date\n"; 
 exit;
}

while ( $date = <> ) {
  chop $date;
  $date=sprintf "%4d-%02d-%02d %02d:%02d:%f",GPS::DATES::sec2cal($date) if $sec;
  print scalar  get_date($date,$out,$delta),"\n";
}

sub select_output {
  $out = shift;
}
