none
Unable to check DST on datetime object RRS feed

  • Question

  • I am trying to check DST for a value in my DateTime variable. Instead of picking the timeszone from the system, we pick it from database and use while checking the date for DST. 

    DateTime date = new DateTime(2017, 11, 3, 20, 0, 0);
    
                for (int i = 0; i < 70; i++)
                {
                    TimeZoneInfo zone = TimeZoneInfo.CreateCustomTimeZone("America/Los_Angeles", new TimeSpan(-8, 0, 0), "America/Los_Angeles", "America/Los_Angeles");
                    bool res = zone.IsAmbiguousTime(date);
                    if (res == false)
                    {
                        bool DST = zone.IsDaylightSavingTime(date);
                        Console.WriteLine (date .ToShortDateString()+ " "+date.ToLongTimeString() + " : " + DST.ToString());
                        if (DST)
                        {
                            // Some logic
                        }
                    }
                    date = date.AddHours(1);
                }
    Above code always returns DST as false where as in PST timezone DST change occurred on 4-5 th nov 2017 night at 2 AM.

    11/3/2017 8:00:00 PM : False

    11/3/2017 9:00:00 PM : False
    11/3/2017 10:00:00 PM : False
    11/3/2017 11:00:00 PM : False
    11/4/2017 12:00:00 AM : False
    11/4/2017 1:00:00 AM : False
    11/4/2017 2:00:00 AM : False
    11/4/2017 3:00:00 AM : False
    11/4/2017 4:00:00 AM : False
    11/4/2017 5:00:00 AM : False
    11/4/2017 6:00:00 AM : False
    11/4/2017 7:00:00 AM : False
    11/4/2017 8:00:00 AM : False
    11/4/2017 9:00:00 AM : False
    11/4/2017 10:00:00 AM : False
    11/4/2017 11:00:00 AM : False
    11/4/2017 12:00:00 PM : False
    11/4/2017 1:00:00 PM : False
    11/4/2017 2:00:00 PM : False
    11/4/2017 3:00:00 PM : False
    11/4/2017 4:00:00 PM : False
    11/4/2017 5:00:00 PM : False
    11/4/2017 6:00:00 PM : False
    11/4/2017 7:00:00 PM : False
    11/4/2017 8:00:00 PM : False
    11/4/2017 9:00:00 PM : False
    11/4/2017 10:00:00 PM : False
    11/4/2017 11:00:00 PM : False
    11/5/2017 12:00:00 AM : False
    11/5/2017 1:00:00 AM : False
    11/5/2017 2:00:00 AM : False
    11/5/2017 3:00:00 AM : False
    11/5/2017 4:00:00 AM : False
    11/5/2017 5:00:00 AM : False
    11/5/2017 6:00:00 AM : False
    11/5/2017 7:00:00 AM : False
    11/5/2017 8:00:00 AM : False
    11/5/2017 9:00:00 AM : False
    11/5/2017 10:00:00 AM : False
    11/5/2017 11:00:00 AM : False
    11/5/2017 12:00:00 PM : False
    11/5/2017 1:00:00 PM : False
    11/5/2017 2:00:00 PM : False
    11/5/2017 3:00:00 PM : False
    11/5/2017 4:00:00 PM : False
    11/5/2017 5:00:00 PM : False
    11/5/2017 6:00:00 PM : False
    11/5/2017 7:00:00 PM : False
    11/5/2017 8:00:00 PM : False
    11/5/2017 9:00:00 PM : False
    11/5/2017 10:00:00 PM : False
    11/5/2017 11:00:00 PM : False
    11/6/2017 12:00:00 AM : False
    11/6/2017 1:00:00 AM : False
    11/6/2017 2:00:00 AM : False
    11/6/2017 3:00:00 AM : False
    11/6/2017 4:00:00 AM : False
    11/6/2017 5:00:00 AM : False
    11/6/2017 6:00:00 AM : False
    11/6/2017 7:00:00 AM : False
    11/6/2017 8:00:00 AM : False
    11/6/2017 9:00:00 AM : False
    11/6/2017 10:00:00 AM : False
    11/6/2017 11:00:00 AM : False
    11/6/2017 12:00:00 PM : False
    11/6/2017 1:00:00 PM : False
    11/6/2017 2:00:00 PM : False
    11/6/2017 3:00:00 PM : False
    11/6/2017 4:00:00 PM : False
    11/6/2017 5:00:00 PM : False

    What is not right in my code?

    Monday, January 22, 2018 6:58 PM

All replies

  • Since zone.SupportsDaylightSavingTime is false, then this custom time zone does not support Daylight Saving Time (DST).

    Check the examples in the Documentation and try this time zone:

    TimeSpan delta = new TimeSpan( 1, 0, 0 );
    
    var transitionRuleStart = TimeZoneInfo.TransitionTime.CreateFixedDateRule( new DateTime( 1, 1, 1, 2, 0, 0 ), 3, 4 );
    
    var transitionRuleEnd = TimeZoneInfo.TransitionTime.CreateFixedDateRule( new DateTime( 1, 1, 1, 2, 0, 0 ), 11, 4 );
    
    var adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule( DateTime.MinValue.Date, DateTime.MaxValue.Date,
    delta, transitionRuleStart, transitionRuleEnd );
     
    TimeZoneInfo zone = TimeZoneInfo.CreateCustomTimeZone( "America/Los_Angeles", new TimeSpan( -8, 0, 0 ), "America/Los_Angeles", "America/Los_Angeles", "America/Los_Angeles (DST)", new[] { adjustment } );

     

    Adjust it according to your needs and further researches.

    Monday, January 22, 2018 7:38 PM
  • Hi Dotnetavalanche,

    The code you provided to test DST is not support daylight saving time. I test the code Viorel provided with yours. Works well.

      TimeSpan delta = new TimeSpan(1, 0, 0);
    
                var transitionRuleStart = TimeZoneInfo.TransitionTime.CreateFixedDateRule(new DateTime(1, 1, 1, 2, 0, 0), 3, 4);
    
                var transitionRuleEnd = TimeZoneInfo.TransitionTime.CreateFixedDateRule(new DateTime(1, 1, 1, 2, 0, 0), 11, 4);
    
                var adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(DateTime.MinValue.Date, DateTime.MaxValue.Date,
                delta, transitionRuleStart, transitionRuleEnd);
    
                TimeZoneInfo zone = TimeZoneInfo.CreateCustomTimeZone("America/Los_Angeles", new TimeSpan(-8, 0, 0), "America/Los_Angeles", "America/Los_Angeles", "America/Los_Angeles (DST)", new[] { adjustment });
                bool b = zone.SupportsDaylightSavingTime;
    
                DateTime date = new DateTime(2017, 11, 3, 20, 0, 0);
    
                for (int i = 0; i < 70; i++)
                {
                  
                    bool res = zone.IsAmbiguousTime(date);
                    if (res == false)
                    {
                        bool DST = zone.IsDaylightSavingTime(date);
                        Console.WriteLine(date.ToShortDateString() + " " + date.ToLongTimeString() + " : " + DST.ToString());
                        if (DST)
                        {
                            // Some logic
                        }
                    }
                    date = date.AddHours(1);
                }
                Console.ReadKey();

    If your question has been solved, please make the useful reply as answer.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 23, 2018 6:54 AM
    Moderator
  • Hi Wendy, I tested the code you shared as it is and tested in my local machine. It still return false always. I am in GMT+5.30 hrs timezone. Solution should not consider local machine timezone. Instead I read timezone from databasefor example  "America/Los_Angeles"
    Tuesday, January 23, 2018 7:17 AM
  • Hi Dotnetavalanche,

    Thank you for feedback.

    >> It still return false always. I am in GMT+5.30 hrs timezone. 

    What does it returns false always mean? Based on my test, it could return true when you set the time of transitionRuleEnd in my code.

    The code like below, it will return all the true when you cover all time you want to test with GMT +5.30. It does not consider about local machine timezone. 

        var transitionRuleEnd = TimeZoneInfo.TransitionTime.CreateFixedDateRule(new DateTime(1, 1, 1, 2, 0, 0), 11, 10);
    
            

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Saturday, January 27, 2018 11:45 AM
    Moderator
  • Still waiting for the correct answer.
    Tuesday, February 6, 2018 6:53 AM
  • Hi Dotnetavalanche,

    Have you try the code I provided? It return all the true? Does this could not solve problem?

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 6, 2018 8:05 AM
    Moderator
  • Hi, it return all true. Problem is still not solved.
    Friday, April 27, 2018 6:18 AM