Important alert: (current site time 10/30/2014 10:03:29 AM EDT)
 

VB icon

Calculate Daylight Saving Time

Email
Submitted on: 7/24/2003 7:18:53 PM
By: Randy McCleary 
Level: Intermediate
User Rating: Unrated
Compatibility: 5.0 (all versions), Active Perl specific
Views: 23923
(About the author)
 
     This is a subroutine that will calculate Daylight Saving Time for the US and EU. This will return the day that DST starts on or ends on based upon the input data.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code 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 code (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 code 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 code or code's description.
				
=**************************************
= Name: Calculate Daylight Saving Time
= Description:This is a subroutine that will calculate Daylight Saving Time for the US and EU. This will return the day that DST starts on or ends on based upon the input data.
= By: Randy McCleary
=
=This code is copyrighted and has= limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=499&lngWId=6=for details.=**************************************

#!/usr/bin/perl -w
##########################################
## Daylight Savings Time in the US and EU
##########################################
use strict;
my ($intDay, $Year) = 0;
my ($Country, $BeginEnd) = '';
$intDay = &DaylightSavings(2003, 'US', 'Begin');
print "************************************************\n";
print "*** Calculate Daylight-Saving Time ***\n";
print "************************************************\n";
print "1. Year - Must be 4 Digits\n";
print "2. Country - Must be US or EU\n";
print "3. Begin/End - Must be Begin or End; 1 or 2\n";
print "************************************************\n\n";
print "1. Enter the Year: ";
chomp ($Year = <STDIN>);
print "2. Choose US or EU: ";
chomp ($Country = uc(<STDIN>));
print "3. Choose Begin or End: ";
chomp ($BeginEnd = ucfirst(lc(<STDIN>)));
$intDay = &DaylightSavings($Year, $Country, $BeginEnd);
if ($intDay == 0) {
	print "An Invalid Calculation was entered. Make sure\n";
	print "A valid 4 Digit Year was used, along with US or EU\n";
	print "was chosen, and the Begin or End was chosen\n";
}
if ($Country eq 'US') {
	if ($BeginEnd eq 'Begin' || $BeginEnd eq 1) {
		print "\nDaylight-Savings Begins on: ";
		print "Sunday, April $intDay, $Year\n";
	}
	elsif ($BeginEnd eq 'End' || $BeginEnd eq 2) {
		print "\nDaylight-Savings Ends on: ";
		print "Sunday, October $intDay, $Year\n";
	}
}
elsif ($Country eq 'EU') {
	if ($BeginEnd eq 'Begin' || $BeginEnd eq 1) {
		print "\nDaylight-Savings Begins on: ";
		print "Sunday, March $intDay, $Year\n";
	}
	elsif ($BeginEnd eq 'End' || $BeginEnd eq 2) {
		print "\nDaylight-Savings Ends on: ";
		print "Sunday, October $intDay, $Year\n";
	}
}
print "\n\n";
#####################################################
## Sub: DaylightSavings
## Description: This sub calculates Daylight Savings
##Based on the Year, Country, Begin(1) or End(2) 
##that is passed to the subroutine.
## Examples:
##$intDay = &DaylightSavings(2003, 'US', 'Begin');
##$intDay = &DaylightSavings(2003, 'EU', '1');
#####################################################
sub DaylightSavings {
	my $intYear = shift;
	my $strCountry = shift;
	my $strBeginEnd = shift;
	my $intDay = 0;	
	if ($strCountry eq 'US') {
		if ($strBeginEnd eq "Begin" || $strBeginEnd eq 1) {
			$intDay = ((2+6*$intYear-int($intYear/4)) % 7+1);
		}
		elsif ($BeginEnd eq "End" || $strBeginEnd eq 2) {
		$intDay = (31-($intYear*5/4+1) % 7);
	}
	else {
		$intDay = 0;
	}
}
elsif ($strCountry eq 'EU') {
	if ($strBeginEnd eq "Begin" || $strBeginEnd eq 1) {
		$intDay = (31 - (5*$intYear/4 + 4) % 7);
	}
	elsif ($strBeginEnd eq "End" || $strBeginEnd eq 2) {
		$intDay = (31 - (5*$intYear/4 + 1) % 7);
	}
	else {
		$intDay = 0;
	}
	}
	else {
		$intDay = 0;
	}
	### Return the Day
	return $intDay;
}


Other 28 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 code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

7/29/2003 12:07:29 PMDamian myerscough

Hi,

I am Starting a Group up Called WebPerland
Which will hopfully be a bunch of Perl programmers and Graphics Designers The group will be an open source one and we will all work together we will write tutorials, program etc if you are interested please could you email me back thanks
(If this comment was disrespectful, please report it.)

 
7/31/2003 7:30:50 AMstephen antony

see my open souce perl site
www.codepark.tk
(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 code, please click here instead.)
 

To post feedback, first please login.