#!/bin/perl ################################################################################ # jdate.pl # # Purpose: Calculates the julian date from date or vice-versa. # # Written by: Kelly Cash on November 30, 2001 # # # # Required files: getopt.pl - Hardcoded location below; find it on your system # # # # Switches: -2 - Specifies 2-character year eg: 01151 # # -4 - Specifies 4-character year eg: 2001151 # # Default is 2-character year format (5-char julian date) # # -r - Reverse mode: derive YYYYMODY date format from supplied jdate # # # # Args: YR MO DY - Specify the date to be converted to julian. # # Arguments are positionally dependent. # # Default is the current day. # # # # Notes: Command-line switches and arguments are all optional. # # # # Command-line switches must precede command-line arguments. # # # # If you specify -4 AND a date to convert, you'd better supply a # # 4-character year (eg: 1999) for me! (I'm not doing it for you) # # # # Reverse mode requires julian date in 4-char year format # # # # Reverse mode is mutually exclusive with all other switches and args # # # # Usage examples: jdate.pl -2 Returns today's jdate, 2-char year # # jdate.pl 99 10 26 Returns jdate for 10/20/1999 # # jdate.pl -4 02 01 01 Returns 20020101 # # jdate.pl -r 2001336 Returns 12/02/2001 # # # # Called by: check_in.pl # # check_in_daemon.pl # # # # Revision history: # # KAC 2001/11/30 Initial implementation # # KAC 2001/12/01 Added reverse mode functionality # # KAC 2001/12/29 Fixed bug in losing a digit from Jan 01 - Jan 09. # # (ex: 2002003 was incorrectly reported as 200203) # ################################################################################ require "/usr/local/lib/perl5/getopts.pl"; # open(DEBUG, ">jdate.debug"); # To calculate the jdate, all days of previous months must be added to day # J F M A M J J A S O N D @dayadd = (0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334); # ^-- Dummy to offset values to start at @dayadd[1] &Getopts('24r'); # -2, -4 and -r switches allowed # printf DEBUG "Switches are: 2: %s 4: %s r: %s\n", $opt_2, $opt_4, $opt_r; # printf DEBUG "Args are:%s %s %s %s %s %s %s %s %s %s %s %s\n", @ARGV; if ($opt_r ne "") { # Reverse mode. Take supplied switch argument and convert back to YYYYMODY. $jday = substr(@ARGV[0],4,3); $mo = 12; $mo-- while ($dayadd[$mo] >= $jday); $dy = $jday - $dayadd[$mo]; $dy = substr("0".$dy,-2,2); # zero-pad the day $mo = substr("0".$mo,-2,2); # zero-pad the month printf "%s/%s/%s\n", $mo, $dy, substr(@ARGV[0],0,4); } else { # Regular mode. Calculate julian date from today's date or supplied arguments. if (@ARGV[0] ne "") { # Arguments were passed, take them as the date $yr = @ARGV[0]; $mo = @ARGV[1]; $dy = @ARGV[2]; } else { # No args passed, use today's date $date = `date '+%Y%m%d'`; $yr = substr($date,0,4); $mo = substr($date,4,2); $dy = substr($date,6,2); } # If not returning a 4-character year, remove the leading two chars $yr = substr($yr,-2,2) if ($opt_4 == 0); # printf DEBUG "JDATE: Year:%s Month:%s Day:%s\n", $yr, $mo, $dy; $jdate = $dy + $dayadd[$mo]; $jdate = substr("00".$jdate,-3,3); # zero-pad the number if ($jdate > 59) { # We're past Feb 28th. Check and correct for leap year. # If year is evenly divisible by 4, add the leap day. $jdate++ if ($yr/4 == 0); } printf "%s\n", $yr.$jdate; } # close(DEBUG);