How does Moodle handle summer time?

How does Moodle handle summer time?

by Gustav W Delius -
Number of replies: 34

I understand that when Moodle stores a date in its database it converts it into an integer that counts the seconds since a certain fixed date in the past. During this conversion it takes the timezone into account. When Moodle prints a date from the database it does this process in reverse, converting the integer back into a time in the user's timezone. That way, if the user changes timezone between entering the date and looking at it again then the date gets shifted as well.

That all sounded quite clever to me until I started worrying over summer time. What happens when during summer the user creates a date in winter? The date shows correctly while the user is in summertime. But when the timezone is changed to agree with winter time will all the previously created dates be off by an hour?

Average of ratings: -
In reply to Gustav W Delius

Re: How does Moodle handle summer time?

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
Yes, changing your current timezone will change how all dates are displayed, including past dates.

Fortunately my state doesn't bother with daylight savings time so things are simple for me big grin. I once started looking into automatic handling of DST but a general automated solution is almost impossibly complex because of the millions of variations around the world.

I think the best way forward is to allow the users to set their own offset, start date and end date, then apply this within the date display functions like userdate(). Dates within the range get offset, and dates outside it do not.

Handling the creation of dates as in your scenarion, well ... phew, not sure about that one.
In reply to Martin Dougiamas

Re: How does Moodle handle summer time?

by Gustav W Delius -

Ah, if this is an unsolved problem then perhaps this thread should be moved to the developers forum or the calendar forum.

Has anyone experimented with the is_dst argument to mktime? In the PHP manual it says:

is_dst can be set to 1 if the time is during daylight savings time (DST), 0 if it is not, or -1 (the default) if it is unknown whether the time is within daylight savings time or not. If it's unknown, PHP tries to figure it out itself. This can cause unexpected (but not incorrect) results.

There must be some PHP code out there to deal with this daylight savings time issue. Does anyone know where? It would be ideal if the users could choose their timezones in the same way as on Windows. The timezone drop-down on Windows for example has two zones with GMT (one with DST and one without) and helps the user to choose the correct one by giving a few representative cities.

In reply to Gustav W Delius

Re: How does Moodle handle summer time?

by John Papaioannou -
This has been in the bug tracker for quite some time now, but it hasn't been tackled with because it's going to be a royal pain no matter what approach we choose.

Personally I 'd prefer to auto-detect this according to a user's country, but as Martin said even in one country things can be different (states/provinces). Maybe the start/end/offset idea will be the simplest in the end.

If you agree, let's keep all relevant discussion to the bugtracker.

Jon
In reply to John Papaioannou

Re: How does Moodle handle summer time?

by Gustav W Delius -

Hi Jon,

I think this issue is actually best discussed in the forums because there may well be someone out there who has solved the daylight savings time issue for a different project.

The situation we have at the moment renders the calendar almost entirely useless because all dates in it will suddenly be off by an hour when we switch to winter time. In the UK that happens on October 31. At the very least we will need a script that goes through the event table and automatically changes all the timestamps.

In reply to Gustav W Delius

Re: How does Moodle handle summer time?

by John Papaioannou -
I think that's a bit of an overstatement...

When summer time was enabled (I was still in Greece, GMT+2) the calendar dates suddenly became off by an hour. So I simply switched my timezone to GMT+3 and all was fine. When DST ceased to apply, I would switch it back to GMT+2 and all would be fine again.

I don't think that makes the calendar "entirely useless". And remember that e.g. in your case, you can switch the timezone of all your students with admin/timezone.php.

With all this I don't want to disregard the issue; I just want to say that it isn't as dire as it might sound at first.

Jon
In reply to John Papaioannou

Re: How does Moodle handle summer time?

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
The problem Gustav has raised is a bit different (one I had never noticed before either!).

For example, DST is active now, and you put something in the calendar for 5pm on a day in the future when DST won't be active. When that time rolls around, everyone's timezone will have changed, so the 5pm is displayed as 4pm instead (or 6pm, whatever smile).

Not "entirely useless" but certainly a bummer.  I imagine this affects most multi-user calendars that try to handle different timezones.
In reply to Martin Dougiamas

Re: How does Moodle handle summer time?

by Gustav W Delius -

Thanks Martin, you have explained the problem well.

I am sorry about the formulation "entirely useless". I didn't mean it to sound so negative. What I had in my mind was that I should not use it in my institution in its current form because the confusion it is going to cause will be a support nightmare. Unfortunately I have already rolled it out black eye. The confusion will hit on October 31.

In reply to Martin Dougiamas

Re: How does Moodle handle summer time?

by John Papaioannou -
Gulp... I didn't think of it that way.

What's more, the only place where you are allowed to mention DST in PHP is mktime(), so I suppose that:

When creating or editing an event, we should be trying to find out if that time (and not the current) falls within the DST range for the creator and inform mktime() accordingly.

I have no idea what will happen when PHP parses that timestamp with strftime() or anything, but just maybe we could also perform that kind of detection when retrieving events and manually compensate if needed. That would be in the case where the event is IN DST but we (right now) are not, or vice versa.

This will be a nightmare to test... but as Martin said, I cannot see how you can sidestep it.

Jon
In reply to Gustav W Delius

Re: How does Moodle handle summer time?

by Bill Burgos -
Just a thought.

How about a crontab trigger. It is flexible and allows for the fuzzy dates like first sunday of October, etc. A choice in TZ could choose from a table that will activate a script for that TZ only.
In reply to Martin Dougiamas

Re: How does Moodle handle summer time?

by Ger Tielemans -

Never thought about that.. So we "double" In springtime one hour and loose one hour next month.. or is it the other way around wink

Is the Apache server not leading in these time things?


Workaround: fake server maintenance during these two nights.

In reply to Ger Tielemans

Re: How does Moodle handle summer time?

by Ger Tielemans -
Comments of Gustav makes it a really problem: Solution could be: - the admin needs a chekbox choice if summertime is involved. (And drop-downboxes to set the days/hours of change) - When you enter during a summerperiod a winter-date and viseversa, Moodle could ask if it should correct the time for you... (Confusing for the user: "No, four o'clock is four o'clock..") User should have some cue like AM PM in the 12 hour clock: - 20.00 WT and 21.00 ST..)
In reply to Ger Tielemans

Re: How does Moodle handle summer time?

by Hooi Ling Eng -
Gustav,

We are experiencing this confusion right now. I thought of executing the update statement below to fix this but realised that I have to do the same when we switch to daylight savings time. How are you dealing with this problem?

UPDATE `mdl_event`
SET timestart = timestart + 3600
WHERE timemodified >= 1080428400
AND timemodified < 1099177200
AND timestart >= 1099177200
AND timestart < 1109458800


In reply to Hooi Ling Eng

Re: How does Moodle handle summer time?

by koen roggemans -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Translators
Hooi Ling, have you tested this SQL for updating the calender?

I've just noticed I'm in the same boat and since a lot of activities in our calender haven't got a start time (so it's set to 00:00), they are now set a day to early.

Too many jobs on sunday for me!blauw oog
In reply to koen roggemans

Re: How does Moodle handle summer time?

by Jean-Pierre Pawlak -
I did a quick test on our test server: the start time of the events set to 00:00  (23:00, winter-time) did not change. triest
In reply to koen roggemans

Re: How does Moodle handle summer time?

by Jean-Pierre Pawlak -
I did a second test just using the first two lines (UPDATE `mdl_event`
SET timestart = timestart + 3600). Now dates show up correctly again. I didn't check them all... I hope I didn't screw up things?
In reply to Jean-Pierre Pawlak

Re: How does Moodle handle summer time?

by koen roggemans -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Translators
It will change all events, but that's no problem as long as I do the same update in March again with -3600 smile
May be something for a button / manual sollution for the problem: make summertime (-h) / make wintertime (+1h). Just hit the button twice a year.
In reply to koen roggemans

Re: How does Moodle handle summer time?

by Marc Dastous -

I like this idea.  Is this something that could be done easily?  Adding this to the Variables page would be awesome.  Obviously, everyone that "springs forward" and "falls back" is facing a similar situation.

My students, although 6-8th graders, are very perceptive when it comes to assignments turned in late wink

Marc

In reply to Marc Dastous

Re: How does Moodle handle summer time?

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
The problem with this idea is that it changes ALL events.

Imagine it's the week after DST has changed and you do this ... the dates from the current week may be fixed but all the dates from the past few months will now be an hour out.
In reply to Martin Dougiamas

Re: How does Moodle handle summer time?

by koen roggemans -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Translators
It's a difficult situation.
I'm not sure about that. Of course you have to change BEFORE anyone puts a new date in the calender.

Since you change the view on the dates by changing the timezone, all dates are wrong, so you can update them all.
New calender items  (after the change) are posted with the new view, so are right against the current time, but wrong for the next change. This is no problem since it hasn't changed yet.

You change always two things: the timezone of the users and the timestamp of the events in the database. I think if you do that in one move, you don't have any problems IF ALL YOUR USERS FOLLOW THE SAME DST REGIME.
(Indeed no sollution for the Indiana-users sad )

Therefore a box in the userprofile could do: this user uses DST. People with that box look at the calender in DST with -3600 seconds. The admin puts Moodle in DST or non DST-mode manualy end october and end may
In reply to koen roggemans

Re: How does Moodle handle summer time?

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
Thanks, Koen, but I think we need to aim for at least a passable solution to arbitrary timezones among users on a site. We can't have numbers slopping up and down twice a year ... the main thing to remember is that any given time can be specified absolutely using Moodle's timestamp of "seconds since 00:00 1/1/1970 GMT", so we should keep storage as that and focus on individual conversions to and from (even though we lose some efficiency).

A quick think:

If every user has a setting for startDSTtime, endDSTtime, offsetseconds, then

- when displaying any given date to that user, we can use this information to convert from GMT to the local time for that user. Is the maths for this consistently GMT + (timezone * 3600) - offsetseconds ? Most of this can be done in userdate().

- when adding a new time anywhere, we look at that user's settings to determine whether the new time is within their DST period or not, and use this information to offset the final time when converting to GMT. Most of this can be done in make_timestamp().

I think those are the only two areas we need to worry about. Can someone else work out:
  • an efficient way of detecting "within their DST period"
  • how the offset is stored and how it should be added/subtracted
  • how things will work right at the hours that DST switches over
?
In reply to Martin Dougiamas

Re: How does Moodle handle summer time?

by koen roggemans -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Translators
Good "quick" thinking Martin! big grin

Storing all events in GMT has at least the advantage you know exactly when the events are going to happen. From that point calculating to the time in the right timezone, DST or not, can't be not to hard.

When DST starts /ends is very variable. Look at this table: (http://webexhibits.org/daylightsaving/g.html)

Continent Country Beginning and ending days
Africa Egypt Start: Last Friday in April
End: Last Thursday in September

Namibia Start: First Sunday in September
End: First Sunday in April
Asia Most states of the former USSR. Start: Last Sunday in March
End: Last Sunday in October

Iraq Start: April 1
End: October 1
  Israel (more info) (Estimate, Israel decides the dates every year)
Start: First Friday in April
End: First Friday in September

Lebanon, Kirgizstan Start: Last Sunday in March
End: Last Sunday in October
  Mongolia Stopped in 2002

Palestine (more info) (Estimate)
Start: First Friday on or after 15 April
End: First Friday on or after 15 October
  Syria Start: April 1
End: October 1

Iran Start: the first day of Farvardin
End: the first day of Mehr
Australasia Australia - South Australia, Victoria,
Australian Capital Territory, New South Wales,
Lord Howe Island
Start: Last Sunday in October
End: Last Sunday in March

Australia - Tasmania Start: First Sunday in October
End: Last Sunday in March
  Fiji Stopped in 2000
 
New Zealand, Chatham - (read law)
Start: First Sunday in October
End: Third Sunday in March
  Tonga Start: First Sunday in November
End: Last Sunday in January
Europe European Union - (read law)
UK - (read law)
Start: Last Sunday in March at 1 am UTC
End: Last Sunday in October at 1 am UTC
  Russia Start: Last Sunday in March at 2 am local time
End: Last Sunday in October at 2 am local time
North America United States, Canada, Mexico
St. Johns, Bahamas, Turks and Caicos
Start: First Sunday in April
End: Last Sunday in October
  Cuba Start: April 1
End: Last Sunday in October
  Greenland Same as EU
South America Brazil
(rules vary quite a bit from year to year).
Also, equatorial Brazil does not observe DST.
Start: First Sunday in November
End: Third Sunday in February
  Chile - (read law)
Start: Second Saturday of October - at midnight
End: Second Saturday of March - at midnight
  Falklands Start: First Sunday on or after 8 September
End: First Sunday on or after 6 April
  Paraguay Start: First Sunday in September
End: First Sunday in April

So detecting can only work when a user is connected to a country/region. So we will have to build/search a table with all the existing timezones/regions related to their daylight saving time regime and figure out how you know what is the "first sunday on or after 8 september"or "the last sunday of october". In the mentioned page is A LOT of information on that subject, including naming of the timezones and translation wink of the timezones.

This page reports on the transitions: when we move an hour ahead, we skip one hour (from 1:59 to 3:00), when we move an hour back, we repeat an hour (from 1:59 back to 1:00)
In reply to koen roggemans

Re: How does Moodle handle summer time?

by Martín Langhoff -
Moodle shouldn't be doing TZ calculations! The OS does it, and PHP offers the needed functions to make it happen.

Look at the date-time related functions that PHP offers. All POSIX OSses (that covers Unix and Win32 platforms) will give you localtime(), you just may have to manipulate the TZ to fit your current Moodle user!
In reply to Martín Langhoff

Re: How does Moodle handle summer time?

by N Hansen -
I don't know about Windows servers, but I can tell you about Windows desktop machines-the Windows OS consistently changes the time two days too early here in Egypt every time. it always changes it on the Wednesday before the Friday when it should really change it. So can we trust Windows servers to do the correct thing?
 
In reply to Martín Langhoff

Re: How does Moodle handle summer time?

by Martín Langhoff -
Ok -- just had a chat with Martin Dougiamas.

I hadn't realized that userdate() was sidestepping the whole localtime() issue, apparently because Win32 systems, while technically POSIX, tend not to be properly configured in GMT+TZ.

Given that I'd hate to see anyone re-inventing the localtime() wheel, I did some digging and found that we can use PEAR's Date package.

http://pear.php.net/package/Date/docs/1.4.2/Date/Date_TimeZone.html
Average of ratings: Useful (1)
In reply to koen roggemans

Re: How does Moodle handle summer time?

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
I don't think maintaining or using such lists will be necessary ... we just need to allow the users/admin to enter the own information (into user profiles) for their local needs.
In reply to Martin Dougiamas

Re: How does Moodle handle summer time?

by koen roggemans -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Translators
It must be possible to enter difficult to handle information like : last sunday of october... because that has to be calculated every time. How to handle that?
In reply to koen roggemans

Re: How does Moodle handle summer time?

by John Papaioannou -
I 'm thinking that a good way to do this is:

  1. Have a kind of complex configuration setting, DST. This would allow you to specify the DST transitions (start and end time) as e.g. "The first/the second/the last" "Monday/Tuesday/.../Sunday" of "January/.../December". Also it should allow you to pick a specific time within that day to trigger the transition and of course the amount of DST offset itself (in minutes).
  2. Since that's going to be a pain to set up multiple times, we can also do this:
    • Have the above be an admin-accessible setting only.
    • Allow the admin to save such DST settings as named presets. For example, in say a site in Greece where all users are high school students, I 'd only need the admin to create one preset: "Greece".
    • The users can then "enable" DST for themselves by choosing one of the presets. Their calendar preferences would read something like "DST Compensation: off" or "DST Compensation: On (Greece)" with a drop-down menu to choose.
    This would be inconvenient for the admins of a site like moodle.org wide eyes but it would be very very convenient for "normal" educational sites, and I think that's what matters.
  3. About detecting when to apply DST: Moodle could keep two stored values per user: the offset that must be applied to events considering the users current DST setting (this will be taken from the "preset" above obviously), and the next timestamp when a DST transition will occur. This scheme is designed to avoid calculating all the DST stuff in each page view. So, when a user logs on, we check his last login time and the stored "DST transition time". If the transition time is between the last and current login, we need to toggle the DST setting for that user, and calculate the next "DST transition time" to update its value.
    • Advantages: easy to implement (calendar will just know that it must fetch a fixed offset value, which may be 0, and apply it in each timestamp), quick ("expensive" checks are done only once when the user logs in)
    • Disadvantages: it won't help if you stay logged in during the DST transition, you 'll have to log out and in again thoughtful
How does it sound?
In reply to John Papaioannou

Re: How does Moodle handle summer time?

by Martin Mueller -

Sounds good. As for the disadvantage mentioned, log the user off, as soon as it passes the DST transition deadline. The overhead might be low if you store the timestamp, when this should happen in $CFG when logging in.

For the convenience of the user, you may render him a page after login, if he is logging in nearer than 24 hours before this time will come.

Martin

In reply to Martin Mueller

Re: How does Moodle handle summer time?

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
All of this has since been implemented in 1.5, Martin. wink  This is an old conversation.
In reply to Martin Dougiamas

Re: How does Moodle handle summer time?

by Tony Hursh -
but a general automated solution is almost impossibly complex because of the millions of variations around the world.

Consider the situation in the U.S. state of Indiana as explained (?) here:

http://www.mccsc.edu/time.html#WHAT

surprise

I'm in a neighboring state, and dealing with businesses or govenment offices in Indiana can be...er..."interesting".

smile


(for those who may not know, we use the terms Daylight Time or Daylight Savings Time for what most of the English-speaking world calls Summer Time).


In reply to Tony Hursh

Re: How does Moodle handle summer time?

by koen roggemans -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Translators
This is nuts. Unbeliefable no one has stand up to fix this situation blauw oog

In reply to koen roggemans

Re: How does Moodle handle summer time?

by Tony Hursh -
Well, there are reasons for it. Most of Indiana is farm country and farmers operate according to the sun, regardless of what the clock says. The parts of Indiana that are just across the border from large cities in neighboring states (Chicago, Illinois and Cincinnati, Ohio) quite reasonably prefer to stay in sync with their neighbors.

Nevertheless, it's very confusing. smile