Friday, November 12, 2010

DayLight Saving and DateTime in .Net

Today, while looking to solve one issue related to DataTime in .Net, I came across article from MSDN that describes the best practice recommendations for writing programs that use the DateTime type in Microsoft .NET-based applications and assemblies.

While going through article, I came across the Daylight saving time (DST) and its impact on Applications. So I decided to give brief about DST and will discuss how it effects the Programmers.

Daylight saving time:

Daylight saving time (DST)—also summer time in British English is the practice of temporarily advancing clocks during the summer time so that afternoons have more daylight and mornings have less. Typically clocks are adjusted forward one hour near the start of spring and are adjusted backward in autumn.

In a typical case where a one-hour shift occurs at 02:00 local time, in spring the clock jumps forward from 02:00 standard time to 03:00 DST and that day has 23 hours, whereas in autumn the clock jumps backward from 02:00 DST to 01:00 standard time, repeating that hour, and that day has 25 hours.

A digital display of local time does not read 02:00 exactly at the shift, but instead jumps from 01:59:59.9 either forward to 03:00:00.0 or backward to 01:00:00.0. Clock shifts are usually scheduled near a weekend midnight to lessen disruption to weekday schedules.

Advantages of DST:
  • Many countries observe DST, and many do not. The reason many countries implement DST is in hopes to make better use of the daylight in the evenings.
  • Some believe that it could be linked to reducing the amount of road accidents and injuries.
  • The extra hour of daylight in the evening is said to give children more social time with friends and family
  • DST can even boost the tourism industry because it increases the amount of outdoor activities.
  • Majority of people also believe that DST saves energy as it influences people to spend more time out of the house, thus decreasing the need for artificial lighting as well as the likelihood of using home electric appliances.

Disadvantages of DST:
  • Flight schedules and inaccurate transportation timetables have caused confusion among travelers, for both personal and business purposes, and regular commuters. The transport industry needed to take into account the costs for adjusting to new time schedules.
  • Other complaints about daylight saving include the safety fears in the dark mornings, especially for school children waiting for a bus in some areas. Those who went early to work or study, they would be leaving their homes in the dark, the time when crime was at its highest, putting them in potentially dangerous situations.
  • Farming groups have also expressed anti-daylight saving views, saying that daylight saving time had a significant adverse impact on rural families, businesses, and communities. There are also those who express health concerns linked with daylight saving time.

Daylight Saving Time and .Net:

Consider a case when on particular morning during DST, at 2:00 AM, the local clock is reset to 1:00 AM, creating a 25-hour day. Since all values of clock time between 1:00 AM and 2:00 AM occur twice on that particular morning—at least in most of the United states and Canada. The computer really has no way to know which 1:10 AM was meant—the one that occurs prior to the switch, or the one that occurs 10 minutes after the daylight savings time switch.

Similarly, programs have to deal with the problem that happens in the springtime when, on a particular morning, there is no such time as 2:10 AM. The reason is that at 2:00 on that particular morning, the time on local clocks suddenly changes to 3:00 AM. The entire 2:00 hour never happens on this 23-hour day.

Programs have to deal with these cases (Particularly applications that deals with Scheduler. Such as Marketing Emails to send during night hours or taking Backup during night), possibly by prompting the user when you detect the ambiguity. 

If you aren't collecting date-time strings from users and parsing them, then you probably don't have these issues. Programs that need to determine whether a particular time falls in daylight savings time can make use of the following:
    Timezone.CurrentTimeZone.IsDaylightSavingTime(DateTimeInstance)
or
DateTimeInstance.IsDaylightSavingTime
   
So, if you need to perform DateTime calculations (add/subtract) on values representing time zones that practice daylight savings time, unexpected calculation errors can result. Instead, convert the local time value to universal time, perform the calculation, and convert back to achieve maximum accuracy.

For the complete tips for dealing with DateTime, go through the below link from MSDN.

http://msdn.microsoft.com/en-us/library/ms973825.aspx

I believe that above link from MSDN is helpful to all the Programmers. If we take care of above points, our software are least prone to errors.


For further details on DST:
http://www.timeanddate.com/time/dst/