locked
Ambiguous DateTimes... RRS feed

  • Question

  • User1657234232 posted

    I want to store DateTimes in my Database in Universal Time. A user can select his/her TimeZone (Retrieved from TimeZoneInfo.GetSystemTimeZones)

    I created a usercontrol, where a (local) datetime can be entered. This LocalDateTime is converted to Universal Time, which is stored in the database:

    Dim LocalTimeZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(UserTimeZoneInfoId)
    Dim LocalDateTime As New DateTime(Year, Month, Day, Hour, Minute, 0)
    Dim UniversalDateTime As DateTime = TimeZoneInfo.ConvertTimeToUtc(LocalDateTime, LocalTimeZone)
    
    


    So far so good. Next step was that I would validate the LocalDateTime. A (Local) DataTime can be Invalid, when the time moves ahead from Standard Time To Daylight Saving Time, preventing it to be converted to Universal Time. This can be done like this:

    Dim IsInvalidTime As Boolean = LocalTimeZone.IsInvalidTime(LocalDateTime)


    When this is true, I can present my users a message that they entered an invalid date. But then the next validation. When the time moves back from Daylight Saving Time to Standard Time, some (local) DateTimes are Ambiguous. This can be checked like:

    Dim IsAmbiguousTime As Boolean = LocalTimeZone.IsAmbiguousTime(LocalDateTime)


    If this is true, I want to present the users a choice between Standard Time and DST Time. But now my problem starts, because when I use the TimeZoneInfo.ConvertTimeToUtc Method, according to MSDN, this method assumes that it is the standard time of the source time zone.

    Dim UniversalDateTime As DateTime = TimeZoneInfo.ConvertTimeToUtc(localDateTime, localTimeZone)


    So my first question is:

    How can I calculate the UniversalDateTime of an Ambiguous Time, if the user has selected DST Time ?

    When this is solved, the other way around has the same problem. This can be done with the method TimeZoneInfo.ConvertTimeFromUtc. This will give the correct local time, however, this can be an ambiguous time. But according to MSDN (and tested by me), the DateTime.Kind property is DateTimeKind.Unspecified Which will result that the date will always be a Standard Time, even if the Universal time would correspond with the DST time!

    LocalDateTime As DateTime = TimeZoneInfo.ConvertTimeFromUtc(UniversalDateTime , localTimeZone)


    So the next question is:

    How can I calculate the Local Time which is ambiguous, with the correct sDaylightSavingTime Property ?

    Thursday, February 25, 2010 1:27 AM

All replies

  • User-1636183269 posted

    Hello,

    Please go through below link hope you will get idea, Please let me know if you have still any doubt.

    http://forums.asp.net/t/1514920.aspx


    Tuesday, March 2, 2010 3:20 AM
  • User1657234232 posted

    Sandeep,

    I'm sorry, I don't think you read my question. That thread is about Culture (how dates, among others, are formatted), my question is all about TimeZones

    Tuesday, March 2, 2010 9:24 AM