none
DateTime, AddSeconds(), and Daylight Savings Time RRS feed

  • Question

  • The documentation for the DateTime AddHours(), AddSeconds(), etc. functions do not make clear what happens if adding the time amount changes the Daylight Time status of the DateTime.

    I have found that when adding a sufficient number of seconds to the start of the year to enter the Daylight Time zone, the following occurs:

    The new time does not reflect the Daylight time.  That is, if I add an even number of hours (as seconds via AddSeconds()) to the start of the year, the hours value of the result will be even and therefore not quite correct.  If I call IsDaylightTime() on the result, it will return true.

    Is this behavior considered to be correct?

    Right now my software expects this behavior.  I worry that someone may come along an apply a fix, therefore breaking my software when it is rebuilt.

    I wish that DateTime had a property that would return the DateTime value as its standard time equivalent (without the DST adjustment).

    Sunday, May 13, 2012 4:12 AM

Answers

  • The Microsoft Date/Time is stored a a number where 1 = Jan 1, 1900.  Each day equals one.  an hour equals 1/24. All the functions like AddHours(), AddSeconds() just performs an addition.

    Now windows stores two items

        1) The Time Zone which includes if your zone switches from Standard to Daylight

        2) The Date/Time.

    Now if I change my computer setting from Eastern Time Zone to Central time Zone th eclock on my computer changes by 1 hour which means windows stores time as GMT time and then does a conversion using the Time Zone to display the local time.  This makes lots of sense if you are using a server in one time zone and you are in a different time zone you want the server and the client to be using the same clock.

    GMT does not change between Standard time and Daylight time.  So when you are adding hours or minutes you are really adding hours and minutes to GMT.

    Now the answer to your last question below.  The time in your computer doesn't change when you switch from Daylight time to Standard time.  The function in your computer that returns the Ascii string with the date uses the time Zone set in your computer and converters the GMT time to your local time as an ascii string.  So on the day when you set your clock back one hour you will get two GMT times that will display 1:30 AM.  Any time function that returns a number will always be correct.  Only the time functons that return a string will create confusion.

    I wish that DateTime had a property that would return the DateTime value as its standard time equivalent (without the DST adjustment).


    jdweng

    Sunday, May 13, 2012 4:53 AM
  • Joel Engineer,

    Thank you for your thoughtful and helpful reply.  I am still uncomfortable with the behavior of the AddSeconds (or AddHours, etc.) function.  As I explained originally, if the addition moves you into Daylight Time, the result will say that it is Daylight (IsDaylightTime() returns true), but the value will be the corresponding standard time.

    Therefore I have changed my code to perform the calculation in UTC (Universal Time) and the convert to Local time.  That is:

    DateTime utcValue = startDateTimeUTC.AddSeconds(value);

    DateTime result = utcValue.ToLocalTime();

    This performs correctly: result is properly adjusted for Daylight Time.  result.IsDaylightTime() returns true.

    Thanks again.

    • Marked as answer by MichaelBate Monday, May 14, 2012 3:18 PM
    Monday, May 14, 2012 3:18 PM

All replies

  • The Microsoft Date/Time is stored a a number where 1 = Jan 1, 1900.  Each day equals one.  an hour equals 1/24. All the functions like AddHours(), AddSeconds() just performs an addition.

    Now windows stores two items

        1) The Time Zone which includes if your zone switches from Standard to Daylight

        2) The Date/Time.

    Now if I change my computer setting from Eastern Time Zone to Central time Zone th eclock on my computer changes by 1 hour which means windows stores time as GMT time and then does a conversion using the Time Zone to display the local time.  This makes lots of sense if you are using a server in one time zone and you are in a different time zone you want the server and the client to be using the same clock.

    GMT does not change between Standard time and Daylight time.  So when you are adding hours or minutes you are really adding hours and minutes to GMT.

    Now the answer to your last question below.  The time in your computer doesn't change when you switch from Daylight time to Standard time.  The function in your computer that returns the Ascii string with the date uses the time Zone set in your computer and converters the GMT time to your local time as an ascii string.  So on the day when you set your clock back one hour you will get two GMT times that will display 1:30 AM.  Any time function that returns a number will always be correct.  Only the time functons that return a string will create confusion.

    I wish that DateTime had a property that would return the DateTime value as its standard time equivalent (without the DST adjustment).


    jdweng

    Sunday, May 13, 2012 4:53 AM
  • Joel Engineer,

    Thank you for your thoughtful and helpful reply.  I am still uncomfortable with the behavior of the AddSeconds (or AddHours, etc.) function.  As I explained originally, if the addition moves you into Daylight Time, the result will say that it is Daylight (IsDaylightTime() returns true), but the value will be the corresponding standard time.

    Therefore I have changed my code to perform the calculation in UTC (Universal Time) and the convert to Local time.  That is:

    DateTime utcValue = startDateTimeUTC.AddSeconds(value);

    DateTime result = utcValue.ToLocalTime();

    This performs correctly: result is properly adjusted for Daylight Time.  result.IsDaylightTime() returns true.

    Thanks again.

    • Marked as answer by MichaelBate Monday, May 14, 2012 3:18 PM
    Monday, May 14, 2012 3:18 PM
  • That is the correct solution.  You didn't post any code so I couldn't give a soltion.  I'm glad my input helps.  The problems AddSeconds (or AddHours, etc.) functions is that the documentation doesn't give you enough information on the behavior of these functions.  That is what makes you feel uncomforatable.  Once you know how a functions works hopefully it will always functiona the same.  But you never know what Microsoft will change or fix from one release to another because it is not documented the way it SHOULD WORK!


    jdweng

    Monday, May 14, 2012 3:31 PM
  • According to the documentation, time zone adjustment rules are never considered when performing arithmetic operations on DateTime and DateTimeOffset values.  For more information, see Performing Operations with Dates and Times and the "DateTime Operations" section of the DateTime Structure topic.

    --Ron Petrusha

    Common Language Runtime User Education

    Microsoft Corporation

    Monday, May 14, 2012 10:52 PM