Important alert: (current site time 7/15/2013 9:14:49 PM EDT)
 

article

CalcDayNumber

Email
Submitted on: 4/28/2003 4:57:37 PM
By: Jerome A. Simon  
Level: Beginner
User Rating: By 1 Users
Compatibility: 5.0 (all versions)
Views: 10211
author picture
 
     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.

This article has accompanying files
 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. 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.
  2. 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.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. 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++;
 }

 # default is 4 digit year (2003) ... but

 # allow 2 digit year ( 03)

 $tYear += 100 if( $tYear < 100);

 # allow 3 digit year (from localtime())
 $tYear += 1900 if( $tYear < 1900);

 for( $tCount = 1970; $tCount < $tYear; $tCount++) {
  $tDayNumber += 365;
  $tDayNumber++ unless( $tCount % 4);
 }

 for( $tCount = 0; $tCount < $tMon; $tCount++) {
  $tDayNumber += $daysInMonth[ $tCount];
  # March is effected by leap year
  if( $tCount == 1) {
   $tDayNumber++ unless( $tYear % 4);
  }
 }

 $tDayNumber += $tDay;

 # Returns Days since Jan 1, 1970
 return( $tDayNumber - 1);
} # sub CalcDayNumber


winzip iconDownload article

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:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. 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...   
  1. 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.
  2. 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.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.


Other 7 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

8/4/2004 4:05:50 PM

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.)

 
8/4/2004 5:17:14 PMJerome A. Simon

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.)
 

To post feedback, first please login.