mattjohnsonpint / TimeZoneConverter

Lightweight libraries to convert between IANA, Windows, Rails, and POSIX time zones.
Other
842 stars 81 forks source link

Add Apple Support #4

Closed cblaze22 closed 6 years ago

cblaze22 commented 7 years ago

You should add a conversion for Apple timezones.

public static Dictionary<string, string> GetWindowsToApple()
        {
            var timeZonesTranslation = new Dictionary<string, string>();

            timeZonesTranslation.Add("Dateline Standard Time", "Pacific/Midway");
            timeZonesTranslation.Add("UTC-11", "GMT");
            timeZonesTranslation.Add("Aleutian Standard Time", "America/Adak");
            timeZonesTranslation.Add("Hawaiian Standard Time", "Pacific/Honolulu");
            timeZonesTranslation.Add("Marquesas Standard Time", "Pacific/Marquesas");
            timeZonesTranslation.Add("Alaskan Standard Time", "America/Juneau");
            timeZonesTranslation.Add("UTC-09", "GMT");
            timeZonesTranslation.Add("Pacific Standard Time (Mexico)", "America/Tijuana");
            timeZonesTranslation.Add("UTC-08", "GMT");
            timeZonesTranslation.Add("Pacific Standard Time", "America/Los_Angeles");
            timeZonesTranslation.Add("US Mountain Standard Time", "America/Phoenix");
            timeZonesTranslation.Add("Mountain Standard Time (Mexico)", "America/Mazatlan");
            timeZonesTranslation.Add("Mountain Standard Time", "America/Denver");
            timeZonesTranslation.Add("Central America Standard Time", "America/Guatemala");
            timeZonesTranslation.Add("Central Standard Time", "America/Chicago");
            timeZonesTranslation.Add("Easter Island Standard Time", "Pacific/Easter");
            timeZonesTranslation.Add("Central Standard Time (Mexico)", "America/Mexico_City");
            timeZonesTranslation.Add("Canada Central Standard Time", "America/Regina");
            timeZonesTranslation.Add("SA Pacific Standard Time", "America/Lima");
            timeZonesTranslation.Add("Eastern Standard Time (Mexico)", "America/Cancun");
            timeZonesTranslation.Add("Eastern Standard Time", "America/New_York");
            timeZonesTranslation.Add("Haiti Standard Time", "America/Port-au-Prince");
            timeZonesTranslation.Add("Cuba Standard Time", "America/Havana");
            timeZonesTranslation.Add("US Eastern Standard Time", "America/Indiana/Indianapolis");
            timeZonesTranslation.Add("Paraguay Standard Time", "America/Asuncion");
            timeZonesTranslation.Add("Atlantic Standard Time", "America/Halifax");
            timeZonesTranslation.Add("Venezuela Standard Time", "America/Caracas");
            timeZonesTranslation.Add("Central Brazilian Standard Time", "America/Cuiaba");
            timeZonesTranslation.Add("SA Western Standard Time", "America/Guyana");
            timeZonesTranslation.Add("Pacific SA Standard Time", "America/Santiago");
            timeZonesTranslation.Add("Turks And Caicos Standard Time", "America/Grand_Turk");
            timeZonesTranslation.Add("Newfoundland Standard Time", "America/St_Johns");
            timeZonesTranslation.Add("Tocantins Standard Time", "America/Araguaina");
            timeZonesTranslation.Add("E. South America Standard Time", "America/Sao_Paulo");
            timeZonesTranslation.Add("SA Eastern Standard Time", "America/Fortaleza");
            timeZonesTranslation.Add("Argentina Standard Time", "America/Argentina/Buenos_Aires");
            timeZonesTranslation.Add("Greenland Standard Time", "America/Godthab");
            timeZonesTranslation.Add("Montevideo Standard Time", "America/Montevideo");
            timeZonesTranslation.Add("Magallanes Standard Time", "America/Argentina/Buenos_Aires");
            timeZonesTranslation.Add("Saint Pierre Standard Time", "America/Miquelon");
            timeZonesTranslation.Add("Bahia Standard Time", "America/El_Salvador");
            timeZonesTranslation.Add("UTC-02", "GMT");
            timeZonesTranslation.Add("Mid-Atlantic Standard Time", "Atlantic/South_Georgia");
            timeZonesTranslation.Add("Azores Standard Time", "Atlantic/Azores");
            timeZonesTranslation.Add("Cape Verde Standard Time", "Atlantic/Cape_Verde");
            timeZonesTranslation.Add("UTC", "GMT");
            timeZonesTranslation.Add("Morocco Standard Time", "Africa/Casablanca");
            timeZonesTranslation.Add("GMT Standard Time", "Europe/Dublin");
            timeZonesTranslation.Add("Greenwich Standard Time", "Africa/Monrovia");
            timeZonesTranslation.Add("W. Europe Standard Time", "Europe/Berlin");
            timeZonesTranslation.Add("Central Europe Standard Time", "Europe/Bratislava");
            timeZonesTranslation.Add("Romance Standard Time", "Europe/Copenhagen");
            timeZonesTranslation.Add("Central European Standard Time", "Europe/Sarajevo");
            timeZonesTranslation.Add("W. Central Africa Standard Time", "Africa/Algiers");
            timeZonesTranslation.Add("Namibia Standard Time", "Africa/Windhoek");
            timeZonesTranslation.Add("Jordan Standard Time", "Asia/Amman");
            timeZonesTranslation.Add("GTB Standard Time", "Europe/Athens");
            timeZonesTranslation.Add("Middle East Standard Time", "Asia/Beirut");
            timeZonesTranslation.Add("Egypt Standard Time", "Africa/Cairo");
            timeZonesTranslation.Add("E. Europe Standard Time", "Europe/Chisinau");
            timeZonesTranslation.Add("Syria Standard Time", "Asia/Damascus");
            timeZonesTranslation.Add("West Bank Standard Time", "Asia/Gaza");
            timeZonesTranslation.Add("South Africa Standard Time", "Africa/Harare");
            timeZonesTranslation.Add("FLE Standard Time", "Europe/Helsinki");
            timeZonesTranslation.Add("Israel Standard Time", "Asia/Jerusalem");
            timeZonesTranslation.Add("Kaliningrad Standard Time", "Europe/Kaliningrad");
            timeZonesTranslation.Add("Libya Standard Time", "Africa/Tripoli");
            timeZonesTranslation.Add("Arabic Standard Time", "Asia/Baghdad");
            timeZonesTranslation.Add("Turkey Standard Time", "Europe/Istanbul");
            timeZonesTranslation.Add("Arab Standard Time", "Asia/Kuwait");
            timeZonesTranslation.Add("Belarus Standard Time", "Europe/Minsk");
            timeZonesTranslation.Add("Russian Standard Time", "Europe/Moscow");
            timeZonesTranslation.Add("E. Africa Standard Time", "Africa/Nairobi");
            timeZonesTranslation.Add("Iran Standard Time", "Asia/Tehran");
            timeZonesTranslation.Add("Arabian Standard Time", "Asia/Muscat");
            timeZonesTranslation.Add("Astrakhan Standard Time", "Europe/Moscow");
            timeZonesTranslation.Add("Azerbaijan Standard Time", "Asia/Baku");
            timeZonesTranslation.Add("Russia Time Zone 3", "Europe/Samara");
            timeZonesTranslation.Add("Mauritius Standard Time", "Indian/Mauritius");
            timeZonesTranslation.Add("Saratov Standard Time", "Europe/Moscow");
            timeZonesTranslation.Add("Georgian Standard Time", "Asia/Tbilisi");
            timeZonesTranslation.Add("Caucasus Standard Time", "Asia/Yerevan");
            timeZonesTranslation.Add("Afghanistan Standard Time", "Asia/Kabul");
            timeZonesTranslation.Add("West Asia Standard Time", "Asia/Tashkent");
            timeZonesTranslation.Add("Ekaterinburg Standard Time", "Asia/Yekaterinburg");
            timeZonesTranslation.Add("Pakistan Standard Time", "Asia/Karachi");
            timeZonesTranslation.Add("India Standard Time", "Asia/Kolkata");
            timeZonesTranslation.Add("Sri Lanka Standard Time", "Asia/Colombo");
            timeZonesTranslation.Add("Nepal Standard Time", "Asia/Kathmandu");
            timeZonesTranslation.Add("Central Asia Standard Time", "Asia/Dhaka");
            timeZonesTranslation.Add("Bangladesh Standard Time", "Asia/Dhaka");
            timeZonesTranslation.Add("Omsk Standard Time", "Asia/Omsk");
            timeZonesTranslation.Add("Myanmar Standard Time", "Asia/Rangoon");
            timeZonesTranslation.Add("SE Asia Standard Time", "Asia/Bangkok");
            timeZonesTranslation.Add("Altai Standard Time", "Asia/Barnaul");
            timeZonesTranslation.Add("W. Mongolia Standard Time", "Asia/Hovd");
            timeZonesTranslation.Add("North Asia Standard Time", "Asia/Krasnoyarsk");
            timeZonesTranslation.Add("N. Central Asia Standard Time", "Asia/Novosibirsk");
            timeZonesTranslation.Add("Tomsk Standard Time", "Asia/Tomsk");
            timeZonesTranslation.Add("China Standard Time", "Asia/Urumqi");
            timeZonesTranslation.Add("North Asia East Standard Time", "Asia/Irkutsk");
            timeZonesTranslation.Add("Singapore Standard Time", "Asia/Kuala_Lumpur");
            timeZonesTranslation.Add("W. Australia Standard Time", "Australia/Perth");
            timeZonesTranslation.Add("Taipei Standard Time", "Asia/Taipei");
            timeZonesTranslation.Add("Ulaanbaatar Standard Time", "Asia/Ulaanbaatar");
            timeZonesTranslation.Add("North Korea Standard Time", "Asia/Pyongyang");
            timeZonesTranslation.Add("Aus Central W. Standard Time", "Australia/Eucla");
            timeZonesTranslation.Add("Transbaikal Standard Time", "Asia/Chita");
            timeZonesTranslation.Add("Tokyo Standard Time", "Asia/Tokyo");
            timeZonesTranslation.Add("Korea Standard Time", "Asia/Seoul");
            timeZonesTranslation.Add("Yakutsk Standard Time", "Asia/Yakutsk");
            timeZonesTranslation.Add("Cen. Australia Standard Time", "Australia/Adelaide");
            timeZonesTranslation.Add("AUS Central Standard Time", "Australia/Darwin");
            timeZonesTranslation.Add("E. Australia Standard Time", "Australia/Brisbane");
            timeZonesTranslation.Add("AUS Eastern Standard Time", "Australia/Melbourne");
            timeZonesTranslation.Add("West Pacific Standard Time", "Pacific/Guam");
            timeZonesTranslation.Add("Tasmania Standard Time", "Australia/Hobart");
            timeZonesTranslation.Add("Vladivostok Standard Time", "Asia/Vladivostok");
            timeZonesTranslation.Add("Lord Howe Standard Time", "Australia/Lord_Howe");
            timeZonesTranslation.Add("Bougainville Standard Time", "Pacific/Bougainville");
            timeZonesTranslation.Add("Russia Time Zone 10", "Europe/Moscow");
            timeZonesTranslation.Add("Magadan Standard Time", "Asia/Magadan");
            timeZonesTranslation.Add("Norfolk Standard Time", "Pacific/Norfolk");
            timeZonesTranslation.Add("Sakhalin Standard Time", "Asia/Sakhalin");
            timeZonesTranslation.Add("Central Pacific Standard Time", "Pacific/Noumea");
            timeZonesTranslation.Add("Russia Time Zone 11", "Asia/Anadyr");
            timeZonesTranslation.Add("New Zealand Standard Time", "Pacific/Auckland");
            timeZonesTranslation.Add("UTC+12", "GMT");
            timeZonesTranslation.Add("Fiji Standard Time", "Pacific/Fiji");
            timeZonesTranslation.Add("Kamchatka Standard Time", "Europe/Moscow");
            timeZonesTranslation.Add("Chatham Islands Standard Time", "Pacific/Chatham");
            timeZonesTranslation.Add("UTC+13", "GMT");
            timeZonesTranslation.Add("Tonga Standard Time", "Pacific / Tongatapu");
            timeZonesTranslation.Add("Samoa Standard Time", "Pacific/Pago_Pago");
            timeZonesTranslation.Add("Line Islands Standard Time", "Pacific/Kiritimati");

            return timeZonesTranslation;

        }
mattjohnsonpint commented 6 years ago

Apple uses IANA time zones. Thus, this functionality already exists.

mattjohnsonpint commented 6 years ago

Also note your list above has many errors. I'd recommend using the TZConvert.WindowsToIANA function from this library instead.

cblaze22 commented 6 years ago

Actually I thought that too, but that is Apple's list they provide in code.

mattjohnsonpint commented 6 years ago

In what code?

mattjohnsonpint commented 6 years ago

Also, important to understand, due to the nature of time zones being political entities, this data changes regularly. I keep my library up-to-date with the changes so you can simply pull down the latest version. If you (or Apple or anyone really) have hard-coded a list somewhere, you're going to find it broken eventually as it ages.

cblaze22 commented 6 years ago

Those timezones I listed is what Apple excepts in the code below, it is not pure IANA. The self.event.timeZone is the timezones I have listed and translated from the Windows.

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setTimeZone:[NSTimeZone timeZoneWithName:self.event.timeZone]];
    [dateFormat setDateFormat:@"yyyy-MM-ddHH:mm:ss"];
mattjohnsonpint commented 6 years ago

That code doesn't give a list of time zones or do any translation. Though I see it's part of your code here: https://stackoverflow.com/questions/43497252/ekeventstore-saveevent-adding-events-a-hour-later

I'm sorry, but your assertion is likely wrong, and you haven't explained how you translated. Did you do it yourself manually? Did Apple give you an API that returned Windows time zones? And how are you getting self.event.timeZone anyway?

This article from Apple describes getting the list of all time zone names available on the system like so:

NSArray *timeZoneNames = [NSTimeZone knownTimeZoneNames];

Does that list actually include values with spaces around the slash like you have in your list above with "Pacific / Tongatapu"?

You also have mismatches like timeZonesTranslation.Add("UTC+13", "GMT"); where it should include the inverted offset as in Etc/GMT-13. And I'm pretty sure you're missing a lot of IANA zones and a few Windows ones.

Can you tell me specifically the values returned from the knownTimeZoneNames function that is not a valid IANA zone?

cblaze22 commented 6 years ago

https://pastebin.com/ibNU2RcG

mattjohnsonpint commented 6 years ago

As far as I can tell, those are all valid IANA time zone IDs. However, it is old data. It is at least older than version 2014b from what I can tell.

It also seems to be missing all zones from the etcetera file, such as Etc/GMT+2 and others. I wonder if that is still the case in the current data? If so, that would be a limiting factor.

Please apply updates to your system and try the generating list again.

Note that this library only tracks the most current time zone data. It is presumed that targeted systems are kept up to date. Anything else would result in insanity. :wink: