Cotonti / Open Source PHP Content Management FrameworkContent Management Framework

Forums / Cotonti / Extensions / Support / DST Checking of Timestamps

Hodges
#1 2014-04-14 11:37

Hi,

I'm in the middle of working on a heavily modified calendar plugin that uses ajax to emulate google calendar. It's all going well but I want to pay special attention to how I present timestamps to users. At this stage I'd like to make it clear that I know it's important to store timestamps in UTC.

Using cotonti's datetime functions, how do I take an historical timestamp and check if DST was in effect at the time (according to the user's timezone selection) and display the time accordingly? I want to ensure that, if DST is currently in effect for the user, I'm not applying DST corrections to a timestamp that refers to a time when DST wasn't in effect.

I hope my question makes sense... Thanks!

I've given examples below:

Create an event for a date outside DST when it is currently DST:

                STORE AS 09:00                 DISPLAY AS 09:00

Create an event for a date outside DST when it is not currently DST:

                STORE AS 09:00                 DISPLAY AS 09:00

Create an event for a date inside DST when it is currently DST:

                STORE AS 08:00                 DISPLAY AS 09:00

Create an event for a date inside DST when it is not currently DST:

                STORE AS 08:00                 DISPLAY AS 09:00

Added 22 minutes later:

My example assumes I'm on the London/Europe timezone.

Added 5 minutes later:

What would be ideal would be a function (and its reciprocal) that could take a UTC timestamp and the user's preferred timezone and output a local timestamp for the user ensuring it includes the DST correction appropriately based on whether the timestamp occurred during that timezone's DST rules.


This post was edited by Hodges (2014-04-14 12:04, 6 years ago)
Macik
#2 2014-04-14 14:03

Thank you for a great question pushed me to read some docs. Here some info I get:

First of all — PHP already had tools to use timezines and DST.

Moreover, as DST is subject to change during long time periods (as legislation changes) PHP had these information in DateTimeZone::listAbbreviations()

Than PHP used this info to properly convert time from one time zone to another.

So two rules should be used: 
1. store timestamps in UTC (as you do)
2. use proper time zone for any convertion.

Let's try some example:

$date = new DateTime('2000-01-01', new DateTimeZone('Europe/Moscow'));  
// DST is used in these period  (+03:00 winter +04:00 summer)
$plugin_body .= $date->format('Y-m-d H:i:s P') . PHP_EOL; // 2000-01-01 00:00:00+03:00
$date->setTimeZone(new DateTimeZone('UTC'));
$plugin_body .= $date->format('Y-m-d H:i:s P') . PHP_EOL; // 1999-12-31 21:00:00+00:00
$date->modify('+ 13 year'); 
// in midsummer of 2011 DST was canceled in our region
// actually we get in DST time forever (+04:00 winter +04:00 summer)
$plugin_body .= $date->format('Y-m-d H:i:s P') . PHP_EOL; // 2012-12-31 21:00:00+00:00
$date->setTimeZone(new DateTimeZone('Europe/Moscow')); 
$plugin_body .= $date->format('Y-m-d H:i:s P') . PHP_EOL; // 2013-01-01 01:00:00+04:00
// as DST is cancelled we +04:00 for whole year

So if user had selected propper timezone in users settings (user profile) DST should be concerned.

But some problem may occurs only if you use time shift functions ($date->modify('+ 13 year');) in local time zone as real calculation is made in UTC.

Some info links:

 

 

https://github.com/macik
правильный хостинг — https://goo.gl/fjCa1F

This post was edited by Macik (2014-04-16 11:32, 6 years ago)
Hodges
#3 2014-04-15 09:36

Hi Macik, thanks for going to the trouble of researching this info. DateTimeZone() does appear to be the solution to my concerns.