Daylight Saving Time (DST) within one country

Discuss features as they are added to the new version. Give us your feedback. Don't post bug reports, feature requests, support questions or suggestions here.
Forum rules
Discuss features as they are added to the new version. Give us your feedback. Don't post bug reports, feature requests, support questions or suggestions here. Feature requests are closed.
Post Reply
User avatar
CodeBuster
Registered User
Posts: 22
Joined: Fri Jul 16, 2004 9:48 pm
Location: The Netherlands
Contact:

Re: Daylight Saving Time (DST) within one country

Post by CodeBuster »

basicly you could use the good ol country mod (or flags mod).. with 2 additional fields, concerning yes or no and when.. hmm.. :D

it's keep track of that timezone info.. which country when.. must be a website or something about it somewhere where you could pull that off..
place witty phrase here

geocator
Registered User
Posts: 100
Joined: Fri Jan 09, 2004 11:45 pm

Re: Daylight Saving Time (DST) within one country

Post by geocator »

And then you have to deal with the extra processing time to figgure all this out. It would be considerable.

Mvastango
Registered User
Posts: 30
Joined: Wed Jun 09, 2004 12:00 am

Re: Daylight Saving Time (DST) within one country

Post by Mvastango »

I'm not too sure about that. I just did this proof-of-concept (feel free to use in a mod) and it seems to be going pretty quick. Granted, I'm testing it on a pretty fast server...

Code: Select all

<?php
//define time zone info
$TZ = array(
	'timezone_name' => 'New York/US',
	'timzone_normal_offset' => -5,
	'timzone_observes_dst' => true,
	'timzone_dst_offset' => -4,
	'timezone_dst_start' => 'strtotime("Next Sunday", strtotime("April First $year -1 Day"))',
	'timezone_dst_end' => 'strtotime("Last Sunday", strtotime("September First $year"))'
);
$timezone_dst_start_cache = array();
$timezone_dst_end_cache = array();
function getAdjustedTimestamp($timestamp)
{
	global $TZ;
	if($TZ['timzone_observes_dst'])
	{
		$dst_dates = getDatesForYear(date('Y', $timestamp));
		$reverse = ($dst_dates['end'] < $dst_dates['start']);
		if ($reverse)
		{
			$start = $dst_dates['end'];
			$end = $dst_dates['start'];
		} else {
			$start = $dst_dates['start'];
			$end = $dst_dates['end'];
		}
		$in_dst = ($timestamp > $start && $timestamp < $end);
		if($reverse) $in_dst = !$in_dst;
		if($in_dst)
		{
			return $timestamp + ($TZ['timzone_dst_offset'] * 3600); 
		} else {
			return $timestamp + ($TZ['timzone_normal_offset'] * 3600);
		}
	} else {
		return $timestamp + ($TZ['timzone_normal_offset'] * 3600);
	}
}
function getDatesForYear($year)
{
	global $timezone_dst_start_cache, $timezone_dst_end_cache, $TZ;
	if (isset($timezone_dst_start_cache[$year]))
	{
		return array(
			'start' => $timezone_dst_start_cache[$year],
			'end' => $timezone_dst_end_cache[$year]
		);
	} else {
		$result = array(
			'start' => eval($TZ['timezone_dst_start']),
			'end' => eval($TZ['timezone_dst_end'])
		);
		$timezone_dst_start_cache[$year] = $result['start'];
		$timezone_dst_end_cache[$year] = $result['end']; 
		return $result;
	}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Time Zone - Proof Of Concept</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<p><strong>Timezone <?=$TZ['timezone_name'] ?> </strong></p>
<p>Generating Random Timestamps:</p>
<?php
//generate a number of random times
$count = 100;
$years_back = 10; //allow timestamps from max X years ago
for($i=0; $i<$count; $i++)
{
	$time = rand(time() - $years_back * 31557600, time()); //in universal time zone
	echo $time . '<br>';
	$times[] = $time;
}
?>
<p><strong>Starting Timer...<?php $startTimer = microtime(); ?>DONE</strong></p>
<p><strong>Rendering Dates</strong></p>
<?php
foreach($times as $time)
{
	echo date('l F dS Y h:i:s', getAdjustedTimestamp($time)) . '<br>'; 
}
?>
<p><strong>...<?php $endTimer = microtime(); ?>DONE</strong></p>
<?php
echo (float)strval($endTimer - $startTimer);
?> seconds elapsed.
</body>
</html>

geocator
Registered User
Posts: 100
Joined: Fri Jan 09, 2004 11:45 pm

Re: Daylight Saving Time (DST) within one country

Post by geocator »

What this does not take into account is the database workload required to pull the data to do this.

Mvastango
Registered User
Posts: 30
Joined: Wed Jun 09, 2004 12:00 am

Re: Daylight Saving Time (DST) within one country

Post by Mvastango »

Who said there had to be a database workload? You could just include $TZ arrays for each location and use the one you need. (Which would probably be more secure anyway, since there is eval'ed code) But if you wanted to put it in a db, I don't forsee any signifigant performance detriment (it might even be faster). You could just do a join to a time_regions table when the user data is obtained. The query could also be easilly cached for an extended period of time.

User avatar
CodeBuster
Registered User
Posts: 22
Joined: Fri Jul 16, 2004 9:48 pm
Location: The Netherlands
Contact:

Re: Daylight Saving Time (DST) within one country

Post by CodeBuster »

I dunno.. in the past several countries in Europe decided to not use DST and others did.. and then changed their mind.. all of them in the same TZ (!!) think of Norway, Holland, Belgium, France, Spain.. etc I think it is settled now, but one never knows :( maybe one day a stupid *beep* thinks it needs to change the date it will be effective :x

with that, you will need the exact location (i.e. country) to make it work.. Poland is in the same TZ as Egypt, but Egypt doesn't need DST, they got enough sun as it is :mrgreen: This would require a table with countries and there DST settings.

Another way is to make TZ's with more then just one option for say GMT +1.. as it is done in Windows, to cover all options wether countries in that TZ uses DST or not.

Best yet is to determine what all the differences are.. i.e. which country actually uses DST and when and have it limited to that, but then again, with the human race constantly changing their borders and believes, it will be a yearly task to see if the data is still correct.
place witty phrase here

Mvastango
Registered User
Posts: 30
Joined: Wed Jun 09, 2004 12:00 am

Re: Daylight Saving Time (DST) within one country

Post by Mvastango »

Right. The way I see it, TZ would be a multidimensional array of regions that the user chooses from. There would be a different array for every place with different timezones/DSTs.

User avatar
CodeBuster
Registered User
Posts: 22
Joined: Fri Jul 16, 2004 9:48 pm
Location: The Netherlands
Contact:

Re: Daylight Saving Time (DST) within one country

Post by CodeBuster »

yes, it is a good way to use the diff TZ's
and although I agree totally, it still depends

on my site, country is an important factor, so I already have that dbase and that would be a perfect startpoint for me, since the users has already selected their country. It makes the update to properly use the DST a breeze, then again, it would require an extra querie or two... or use it in combination with a TZ array.. or add an additional field to the users table.

but that would work for me since I already need that country thingy anyway..
and if you only use it for your local fish club.. who needs it

point being.. should it go fully automatic, or only triggered if the user checks the DST box? and if automaticly, what would be an easy way to update the excisting data?

Mvastango
Registered User
Posts: 30
Joined: Wed Jun 09, 2004 12:00 am

Re: Daylight Saving Time (DST) within one country

Post by Mvastango »

Well you can't just go by country. I live in the US. There are at least 5 different time zones, so there would have to be a more specific option for some areas. I don't know how it couldn't go fully automatic. The whole point would be to have it render every date based on the time when the post was made. There wouldn't even be a need for the DST selection, since all times are calculated regardless of whether or not you are in DST at the time of viewing.

geocator
Registered User
Posts: 100
Joined: Fri Jan 09, 2004 11:45 pm

Re: Daylight Saving Time (DST) within one country

Post by geocator »

Mvastango wrote:Well you can't just go by country. I live in the US. There are at least 5 different time zones, so there would have to be a more specific option for some areas. I don't know how it couldn't go fully automatic. The whole point would be to have it render every date based on the time when the post was made. There wouldn't even be a need for the DST selection, since all times are calculated regardless of whether or not you are in DST at the time of viewing.
And then there are individual states that have there own thing too. This is why many feel that auto DST just wont work. You would have to have a list a mile long of locations for each user to select from. Then all the calculating, etc. Add into this what the originall poster wants wich is to show the time based on wherther it was posted during dst and you have a mess on you hands. Each post would haveto have the users setting atthed to it etc. Hence why just letting your users do it works so very well.

Post Reply