C# Program to Get the Ambiguous Time Offsets (Timezoneinfo.getambiguoustimeoffsets() Method)

You are currently viewing C# Program to Get the Ambiguous Time Offsets (Timezoneinfo.getambiguoustimeoffsets() Method)

C# program to get the ambiguous time offsets (TimeZoneInfo.GetAmbiguousTimeOffsets() Method)

using System;
using System.Globalization;
using System.Collections.ObjectModel;

class TimeZoneInfoDemo
{
    //Entry point of Program
    static public void Main()
    {
        DateTime ambiguousTime;

        TimeZoneInfo timeZone1 = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
        TimeZoneInfo timeZone2 = TimeZoneInfo.FindSystemTimeZoneById("UTC");

        ambiguousTime = new DateTime(2007, 11, 4, 1, 0, 0);

        if (timeZone1.IsAmbiguousTime(ambiguousTime))
        {
            Console.WriteLine("Time is ambiguous");

            TimeSpan[] ts = timeZone1.GetAmbiguousTimeOffsets(ambiguousTime);
            Console.WriteLine("Ambiguous time offsets: ");
            foreach (TimeSpan t in ts)
            {
                Console.WriteLine("\t" + t);
            }
        }
        else
            Console.WriteLine("Time is not ambiguous");

        if (timeZone2.IsAmbiguousTime(ambiguousTime))
        {
            Console.WriteLine("Time is ambiguous");

            TimeSpan[] ts = timeZone2.GetAmbiguousTimeOffsets(ambiguousTime);
            Console.WriteLine("Ambiguous time offsets: ");
            foreach (TimeSpan t in ts)
            {
                Console.WriteLine("\t" + t);
            }
        }
        else
            Console.WriteLine("Time is not ambiguous");
    }
}
Time is ambiguous
Ambiguous time offsets:
        -06:00:00
        -05:00:00
Time is not ambiguous
Press any key to continue . . .

Ranjith Kumar

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply