#!/usr/bin/perl # # NAME OF SCRIPT FILE: # day1900 # # DESCRIPTION # Converts a date to the day number from 1900-01-00. # If one parameter is specified, it should be in form YYMMDD or YYYY-MM-DD. # If three parameters are specified they should be in order of year, # month, day. Year can be 19YY or YY. Month should be month number or # 3 letter abbreviation. Uses today if no date is specified. # # USAGE: # day1900 YYMMDD # day1900 YYYY-MM-DD # day1900 (YY|19YY) (MM|mmm) DD # # HISTORY: # nash 921027 written # nash 950811 added YYYY-MM-DD processing, cleaned up code # $Header: /var/local/src/tmp/day1900,v 1.4 1995/08/11 21:28:12 root Exp $ # # use today as default if (!@ARGV) { ($dy,$dy,$dy,$dy,$mo,$yr,@junk) = localtime(time); $mo++; } # get year, month, and day from full date elsif ($#ARGV == 0) { $date = shift; if ($date =~ /-+/) { ($yr,$mo,$dy) = ($date =~ /\D*(\d+)-(\w+)-(\d+)/); } else { ($yr,$mo,$dy) = ($date =~ /\D*(\d+)(\d\d)(\d\d)/); } } # get year, month, and day from separate parameters elsif ($#ARGV == 2) { $yr = shift; $mo = shift; $dy = shift; } # bad number of parameters else { die "Incorrect number of parameters: must be 0, 1, or 3\n"; } # month given as characters if (length($mo) eq 3) { $mo =~ tr/[A-Z]/[a-z]/; @mlist{'jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov', 'dec'} = (1,2,3,4,5,6,7,8,9,10,11,12); ($mo = $mlist{$mo}) || die "Month is incorrectly specified\n"; } # check ranges if ($yr > 1899) { $yr = $yr-1900; } if ($yr < 0) { $yr = 0; } if ($mo < 1) { $mo = 1; } if ($mo > 12) { $mo = 12; } $mo--; if ($dy < 1) { $dy = 1; } @mlist = (0,31,59,90,120,151,181,212,243,273,304,334,365); @difflist = (31,28,31,30,31,30,31,31,30,31,30,31); # leap year $leap = (($yr-int($yr/4)*4) == 0); $leap = ($leap & ($yr != 0)); $dmax = $difflist[$mo]+($leap & ($mo == 1)); if ($dy > $dmax) { $dy = $dmax; } $yrmod = ($mo+1)*100+$dy; # result print $yr*365+int(($yr-1)/4)+(($yrmod > 300) & $leap)+$mlist[$mo]+$dy,"\n"; exit;