Perl is able to track the seconds since Jan 1, 1970 - so calculating the DayNumber from the seconds is real easy: Seconds / secondsPerDay. But what if you don't start with the seconds. What if what you have is a Date. How do you know what day it is? CalDayNumber() will tell you.
Terms of Agreement:
By using this article, you agree to the following terms...
You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
You may link to this article from another website, but ONLY if it is not wrapped in a frame.
You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
But Why? I am writing a Calendar script and I need to allow the user to enter a Date, and display weeks before and after the date. The script I wrote uses localtime() to tell me when the month/year changes as I count off the DayNumber... So, I need to know the starting DayNumber based on the users request.
# USAGE:
# DD, MM, YYYY
my $dayNumber = CalcDayNumber( 28, 3,
2003);
# -OR-
# year value from localtime()
my $dayNumber = CalcDayNumber( 28, 3,
103);
# -OR-
# DD, MM, YY
my $dayNumber = CalcDayNumber( 28, 3,
03);
# NOTE:
# Month value is 0 - 11 (not 1-12)
# These values are those used in localtime()
sub CalcDayNumber { #
$tDaY, $tMon, $tYear
my ( $tDay, $tMon, $tYear) = @_;
my $tDayNumber = 0; # Days
SINCE Jan 1, 1970
my $tCount;
# NOTE: since the seconds value tops out
# around 2034, the 100th Year
# check for leap year is NOT performed!
# ..And since every 1000 years
is a
# leap year 2000 is not effected
# ie. this routine is only valid for
# dates between 1/1/1970 - 12/31/2034
# Fix $tMon ( it might be more than 0-11)
# and adjust $tYear
until( $tMon < scalar( @daysInMonth)) {
$tMon -= scalar( @daysInMonth);
$tYear++;
}
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come
out every day, so no prevention program can catch 100% of them. For your own safety, please:
Re-scan downloaded files using your personal virus checker before using it.
NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
If you don't have a virus scanner, you can get one at many places on the net
including:McAfee.com
Terms of Agreement:
By using this article, you agree to the following terms...
You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
You may link to this article from another website, but ONLY if it is not wrapped in a frame.
You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
Dear, I was very pleased to find your script onto Planet-Source-Code. But it wil not work, it seems to go into a loop. I used without other code (exept a 'print' statement) with Perl 5.6. The script you wrote was exactly wat i need. Do you have a solution for this? Regards Jan, newbie (If this comment was disrespectful, please report it.)
8/4/2004 4:09:19 PM:
Dearest, This script you wrote is exactly wat i'm trying to do, but it wil not work. It seems to go in a loop. I used it in bare form on Perl 5.6 Can you help? Jan, newbie from Belgium (If this comment was disrespectful, please report it.)
The script is a routine. It should be called with the target date. Inside the routine, there is a loop... I would have to see what you tried to better understand your question?
USAGE: print "The day number for 28/04/2004 is " . CalcDayNumber( 28, 4, 2004) . "\n"; (If this comment was disrespectful, please report it.)
10/31/2004 2:25:47 PM:
Jan and Jerome I added the necessary whistles and bells so that it works as a standalone (not a lot of validation though) for code email me. (If this comment was disrespectful, please report it.)
10/31/2004 2:27:11 PM:
Last comment sent by me b4 reg process was complete.
(If this comment was disrespectful, please report it.)
Add Your Feedback
Your feedback will be posted below and an email sent to
the author. Please remember that the author was kind enough to
share this with you, so any criticisms must be stated politely, or they
will be deleted. (For feedback not related to this particular article, please
click here instead.)