Daylight saving

The IsDaylightSavingTime and GetUtcOffset methods work as follows:


using System;
using System.Text;
class Sample
{
    public static void Main()
    {
        TimeZone zone = TimeZone.CurrentTimeZone;


        DateTime dt1 = new DateTime(2008, 1, 1);
        DateTime dt2 = new DateTime(2008, 6, 1);
        Console.WriteLine(zone.IsDaylightSavingTime(dt1));
        Console.WriteLine(zone.IsDaylightSavingTime(dt2));  

    }
}

The output:


False
True

GetDaylightChanges method returns specific daylight saving time information for a given year:


using System;
using System.Text;
using System.Globalization;
class Sample
{
    public static void Main()
    {
        TimeZone zone = TimeZone.CurrentTimeZone;
        DaylightTime day = zone.GetDaylightChanges(2008);
        Console.WriteLine(day.Start);
        Console.WriteLine(day.End);
        Console.WriteLine(day.Delta);  
    }
}

The output:


3/9/2008 2:00:00 AM
11/2/2008 2:00:00 AM
01:00:00

TimeZoneInfo IsDaylightSavingTime method accepts either a DateTime or a DateTimeOffset


using System;
using System.Text;
using System.Globalization;
class Sample
{
    public static void Main()
    {
        TimeZoneInfo zone = TimeZoneInfo.Local;

        DateTime dt1 = new DateTime(2008, 1, 1);

        Console.WriteLine(zone.IsDaylightSavingTime(dt1));

        DateTime dt = new DateTime(2008, 1, 1);

        Console.WriteLine(zone.IsDaylightSavingTime(dt));
    }
}

The output:


False
False

Daylight saving time and TimeZoneInfo

IsInvalidTime returns true if a DateTime is within the hour that's skipped when the clocks move forward.

IsAmbiguousTime returns true if a DateTime or DateTimeOffset is within the hour that's repeated when the clocks move back.

GetAmbiguousTimeOffsets returns an array of TimeSpans representing the valid offset choices for an ambiguous DateTime or DateTimeOffset.


using System;
using System.Text;
using System.Globalization;
class Sample
{
    public static void Main()
    {
        TimeZoneInfo zone = TimeZoneInfo.Local;

        DateTime dt1 = new DateTime(2008, 1, 1);

        Console.WriteLine(zone.IsInvalidTime(dt1));
        Console.WriteLine(zone.IsAmbiguousTime(dt1));
        Console.WriteLine(zone.GetAmbiguousTimeOffsets(dt1));

    }
}

The output:


False
False

Unhandled Exception: System.ArgumentException: The supplied DateTime is not in a
n ambiguous time range.
Parameter name: dateTime
   at System.TimeZoneInfo.GetAmbiguousTimeOffsets(DateTime dateTime)
   at Sample.Main()

GetAdjustmentRules returns a declarative summary of all daylight saving rules


using System;
using System.Text;
using System.Globalization;
class Sample
{
    public static void Main()
    {
        foreach (TimeZoneInfo.AdjustmentRule rule in TimeZoneInfo.Local.GetAdjustmentRules())
        {
            Console.WriteLine("Rule: applies from " + rule.DateStart + " to " + rule.DateEnd);
            Console.WriteLine("  Delta: " + rule.DaylightDelta);
            Console.WriteLine("  Start: " + rule.DaylightTransitionStart);

            Console.WriteLine("  End:  " + rule.DaylightTransitionEnd);
            Console.WriteLine();

        }

    }
}

The output:


Rule: applies from 1/1/0001 12:00:00 AM to 12/31/2006 12:00:00 AM
	Delta: 01:00:00
	Start: System.TimeZoneInfo+TransitionTime
	End:	System.TimeZoneInfo+TransitionTime

Rule: applies from 1/1/2007 12:00:00 AM to 12/31/9999 12:00:00 AM
	Delta: 01:00:00
	Start: System.TimeZoneInfo+TransitionTime
	End:	System.TimeZoneInfo+TransitionTime
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.