ical.net icon indicating copy to clipboard operation
ical.net copied to clipboard

Offset must be specified in whole minutes - for certain timezones on Calendar.AddTimezone

Open davidMuir opened this issue 8 years ago • 0 comments

Getting an ArgumentException of Offset must be specified in whole minutes when calling Calendar.AddTimezone for three windows timezones:

  • SA Western Standard Time (-4h 0m)
  • Arab Standard Time (3h 0m)
  • Afghanistan Standard Time

Exception:

Unhandled Exception: System.ArgumentException: Offset must be specified in whole minutes.
Parameter name: offset
   at System.DateTimeOffset.ValidateOffset(TimeSpan offset)
   at System.DateTimeOffset..ctor(DateTime dateTime, TimeSpan offset)
   at Ical.Net.CalendarComponents.VTimeZone.FromDateTimeZone(String tzId, DateTime earlistDateTimeToSupport, Boolean includeHistoricalData) in C:\git\ical.net\net-core\Ical.Net\CalendarComponents\VTimeZone.cs:line 57
   at Ical.Net.CalendarComponents.VTimeZone.FromSystemTimeZone(TimeZoneInfo tzinfo) in C:\git\ical.net\net-core\Ical.Net\CalendarComponents\VTimeZone.cs:line 23
   at Ical.Net.Calendar.AddTimeZone(TimeZoneInfo tzi) in C:\git\ical.net\net-core\Ical.Net\Calendar.cs:line 393
   at ICal_Net_Timezone_Bug_Repro.Program.Run() in C:\Users\David\Source\Repos\misc\ICal_Net_Timezone_Bug_Repro\Program.cs:line 39
   at ICal_Net_Timezone_Bug_Repro.Program.Main(String[] args) in C:\Users\David\Source\Repos\misc\ICal_Net_Timezone_Bug_Repro\Program.cs:line 12

Console App to find failing timezones:

using System;
using System.Collections.Generic;
using Ical.Net;
using Ical.Net.CalendarComponents;

namespace ICal_Net_Timezone_Bug_Repro
{
    class Program
    {
        static void Main(string[] args)
        {
            new Program().Run();
        }

        void Run()
        {
            var cal = new Calendar();

            var timezones = TimeZoneInfo.GetSystemTimeZones();

            foreach(var timezone in timezones)
            {
                try
                {
                    cal.AddTimeZone(timezone);
                }
                catch (Exception e)
                {
                    // Ignore timezones that couldn't be found
                    if (!e.Message.Contains("Offset")) continue;

                    var offset = $"{timezone.BaseUtcOffset.Hours}h {timezone.BaseUtcOffset.Minutes}m";

                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine($"{timezone.Id} ({offset}) --- {e.Message}");
                    Console.ResetColor();

                    continue;
                }
            }
        }
    }
}

Result from above program:

SA Western Standard Time (-4h 0m) --- Offset must be specified in whole minutes.
Parameter name: offset
Arab Standard Time (3h 0m) --- Offset must be specified in whole minutes.
Parameter name: offset
Afghanistan Standard Time (4h 30m) --- Offset must be specified in whole minutes.
Parameter name: offset

davidMuir avatar Apr 12 '18 15:04 davidMuir