|
| 1 | +// |
| 2 | +// System.TimeZoneInfo helper for Unity |
| 3 | +// because the devices cannot access the file system to read the data |
| 4 | +// |
| 5 | +// Authors: |
| 6 | +// Michael DeRoy <[email protected]> |
| 7 | +// Jonathan Chambers <[email protected]> |
| 8 | +// |
| 9 | +// Copyright 2018 Unity Technologies, Inc. |
| 10 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 11 | + |
| 12 | +#if UNITY |
| 13 | + |
| 14 | +using System; |
| 15 | +using System.Collections.Generic; |
| 16 | +using System.Collections.ObjectModel; |
| 17 | +using System.Globalization; |
| 18 | +using System.IO; |
| 19 | +using System.Runtime.CompilerServices; |
| 20 | + |
| 21 | +namespace System { |
| 22 | + |
| 23 | + public partial class TimeZoneInfo { |
| 24 | + enum TimeZoneData |
| 25 | + { |
| 26 | + DaylightSavingFirstTransitionIdx, |
| 27 | + DaylightSavingSecondTransitionIdx, |
| 28 | + UtcOffsetIdx, |
| 29 | + AdditionalDaylightOffsetIdx |
| 30 | + }; |
| 31 | + |
| 32 | + enum TimeZoneNames |
| 33 | + { |
| 34 | + StandardNameIdx, |
| 35 | + DaylightNameIdx |
| 36 | + }; |
| 37 | + |
| 38 | + static List<AdjustmentRule> CreateAdjustmentRule (int year, out Int64[] data, out string[] names, string standardNameCurrentYear, string daylightNameCurrentYear) |
| 39 | + { |
| 40 | + List<AdjustmentRule> rulesForYear = new List<AdjustmentRule> (); |
| 41 | + bool dst_inverted; |
| 42 | + if (!System.CurrentSystemTimeZone.GetTimeZoneData(year, out data, out names, out dst_inverted)) |
| 43 | + return rulesForYear; |
| 44 | + var firstTransition = new DateTime (data[(int)TimeZoneData.DaylightSavingFirstTransitionIdx]); |
| 45 | + var secondTransition = new DateTime (data[(int)TimeZoneData.DaylightSavingSecondTransitionIdx]); |
| 46 | + var daylightOffset = new TimeSpan (data[(int)TimeZoneData.AdditionalDaylightOffsetIdx]); |
| 47 | + |
| 48 | + /* C# TimeZoneInfo does not support timezones the same way as unix. In unix, timezone files are specified by region such as |
| 49 | + * America/New_York or Asia/Singapore. If a region like Asia/Singapore changes it's timezone from +0730 to +08, the UTC offset |
| 50 | + * has changed, but there is no support in the C# code to transition to this new UTC offset except for the case of daylight |
| 51 | + * savings time. As such we'll only generate timezone rules for a region at the times associated with the timezone of the current year. |
| 52 | + */ |
| 53 | + if(standardNameCurrentYear != names[(int)TimeZoneNames.StandardNameIdx]) |
| 54 | + return rulesForYear; |
| 55 | + if(daylightNameCurrentYear != names[(int)TimeZoneNames.DaylightNameIdx]) |
| 56 | + return rulesForYear; |
| 57 | + |
| 58 | + // If the first and second transition DateTime objects are the same, ValidateAdjustmentRule will throw |
| 59 | + // an exception. I'm unsure why these would be the same, but we do see that occur for some locales. |
| 60 | + // In that case, just exit early. |
| 61 | + if (firstTransition.Equals(secondTransition)) |
| 62 | + return rulesForYear; |
| 63 | + |
| 64 | + var beginningOfYear = new DateTime (year, 1, 1, 0, 0, 0, 0); |
| 65 | + var endOfYearDay = new DateTime (year, 12, DateTime.DaysInMonth (year, 12)); |
| 66 | + var endOfYearMaxTimeout = new DateTime (year, 12, DateTime.DaysInMonth(year, 12), 23, 59, 59, 999); |
| 67 | + |
| 68 | + if (!dst_inverted) { |
| 69 | + // For daylight savings time that happens between jan and dec, create a rule from jan 1 to dec 31 (the entire year) |
| 70 | + |
| 71 | + // This rule (for the whole year) specifies the starting and ending months of daylight savings time. |
| 72 | + var startOfDaylightSavingsTime = TransitionTime.CreateFixedDateRule (new DateTime (1,1,1).Add (firstTransition.TimeOfDay), |
| 73 | + firstTransition.Month, firstTransition.Day); |
| 74 | + var endOfDaylightSavingsTime = TransitionTime.CreateFixedDateRule (new DateTime (1,1,1).Add (secondTransition.TimeOfDay), |
| 75 | + secondTransition.Month, secondTransition.Day); |
| 76 | + |
| 77 | + var fullYearRule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (beginningOfYear, |
| 78 | + endOfYearDay, |
| 79 | + daylightOffset, |
| 80 | + startOfDaylightSavingsTime, |
| 81 | + endOfDaylightSavingsTime); |
| 82 | + rulesForYear.Add (fullYearRule); |
| 83 | + } else { |
| 84 | + // Some timezones (Australia/Sydney) have daylight savings over the new year. |
| 85 | + // Our icall returns the transitions for the current year, so we need two adjustment rules each year for this case |
| 86 | + |
| 87 | + // The first rule specifies daylight savings starting at jan 1 and ending at the first transition. |
| 88 | + var startOfFirstDaylightSavingsTime = TransitionTime.CreateFixedDateRule (new DateTime (1,1,1), 1, 1); |
| 89 | + var endOfFirstDaylightSavingsTime = TransitionTime.CreateFixedDateRule (new DateTime (1,1,1).Add (firstTransition.TimeOfDay), |
| 90 | + firstTransition.Month, firstTransition.Day); |
| 91 | + |
| 92 | + var transitionOutOfDaylightSavingsRule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule ( |
| 93 | + new DateTime (year, 1, 1), |
| 94 | + new DateTime (firstTransition.Year, firstTransition.Month, firstTransition.Day), |
| 95 | + daylightOffset, |
| 96 | + startOfFirstDaylightSavingsTime, |
| 97 | + endOfFirstDaylightSavingsTime); |
| 98 | + rulesForYear.Add (transitionOutOfDaylightSavingsRule); |
| 99 | + |
| 100 | + // The second rule specifies daylight savings time starting the day after we transition out of daylight savings |
| 101 | + // and ending at the end of the year, with daylight savings starting near the end and ending on the last day of the year |
| 102 | + var startOfSecondDaylightSavingsTime = TransitionTime.CreateFixedDateRule (new DateTime (1,1,1).Add (secondTransition.TimeOfDay), |
| 103 | + secondTransition.Month, secondTransition.Day); |
| 104 | + var endOfSecondDaylightSavingsTime = TransitionTime.CreateFixedDateRule (new DateTime (1,1,1).Add (endOfYearMaxTimeout.TimeOfDay), |
| 105 | + endOfYearMaxTimeout.Month, endOfYearMaxTimeout.Day); |
| 106 | + |
| 107 | + var transitionIntoDaylightSavingsRule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule ( |
| 108 | + new DateTime (firstTransition.Year, firstTransition.Month, firstTransition.Day).AddDays (1), |
| 109 | + endOfYearDay, |
| 110 | + daylightOffset, |
| 111 | + startOfSecondDaylightSavingsTime, |
| 112 | + endOfSecondDaylightSavingsTime); |
| 113 | + rulesForYear.Add (transitionIntoDaylightSavingsRule); |
| 114 | + } |
| 115 | + return rulesForYear; |
| 116 | + } |
| 117 | + |
| 118 | + static TimeZoneInfo CreateLocalUnity () |
| 119 | + { |
| 120 | + Int64[] data; |
| 121 | + string[] names; |
| 122 | + //Some timezones start in DST on january first and disable it during the summer |
| 123 | + bool dst_inverted; |
| 124 | + int currentYear = DateTime.UtcNow.Year; |
| 125 | + if (!System.CurrentSystemTimeZone.GetTimeZoneData (currentYear, out data, out names, out dst_inverted)) |
| 126 | + throw new NotSupportedException ("Can't get timezone name."); |
| 127 | + |
| 128 | + var utcOffsetTS = TimeSpan.FromTicks (data[(int)TimeZoneData.UtcOffsetIdx]); |
| 129 | + char utcOffsetSign = (utcOffsetTS >= TimeSpan.Zero) ? '+' : '-'; |
| 130 | + string displayName = "(GMT" + utcOffsetSign + utcOffsetTS.ToString (@"hh\:mm") + ") Local Time"; |
| 131 | + string standardDisplayName = names[(int)TimeZoneNames.StandardNameIdx]; |
| 132 | + string daylightDisplayName = names[(int)TimeZoneNames.DaylightNameIdx]; |
| 133 | + |
| 134 | + var adjustmentRulesList = new List<AdjustmentRule> (); |
| 135 | + bool disableDaylightSavings = data[(int)TimeZoneData.AdditionalDaylightOffsetIdx] == 0; |
| 136 | + //If the timezone supports daylight savings time, generate adjustment rules for the timezone |
| 137 | + if (!disableDaylightSavings) { |
| 138 | + //the icall only supports years from 1970 through 2037. |
| 139 | + int firstSupportedDate = 1971; |
| 140 | + int lastSupportedDate = 2037; |
| 141 | + |
| 142 | + //first, generate rules from the current year until the last year mktime is guaranteed to supports |
| 143 | + for (int year = currentYear; year <= lastSupportedDate; year++) { |
| 144 | + var rulesForCurrentYear = CreateAdjustmentRule (year, out data, out names, standardDisplayName, daylightDisplayName); |
| 145 | + //breakout if no more rules |
| 146 | + if (rulesForCurrentYear.Count > 0) |
| 147 | + adjustmentRulesList.AddRange (rulesForCurrentYear); |
| 148 | + else |
| 149 | + break; |
| 150 | + |
| 151 | + } |
| 152 | + |
| 153 | + for (int year = currentYear - 1; year >= firstSupportedDate; year--) { |
| 154 | + var rulesForCurrentYear = CreateAdjustmentRule (year, out data, out names, standardDisplayName, daylightDisplayName); |
| 155 | + //breakout if no more rules |
| 156 | + if (rulesForCurrentYear.Count > 0) |
| 157 | + adjustmentRulesList.AddRange (rulesForCurrentYear); |
| 158 | + else |
| 159 | + break; |
| 160 | + } |
| 161 | + |
| 162 | + adjustmentRulesList.Sort ( (rule1, rule2) => rule1.DateStart.CompareTo (rule2.DateStart) ); |
| 163 | + } |
| 164 | + return TimeZoneInfo.CreateCustomTimeZone ("Local", |
| 165 | + utcOffsetTS, |
| 166 | + displayName, |
| 167 | + standardDisplayName, |
| 168 | + daylightDisplayName, |
| 169 | + adjustmentRulesList.ToArray (), |
| 170 | + disableDaylightSavings); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + partial class TimeZone |
| 175 | + { |
| 176 | + // Internal method to get timezone data. |
| 177 | + // data[0]: start of daylight saving time (in DateTime ticks). |
| 178 | + // data[1]: end of daylight saving time (in DateTime ticks). |
| 179 | + // data[2]: utcoffset (in TimeSpan ticks). |
| 180 | + // data[3]: additional offset when daylight saving (in TimeSpan ticks). |
| 181 | + // name[0]: name of this timezone when not daylight saving. |
| 182 | + // name[1]: name of this timezone when daylight saving. |
| 183 | + [MethodImplAttribute(MethodImplOptions.InternalCall)] |
| 184 | + public static extern bool GetTimeZoneData (int year, out Int64[] data, out string[] names, out bool daylight_inverted); |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +#endif |
0 commit comments