Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl date check 2

Status
Not open for further replies.

wallflower

Programmer
Nov 8, 2002
4
US
I need a little help.
i need to stick a simple "experation date" is less than "today date" perl on an already existing .pl.
IF variable is less that today, do nothing, if its greater, I'll redirect them.

I know, I know, its probably super simple and I just need to learn Perl, but this .pl got shoved on my desk and I need a quick fix.

Perl is on my list of lanuages to learn, but still have so much ASP to go.

 
ok, sounds easy -- what's in the variable you need to compare to "today"? Mike

"Experience is the comb that Nature gives us, after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Hi wallflower,

the simple way is to convert the dates into a string of the format 'YYYY-MM-DD' and use the alphabetical compare operators (gt= greater then; lt= less then; eq= equal etc.).

If you would also need to know the number of days between the two given dates you may wana use the below code, which inculdes functions to convert the date into a such called julian date format (represents the number of days since 1. Jan. 4712 B.C.). On the other hand it also includes functions to convert a julian date into a calendar date.

#! c:/indigoperl/bin/Perl

use strict;
use Time::localtime;

my $TodayCD; #Used to hold the current calendar date
my $TodayJD; #Used to hold the current julian date value

my $PastDateCD='1995-10-11';
my $PastDateJD=CD2JD($PastDateCD);

my $FutureDateCD='2100-10-11';
my $FutureDateJD=CD2JD($FutureDateCD);

$TodayCD=(localtime->year()+1900) . "-";
$TodayCD=$TodayCD . sprintf("%02d",(localtime->mon()+1)) . "-";
$TodayCD=$TodayCD . sprintf("%02d",localtime->mday());

$TodayJD=CD2JD($TodayCD);

print "The PastDateCD is past since " . ($TodayJD-$PastDateJD) . " days\n";
print "The FutureDateCD is about to come in " . ($FutureDateJD-$TodayJD) . " days\n";

exit;

#===========================================================
# MonthFromJD(JulianDay); -> Calculates the Month of a
# Julian Date
#===========================================================
sub MonthFromJD
{
my $JD=$_[0];
my @Date;
my $Month;
my $CD;

$CD = JD2CD($JD);
@Date = split(/-/,$CD);
$Month = $Date[1];

return $Month;
}

#===========================================================
# DayFromJD(JulianDay); -> Calculates the Day of a Julian
# Date
#===========================================================
sub DayFromJD
{
my $JD=$_[0];
my @Date;
my $Day;
my $CD;

$CD = JD2CD($JD);
@Date = split(/-/,$CD);
$Day = $Date[2];

return $Day;
}

#===========================================================
# YearFromJD(JulianDay); -> Calculates the Year of a Julian
# Date
#===========================================================
sub YearFromJD
{
my $JD=$_[0]; #Assign input parameter to $JD
my @Date;
my $Year;
my $CD;

#Convert the Julian Day into a calendar date of the
#format 'YYYY-MM-DD'

$CD = JD2CD($JD);

#Use '-' as delimiter to split the date into its 3 parts.

@Date = split(/-/,$CD);

#Use the first part as return value

$Year = $Date[0];

return $Year;
}

#===========================================================
# JD2CD(JulianDay); -> Converts a Julian Date into a
# Calendar Date (e.g. "2002-10-15")
#===========================================================
sub JD2CD
{
my $JD=$_[0]; #Assign input parameter to $JD

#Some temporary variables
my $Z;
my $A;
my $Alpha;
my $B;
my $C;
my $D;
my $E;

#Variables for the resulting date
my $Day;
my $Month;
my $Year;
my $RetVal;

$Z = int($JD + 0.5);

#Base calculation of A

if ($Z < 2299161)
{
$A = $Z;
}
else
{
$Alpha = int(($Z - 1867216.25) / 36524.25);
$A = $Z + 1 + $Alpha - int($Alpha / 4);
}

#Calculate temporary values

$B = $A + 1524;
$C = int(($B - 122.1) / 365.25);
$D = int(365.25 * $C);
$E = int(($B - $D) / 30.6001);

#Calculate the day of month incl. the time fraction

$Day = $B - $D - int(30.6001 * $E);

#Calculate the month of the year

if ($E < 14)
{
$Month = $E - 1;
}
else
{
$Month = $E - 13;
}

#Calculate the year

if ($Month > 2)
{
$Year = $C - 4716;
}
else
{
$Year = $C - 4715;
}

#Calculate the calendar date

$RetVal = $Year . &quot;-&quot; . sprintf(&quot;%02d&quot;,$Month) . &quot;-&quot; . sprintf(&quot;%02d&quot;,$Day);

return $RetVal;
}


#===========================================================
# DayOfWeek(JulianDay); -> Day of the week (e.g. &quot;Mo.&quot;)
#===========================================================
sub DayOfWeek
{
my $RetVal=$_[0];

$RetVal +=1.5;
$RetVal %= 7; #Get the remainder of the modulu.

#Assign a weekday name (the example is in german language).
return (&quot;So.&quot;,&quot;Mo.&quot;,&quot;Di.&quot;,&quot;Mi.&quot;,&quot;Do.&quot;,&quot;Fr.&quot;,&quot;Sa.&quot;)[$RetVal];
}

#===========================================================
# CD2JD(year, month, day); -> Converts a Calendar Date into
# a Julian Date
#
# The parameter for month and day are optional,
# if omitted, the function assumes the date to be
# stored in the year parameter, using the format
# 'YYYY-MM-DD'.
#===========================================================
sub CD2JD
{
my $Year=$_[0];
my $Month=$_[1]+0;
my $Day=$_[2]+0;
my @Date;
my $JD;
my $A;
my $B;
my $DateVal;
my $RetVal;

if (($Month == 0) && ($Day == 0))
{
@Date=split(/-/,$Year);
$Year=$Date[0];
$Month=$Date[1]+0;
$Day=$Date[2]+0;
}

$DateVal=$Year;
$DateVal.=&quot;-&quot;;
$DateVal.=sprintf(&quot;%02d&quot;,$Month);
$DateVal.=&quot;-&quot;;
$DateVal.=sprintf(&quot;%02d&quot;,$Day);

#moves the start of the year to the first of march

if ( $Month > 2)
{
$Year = $Year;
$Month = $Month;
}
else
{
$Year -= 1;
$Month += 12;
}

#Check if the date falls into the julian calendar (older
#calendar used until 04.10.1582) or into the gregorian
#calendar(newer calendar used since 15.10.1582)

if ($DateVal gt &quot;1582-10-15&quot;)
{
$A = int($Year / 100);
$B = 2 - $A + int($A / 4);
}
else
{
$B = 0;
}

#Returns a double number with the days passed since -4712
#Jan. 0.0

$RetVal = int(365.25 * ($Year + 4716)) + int(30.6001 * ($Month + 1)) + $Day - 1524.5 + $B;
return $RetVal;
}
 
I can't better that OpenWater, useful routines there; what about turning that into a FAQ?

&quot;How do I do Julian date calculations using Perl?&quot; Mike

&quot;Experience is the comb that Nature gives us, after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top