| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703 |
- using Ryujinx.Common;
- using Ryujinx.Common.Memory;
- using Ryujinx.Common.Utilities;
- using Ryujinx.HLE.Utilities;
- using System;
- using System.Buffers.Binary;
- using System.IO;
- using System.Runtime.CompilerServices;
- using System.Runtime.InteropServices;
- using System.Text;
- using static Ryujinx.HLE.HOS.Services.Time.TimeZone.TimeZoneRule;
- namespace Ryujinx.HLE.HOS.Services.Time.TimeZone
- {
- public class TimeZone
- {
- private const int TimeTypeSize = 8;
- private const int EpochYear = 1970;
- private const int YearBase = 1900;
- private const int EpochWeekDay = 4;
- private const int SecondsPerMinute = 60;
- private const int MinutesPerHour = 60;
- private const int HoursPerDays = 24;
- private const int DaysPerWekk = 7;
- private const int DaysPerNYear = 365;
- private const int DaysPerLYear = 366;
- private const int MonthsPerYear = 12;
- private const int SecondsPerHour = SecondsPerMinute * MinutesPerHour;
- private const int SecondsPerDay = SecondsPerHour * HoursPerDays;
- private const int YearsPerRepeat = 400;
- private const long AverageSecondsPerYear = 31556952;
- private const long SecondsPerRepeat = YearsPerRepeat * AverageSecondsPerYear;
- private static readonly int[] YearLengths = { DaysPerNYear, DaysPerLYear };
- private static readonly int[][] MonthsLengths = new int[][]
- {
- new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
- new int[] { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
- };
- private static ReadOnlySpan<byte> TimeZoneDefaultRule => ",M4.1.0,M10.5.0"u8;
- [StructLayout(LayoutKind.Sequential, Pack = 0x4, Size = 0x10)]
- private struct CalendarTimeInternal
- {
- // NOTE: On the IPC side this is supposed to be a 16 bits value but internally this need to be a 64 bits value for ToPosixTime.
- public long Year;
- public sbyte Month;
- public sbyte Day;
- public sbyte Hour;
- public sbyte Minute;
- public sbyte Second;
- public int CompareTo(CalendarTimeInternal other)
- {
- if (Year != other.Year)
- {
- if (Year < other.Year)
- {
- return -1;
- }
- return 1;
- }
- if (Month != other.Month)
- {
- return Month - other.Month;
- }
- if (Day != other.Day)
- {
- return Day - other.Day;
- }
- if (Hour != other.Hour)
- {
- return Hour - other.Hour;
- }
- if (Minute != other.Minute)
- {
- return Minute - other.Minute;
- }
- if (Second != other.Second)
- {
- return Second - other.Second;
- }
- return 0;
- }
- }
- private enum RuleType
- {
- JulianDay,
- DayOfYear,
- MonthNthDayOfWeek
- }
- private struct Rule
- {
- public RuleType Type;
- public int Day;
- public int Week;
- public int Month;
- public int TransitionTime;
- }
- private static int Detzcode32(ReadOnlySpan<byte> bytes)
- {
- return BinaryPrimitives.ReadInt32BigEndian(bytes);
- }
- private static int Detzcode32(int value)
- {
- if (BitConverter.IsLittleEndian)
- {
- return BinaryPrimitives.ReverseEndianness(value);
- }
- return value;
- }
- private static long Detzcode64(ReadOnlySpan<byte> bytes)
- {
- return BinaryPrimitives.ReadInt64BigEndian(bytes);
- }
- private static bool DifferByRepeat(long t1, long t0)
- {
- return (t1 - t0) == SecondsPerRepeat;
- }
- private static bool TimeTypeEquals(in TimeZoneRule outRules, byte aIndex, byte bIndex)
- {
- if (aIndex < 0 || aIndex >= outRules.TypeCount || bIndex < 0 || bIndex >= outRules.TypeCount)
- {
- return false;
- }
- TimeTypeInfo a = outRules.Ttis[aIndex];
- TimeTypeInfo b = outRules.Ttis[bIndex];
- return a.GmtOffset == b.GmtOffset &&
- a.IsDaySavingTime == b.IsDaySavingTime &&
- a.IsStandardTimeDaylight == b.IsStandardTimeDaylight &&
- a.IsGMT == b.IsGMT &&
- StringUtils.CompareCStr(outRules.Chars[a.AbbreviationListIndex..], outRules.Chars[b.AbbreviationListIndex..]) == 0;
- }
- private static int GetQZName(ReadOnlySpan<byte> name, int namePosition, char delimiter)
- {
- int i = namePosition;
- while (name[i] != '\0' && name[i] != delimiter)
- {
- i++;
- }
- return i;
- }
- private static int GetTZName(ReadOnlySpan<byte> name, int namePosition)
- {
- int i = namePosition;
- char c;
- while ((c = (char)name[i]) != '\0' && !char.IsDigit(c) && c != ',' && c != '-' && c != '+')
- {
- i++;
- }
- return i;
- }
- private static bool GetNum(ReadOnlySpan<byte> name, ref int namePosition, out int num, int min, int max)
- {
- num = 0;
- if (namePosition >= name.Length)
- {
- return false;
- }
- char c = (char)name[namePosition];
- if (!char.IsDigit(c))
- {
- return false;
- }
- do
- {
- num = num * 10 + (c - '0');
- if (num > max)
- {
- return false;
- }
- if (++namePosition >= name.Length)
- {
- return false;
- }
- c = (char)name[namePosition];
- }
- while (char.IsDigit(c));
- if (num < min)
- {
- return false;
- }
- return true;
- }
- private static bool GetSeconds(ReadOnlySpan<byte> name, ref int namePosition, out int seconds)
- {
- seconds = 0;
- bool isValid = GetNum(name, ref namePosition, out int num, 0, HoursPerDays * DaysPerWekk - 1);
- if (!isValid)
- {
- return false;
- }
- seconds = num * SecondsPerHour;
- if (namePosition >= name.Length)
- {
- return false;
- }
- if (name[namePosition] == ':')
- {
- namePosition++;
- isValid = GetNum(name, ref namePosition, out num, 0, MinutesPerHour - 1);
- if (!isValid)
- {
- return false;
- }
- seconds += num * SecondsPerMinute;
- if (namePosition >= name.Length)
- {
- return false;
- }
- if (name[namePosition] == ':')
- {
- namePosition++;
- isValid = GetNum(name, ref namePosition, out num, 0, SecondsPerMinute);
- if (!isValid)
- {
- return false;
- }
- seconds += num;
- }
- }
- return true;
- }
- private static bool GetOffset(ReadOnlySpan<byte> name, ref int namePosition, ref int offset)
- {
- bool isNegative = false;
- if (namePosition >= name.Length)
- {
- return false;
- }
- if (name[namePosition] == '-')
- {
- isNegative = true;
- namePosition++;
- }
- else if (name[namePosition] == '+')
- {
- namePosition++;
- }
- if (namePosition >= name.Length)
- {
- return false;
- }
- bool isValid = GetSeconds(name, ref namePosition, out offset);
- if (!isValid)
- {
- return false;
- }
- if (isNegative)
- {
- offset = -offset;
- }
- return true;
- }
- private static bool GetRule(ReadOnlySpan<byte> name, ref int namePosition, out Rule rule)
- {
- rule = new Rule();
- bool isValid = false;
- if (name[namePosition] == 'J')
- {
- namePosition++;
- rule.Type = RuleType.JulianDay;
- isValid = GetNum(name, ref namePosition, out rule.Day, 1, DaysPerNYear);
- }
- else if (name[namePosition] == 'M')
- {
- namePosition++;
- rule.Type = RuleType.MonthNthDayOfWeek;
- isValid = GetNum(name, ref namePosition, out rule.Month, 1, MonthsPerYear);
- if (!isValid)
- {
- return false;
- }
- if (name[namePosition++] != '.')
- {
- return false;
- }
- isValid = GetNum(name, ref namePosition, out rule.Week, 1, 5);
- if (!isValid)
- {
- return false;
- }
- if (name[namePosition++] != '.')
- {
- return false;
- }
- isValid = GetNum(name, ref namePosition, out rule.Day, 0, DaysPerWekk - 1);
- }
- else if (char.IsDigit((char)name[namePosition]))
- {
- rule.Type = RuleType.DayOfYear;
- isValid = GetNum(name, ref namePosition, out rule.Day, 0, DaysPerLYear - 1);
- }
- else
- {
- return false;
- }
- if (!isValid)
- {
- return false;
- }
- if (name[namePosition] == '/')
- {
- namePosition++;
- return GetOffset(name, ref namePosition, ref rule.TransitionTime);
- }
- else
- {
- rule.TransitionTime = 2 * SecondsPerHour;
- }
- return true;
- }
- private static int IsLeap(int year)
- {
- if (((year) % 4) == 0 && (((year) % 100) != 0 || ((year) % 400) == 0))
- {
- return 1;
- }
- return 0;
- }
- private static bool ParsePosixName(ReadOnlySpan<byte> name, ref TimeZoneRule outRules, bool lastDitch)
- {
- outRules = new TimeZoneRule();
- int stdLen;
- ReadOnlySpan<byte> stdName = name;
- int namePosition = 0;
- int stdOffset = 0;
- if (lastDitch)
- {
- stdLen = 3;
- namePosition += stdLen;
- }
- else
- {
- if (name[namePosition] == '<')
- {
- namePosition++;
- stdName = name.Slice(namePosition);
- int stdNamePosition = namePosition;
- namePosition = GetQZName(name, namePosition, '>');
- if (name[namePosition] != '>')
- {
- return false;
- }
- stdLen = namePosition - stdNamePosition;
- namePosition++;
- }
- else
- {
- namePosition = GetTZName(name, namePosition);
- stdLen = namePosition;
- }
- if (stdLen == 0)
- {
- return false;
- }
- bool isValid = GetOffset(name.ToArray(), ref namePosition, ref stdOffset);
- if (!isValid)
- {
- return false;
- }
- }
- int charCount = stdLen + 1;
- int destLen = 0;
- int dstOffset = 0;
- ReadOnlySpan<byte> destName = name.Slice(namePosition);
- if (TzCharsArraySize < charCount)
- {
- return false;
- }
- if (name[namePosition] != '\0')
- {
- if (name[namePosition] == '<')
- {
- destName = name.Slice(++namePosition);
- int destNamePosition = namePosition;
- namePosition = GetQZName(name.ToArray(), namePosition, '>');
- if (name[namePosition] != '>')
- {
- return false;
- }
- destLen = namePosition - destNamePosition;
- namePosition++;
- }
- else
- {
- destName = name.Slice(namePosition);
- namePosition = GetTZName(name, namePosition);
- destLen = namePosition;
- }
- if (destLen == 0)
- {
- return false;
- }
- charCount += destLen + 1;
- if (TzCharsArraySize < charCount)
- {
- return false;
- }
- if (name[namePosition] != '\0' && name[namePosition] != ',' && name[namePosition] != ';')
- {
- bool isValid = GetOffset(name.ToArray(), ref namePosition, ref dstOffset);
- if (!isValid)
- {
- return false;
- }
- }
- else
- {
- dstOffset = stdOffset - SecondsPerHour;
- }
- if (name[namePosition] == '\0')
- {
- name = TimeZoneDefaultRule;
- namePosition = 0;
- }
- if (name[namePosition] == ',' || name[namePosition] == ';')
- {
- namePosition++;
- bool IsRuleValid = GetRule(name, ref namePosition, out Rule start);
- if (!IsRuleValid)
- {
- return false;
- }
- if (name[namePosition++] != ',')
- {
- return false;
- }
- IsRuleValid = GetRule(name, ref namePosition, out Rule end);
- if (!IsRuleValid)
- {
- return false;
- }
- if (name[namePosition] != '\0')
- {
- return false;
- }
- outRules.TypeCount = 2;
- outRules.Ttis[0] = new TimeTypeInfo
- {
- GmtOffset = -dstOffset,
- IsDaySavingTime = true,
- AbbreviationListIndex = stdLen + 1
- };
- outRules.Ttis[1] = new TimeTypeInfo
- {
- GmtOffset = -stdOffset,
- IsDaySavingTime = false,
- AbbreviationListIndex = 0
- };
- outRules.DefaultType = 0;
- int timeCount = 0;
- long janFirst = 0;
- int janOffset = 0;
- int yearBegining = EpochYear;
- do
- {
- int yearSeconds = YearLengths[IsLeap(yearBegining - 1)] * SecondsPerDay;
- yearBegining--;
- if (IncrementOverflow64(ref janFirst, -yearSeconds))
- {
- janOffset = -yearSeconds;
- break;
- }
- }
- while (EpochYear - YearsPerRepeat / 2 < yearBegining);
- int yearLimit = yearBegining + YearsPerRepeat + 1;
- int year;
- for (year = yearBegining; year < yearLimit; year++)
- {
- int startTime = TransitionTime(year, start, stdOffset);
- int endTime = TransitionTime(year, end, dstOffset);
- int yearSeconds = YearLengths[IsLeap(year)] * SecondsPerDay;
- bool isReversed = endTime < startTime;
- if (isReversed)
- {
- int swap = startTime;
- startTime = endTime;
- endTime = swap;
- }
- if (isReversed || (startTime < endTime && (endTime - startTime < (yearSeconds + (stdOffset - dstOffset)))))
- {
- if (TzMaxTimes - 2 < timeCount)
- {
- break;
- }
- outRules.Ats[timeCount] = janFirst;
- if (!IncrementOverflow64(ref outRules.Ats[timeCount], janOffset + startTime))
- {
- outRules.Types[timeCount++] = isReversed ? (byte)1 : (byte)0;
- }
- else if (janOffset != 0)
- {
- outRules.DefaultType = isReversed ? 1 : 0;
- }
- outRules.Ats[timeCount] = janFirst;
- if (!IncrementOverflow64(ref outRules.Ats[timeCount], janOffset + endTime))
- {
- outRules.Types[timeCount++] = isReversed ? (byte)0 : (byte)1;
- yearLimit = year + YearsPerRepeat + 1;
- }
- else if (janOffset != 0)
- {
- outRules.DefaultType = isReversed ? 0 : 1;
- }
- }
- if (IncrementOverflow64(ref janFirst, janOffset + yearSeconds))
- {
- break;
- }
- janOffset = 0;
- }
- outRules.TimeCount = timeCount;
- // There is no time variation, this is then a perpetual DST rule
- if (timeCount == 0)
- {
- outRules.TypeCount = 1;
- }
- else if (YearsPerRepeat < year - yearBegining)
- {
- outRules.GoBack = true;
- outRules.GoAhead = true;
- }
- }
- else
- {
- if (name[namePosition] == '\0')
- {
- return false;
- }
- long theirStdOffset = 0;
- for (int i = 0; i < outRules.TimeCount; i++)
- {
- int j = outRules.Types[i];
- if (outRules.Ttis[j].IsStandardTimeDaylight)
- {
- theirStdOffset = -outRules.Ttis[j].GmtOffset;
- }
- }
- long theirDstOffset = 0;
- for (int i = 0; i < outRules.TimeCount; i++)
- {
- int j = outRules.Types[i];
- if (outRules.Ttis[j].IsDaySavingTime)
- {
- theirDstOffset = -outRules.Ttis[j].GmtOffset;
- }
- }
- bool isDaySavingTime = false;
- long theirOffset = theirStdOffset;
- for (int i = 0; i < outRules.TimeCount; i++)
- {
- int j = outRules.Types[i];
- outRules.Types[i] = outRules.Ttis[j].IsDaySavingTime ? (byte)1 : (byte)0;
- if (!outRules.Ttis[j].IsGMT)
- {
- if (isDaySavingTime && !outRules.Ttis[j].IsStandardTimeDaylight)
- {
- outRules.Ats[i] += dstOffset - theirStdOffset;
- }
- else
- {
- outRules.Ats[i] += stdOffset - theirStdOffset;
- }
- }
- theirOffset = -outRules.Ttis[j].GmtOffset;
- if (outRules.Ttis[j].IsDaySavingTime)
- {
- theirDstOffset = theirOffset;
- }
- else
- {
- theirStdOffset = theirOffset;
- }
- }
- outRules.Ttis[0] = new TimeTypeInfo
- {
- GmtOffset = -stdOffset,
- IsDaySavingTime = false,
- AbbreviationListIndex = 0
- };
- outRules.Ttis[1] = new TimeTypeInfo
- {
- GmtOffset = -dstOffset,
- IsDaySavingTime = true,
- AbbreviationListIndex = stdLen + 1
- };
- outRules.TypeCount = 2;
- outRules.DefaultType = 0;
- }
- }
- else
- {
- // default is perpetual standard time
- outRules.TypeCount = 1;
- outRules.TimeCount = 0;
- outRules.DefaultType = 0;
- outRules.Ttis[0] = new TimeTypeInfo
- {
- GmtOffset = -stdOffset,
- IsDaySavingTime = false,
- AbbreviationListIndex = 0
- };
- }
- outRules.CharCount = charCount;
- int charsPosition = 0;
- for (int i = 0; i < stdLen; i++)
- {
- outRules.Chars[i] = stdName[i];
- }
- charsPosition += stdLen;
- outRules.Chars[charsPosition++] = 0;
- if (destLen != 0)
- {
- for (int i = 0; i < destLen; i++)
- {
- outRules.Chars[charsPosition + i] = destName[i];
- }
- outRules.Chars[charsPosition + destLen] = 0;
- }
- return true;
- }
- private static int TransitionTime(int year, Rule rule, int offset)
- {
- int leapYear = IsLeap(year);
- int value;
- switch (rule.Type)
- {
- case RuleType.JulianDay:
- value = (rule.Day - 1) * SecondsPerDay;
- if (leapYear == 1 && rule.Day >= 60)
- {
- value += SecondsPerDay;
- }
- break;
- case RuleType.DayOfYear:
- value = rule.Day * SecondsPerDay;
- break;
- case RuleType.MonthNthDayOfWeek:
- // Here we use Zeller's Congruence to get the day of week of the first month.
- int m1 = (rule.Month + 9) % 12 + 1;
- int yy0 = (rule.Month <= 2) ? (year - 1) : year;
- int yy1 = yy0 / 100;
- int yy2 = yy0 % 100;
- int dayOfWeek = ((26 * m1 - 2) / 10 + 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
- if (dayOfWeek < 0)
- {
- dayOfWeek += DaysPerWekk;
- }
- // Get the zero origin
- int d = rule.Day - dayOfWeek;
- if (d < 0)
- {
- d += DaysPerWekk;
- }
- for (int i = 1; i < rule.Week; i++)
- {
- if (d + DaysPerWekk >= MonthsLengths[leapYear][rule.Month - 1])
- {
- break;
- }
- d += DaysPerWekk;
- }
- value = d * SecondsPerDay;
- for (int i = 0; i < rule.Month - 1; i++)
- {
- value += MonthsLengths[leapYear][i] * SecondsPerDay;
- }
- break;
- default:
- throw new NotImplementedException("Unknown time transition!");
- }
- return value + rule.TransitionTime + offset;
- }
- private static bool NormalizeOverflow32(ref int ip, ref int unit, int baseValue)
- {
- int delta;
- if (unit >= 0)
- {
- delta = unit / baseValue;
- }
- else
- {
- delta = -1 - (-1 - unit) / baseValue;
- }
- unit -= delta * baseValue;
- return IncrementOverflow32(ref ip, delta);
- }
- private static bool NormalizeOverflow64(ref long ip, ref long unit, long baseValue)
- {
- long delta;
- if (unit >= 0)
- {
- delta = unit / baseValue;
- }
- else
- {
- delta = -1 - (-1 - unit) / baseValue;
- }
- unit -= delta * baseValue;
- return IncrementOverflow64(ref ip, delta);
- }
- private static bool IncrementOverflow32(ref int time, int j)
- {
- try
- {
- time = checked(time + j);
- return false;
- }
- catch (OverflowException)
- {
- return true;
- }
- }
- private static bool IncrementOverflow64(ref long time, long j)
- {
- try
- {
- time = checked(time + j);
- return false;
- }
- catch (OverflowException)
- {
- return true;
- }
- }
- internal static bool ParsePosixName(string name, ref TimeZoneRule outRules)
- {
- return ParsePosixName(Encoding.ASCII.GetBytes(name), ref outRules, false);
- }
- internal static bool ParseTimeZoneBinary(ref TimeZoneRule outRules, Stream inputData)
- {
- outRules = new TimeZoneRule();
- BinaryReader reader = new BinaryReader(inputData);
- long streamLength = reader.BaseStream.Length;
- if (streamLength < Unsafe.SizeOf<TzifHeader>())
- {
- return false;
- }
- TzifHeader header = reader.ReadStruct<TzifHeader>();
- streamLength -= Unsafe.SizeOf<TzifHeader>();
- int ttisGMTCount = Detzcode32(header.TtisGMTCount);
- int ttisSTDCount = Detzcode32(header.TtisSTDCount);
- int leapCount = Detzcode32(header.LeapCount);
- int timeCount = Detzcode32(header.TimeCount);
- int typeCount = Detzcode32(header.TypeCount);
- int charCount = Detzcode32(header.CharCount);
- if (!(0 <= leapCount
- && leapCount < TzMaxLeaps
- && 0 < typeCount
- && typeCount < TzMaxTypes
- && 0 <= timeCount
- && timeCount < TzMaxTimes
- && 0 <= charCount
- && charCount < TzMaxChars
- && (ttisSTDCount == typeCount || ttisSTDCount == 0)
- && (ttisGMTCount == typeCount || ttisGMTCount == 0)))
- {
- return false;
- }
- if (streamLength < (timeCount * TimeTypeSize
- + timeCount
- + typeCount * 6
- + charCount
- + leapCount * (TimeTypeSize + 4)
- + ttisSTDCount
- + ttisGMTCount))
- {
- return false;
- }
- outRules.TimeCount = timeCount;
- outRules.TypeCount = typeCount;
- outRules.CharCount = charCount;
- byte[] workBuffer = StreamUtils.StreamToBytes(inputData);
- timeCount = 0;
- {
- Span<byte> p = workBuffer;
- for (int i = 0; i < outRules.TimeCount; i++)
- {
- long at = Detzcode64(p);
- outRules.Types[i] = 1;
- if (timeCount != 0 && at <= outRules.Ats[timeCount - 1])
- {
- if (at < outRules.Ats[timeCount - 1])
- {
- return false;
- }
- outRules.Types[i - 1] = 0;
- timeCount--;
- }
- outRules.Ats[timeCount++] = at;
- p = p[TimeTypeSize..];
- }
- timeCount = 0;
- for (int i = 0; i < outRules.TimeCount; i++)
- {
- byte type = p[0];
- p = p[1..];
- if (outRules.TypeCount <= type)
- {
- return false;
- }
- if (outRules.Types[i] != 0)
- {
- outRules.Types[timeCount++] = type;
- }
- }
- outRules.TimeCount = timeCount;
- for (int i = 0; i < outRules.TypeCount; i++)
- {
- TimeTypeInfo ttis = outRules.Ttis[i];
- ttis.GmtOffset = Detzcode32(p);
- p = p[sizeof(int)..];
- if (p[0] >= 2)
- {
- return false;
- }
- ttis.IsDaySavingTime = p[0] != 0;
- p = p[1..];
- int abbreviationListIndex = p[0];
- p = p[1..];
- if (abbreviationListIndex >= outRules.CharCount)
- {
- return false;
- }
- ttis.AbbreviationListIndex = abbreviationListIndex;
- outRules.Ttis[i] = ttis;
- }
- p[..outRules.CharCount].CopyTo(outRules.Chars);
- p = p[outRules.CharCount..];
- outRules.Chars[outRules.CharCount] = 0;
- for (int i = 0; i < outRules.TypeCount; i++)
- {
- if (ttisSTDCount == 0)
- {
- outRules.Ttis[i].IsStandardTimeDaylight = false;
- }
- else
- {
- if (p[0] >= 2)
- {
- return false;
- }
- outRules.Ttis[i].IsStandardTimeDaylight = p[0] != 0;
- p = p[1..];
- }
- }
- for (int i = 0; i < outRules.TypeCount; i++)
- {
- if (ttisSTDCount == 0)
- {
- outRules.Ttis[i].IsGMT = false;
- }
- else
- {
- if (p[0] >= 2)
- {
- return false;
- }
- outRules.Ttis[i].IsGMT = p[0] != 0;
- p = p[1..];
- }
- }
- long position = (workBuffer.Length - p.Length);
- long nRead = streamLength - position;
- if (nRead < 0)
- {
- return false;
- }
- // Nintendo abort in case of a TzIf file with a POSIX TZ Name too long to fit inside a TimeZoneRule.
- // As it's impossible in normal usage to achive this, we also force a crash.
- if (nRead > (TzNameMax + 1))
- {
- throw new InvalidOperationException();
- }
- byte[] tempName = new byte[TzNameMax + 1];
- Array.Copy(workBuffer, position, tempName, 0, nRead);
- if (nRead > 2 && tempName[0] == '\n' && tempName[nRead - 1] == '\n' && outRules.TypeCount + 2 <= TzMaxTypes)
- {
- tempName[nRead - 1] = 0;
- byte[] name = new byte[TzNameMax];
- Array.Copy(tempName, 1, name, 0, nRead - 1);
- Box<TimeZoneRule> tempRulesBox = new Box<TimeZoneRule>();
- ref TimeZoneRule tempRules = ref tempRulesBox.Data;
- if (ParsePosixName(name, ref tempRulesBox.Data, false))
- {
- int abbreviationCount = 0;
- charCount = outRules.CharCount;
- Span<byte> chars = outRules.Chars;
- for (int i = 0; i < tempRules.TypeCount; i++)
- {
- ReadOnlySpan<byte> tempChars = tempRules.Chars;
- ReadOnlySpan<byte> tempAbbreviation = tempChars[tempRules.Ttis[i].AbbreviationListIndex..];
- int j;
- for (j = 0; j < charCount; j++)
- {
- if (StringUtils.CompareCStr(chars[j..], tempAbbreviation) == 0)
- {
- tempRules.Ttis[i].AbbreviationListIndex = j;
- abbreviationCount++;
- break;
- }
- }
- if (j >= charCount)
- {
- int abbreviationLength = StringUtils.LengthCstr(tempAbbreviation);
- if (j + abbreviationLength < TzMaxChars)
- {
- for (int x = 0; x < abbreviationLength; x++)
- {
- chars[j + x] = tempAbbreviation[x];
- }
- charCount = j + abbreviationLength + 1;
- tempRules.Ttis[i].AbbreviationListIndex = j;
- abbreviationCount++;
- }
- }
- }
- if (abbreviationCount == tempRules.TypeCount)
- {
- outRules.CharCount = charCount;
- // Remove trailing
- while (1 < outRules.TimeCount && (outRules.Types[outRules.TimeCount - 1] == outRules.Types[outRules.TimeCount - 2]))
- {
- outRules.TimeCount--;
- }
- int i;
- for (i = 0; i < tempRules.TimeCount; i++)
- {
- if (outRules.TimeCount == 0 || outRules.Ats[outRules.TimeCount - 1] < tempRules.Ats[i])
- {
- break;
- }
- }
- while (i < tempRules.TimeCount && outRules.TimeCount < TzMaxTimes)
- {
- outRules.Ats[outRules.TimeCount] = tempRules.Ats[i];
- outRules.Types[outRules.TimeCount] = (byte)(outRules.TypeCount + (byte)tempRules.Types[i]);
- outRules.TimeCount++;
- i++;
- }
- for (i = 0; i < tempRules.TypeCount; i++)
- {
- outRules.Ttis[outRules.TypeCount++] = tempRules.Ttis[i];
- }
- }
- }
- }
- if (outRules.TypeCount == 0)
- {
- return false;
- }
- if (outRules.TimeCount > 1)
- {
- for (int i = 1; i < outRules.TimeCount; i++)
- {
- if (TimeTypeEquals(in outRules, outRules.Types[i], outRules.Types[0]) && DifferByRepeat(outRules.Ats[i], outRules.Ats[0]))
- {
- outRules.GoBack = true;
- break;
- }
- }
- for (int i = outRules.TimeCount - 2; i >= 0; i--)
- {
- if (TimeTypeEquals(in outRules, outRules.Types[outRules.TimeCount - 1], outRules.Types[i]) && DifferByRepeat(outRules.Ats[outRules.TimeCount - 1], outRules.Ats[i]))
- {
- outRules.GoAhead = true;
- break;
- }
- }
- }
- int defaultType;
- for (defaultType = 0; defaultType < outRules.TimeCount; defaultType++)
- {
- if (outRules.Types[defaultType] == 0)
- {
- break;
- }
- }
- defaultType = defaultType < outRules.TimeCount ? -1 : 0;
- if (defaultType < 0 && outRules.TimeCount > 0 && outRules.Ttis[outRules.Types[0]].IsDaySavingTime)
- {
- defaultType = outRules.Types[0];
- while (--defaultType >= 0)
- {
- if (!outRules.Ttis[defaultType].IsDaySavingTime)
- {
- break;
- }
- }
- }
- if (defaultType < 0)
- {
- defaultType = 0;
- while (outRules.Ttis[defaultType].IsDaySavingTime)
- {
- if (++defaultType >= outRules.TypeCount)
- {
- defaultType = 0;
- break;
- }
- }
- }
- outRules.DefaultType = defaultType;
- }
- return true;
- }
- private static long GetLeapDaysNotNeg(long year)
- {
- return year / 4 - year / 100 + year / 400;
- }
- private static long GetLeapDays(long year)
- {
- if (year < 0)
- {
- return -1 - GetLeapDaysNotNeg(-1 - year);
- }
- else
- {
- return GetLeapDaysNotNeg(year);
- }
- }
- private static ResultCode CreateCalendarTime(long time, int gmtOffset, out CalendarTimeInternal calendarTime, out CalendarAdditionalInfo calendarAdditionalInfo)
- {
- long year = EpochYear;
- long timeDays = time / SecondsPerDay;
- long remainingSeconds = time % SecondsPerDay;
- calendarTime = new CalendarTimeInternal();
- calendarAdditionalInfo = new CalendarAdditionalInfo();
- while (timeDays < 0 || timeDays >= YearLengths[IsLeap((int)year)])
- {
- long timeDelta = timeDays / DaysPerLYear;
- long delta = timeDelta;
- if (delta == 0)
- {
- delta = timeDays < 0 ? -1 : 1;
- }
- long newYear = year;
- if (IncrementOverflow64(ref newYear, delta))
- {
- return ResultCode.OutOfRange;
- }
- long leapDays = GetLeapDays(newYear - 1) - GetLeapDays(year - 1);
- timeDays -= (newYear - year) * DaysPerNYear;
- timeDays -= leapDays;
- year = newYear;
- }
- long dayOfYear = timeDays;
- remainingSeconds += gmtOffset;
- while (remainingSeconds < 0)
- {
- remainingSeconds += SecondsPerDay;
- dayOfYear -= 1;
- }
- while (remainingSeconds >= SecondsPerDay)
- {
- remainingSeconds -= SecondsPerDay;
- dayOfYear += 1;
- }
- while (dayOfYear < 0)
- {
- if (IncrementOverflow64(ref year, -1))
- {
- return ResultCode.OutOfRange;
- }
- dayOfYear += YearLengths[IsLeap((int)year)];
- }
- while (dayOfYear >= YearLengths[IsLeap((int)year)])
- {
- dayOfYear -= YearLengths[IsLeap((int)year)];
- if (IncrementOverflow64(ref year, 1))
- {
- return ResultCode.OutOfRange;
- }
- }
- calendarTime.Year = year;
- calendarAdditionalInfo.DayOfYear = (uint)dayOfYear;
- long dayOfWeek = (EpochWeekDay + ((year - EpochYear) % DaysPerWekk) * (DaysPerNYear % DaysPerWekk) + GetLeapDays(year - 1) - GetLeapDays(EpochYear - 1) + dayOfYear) % DaysPerWekk;
- if (dayOfWeek < 0)
- {
- dayOfWeek += DaysPerWekk;
- }
- calendarAdditionalInfo.DayOfWeek = (uint)dayOfWeek;
- calendarTime.Hour = (sbyte)((remainingSeconds / SecondsPerHour) % SecondsPerHour);
- remainingSeconds %= SecondsPerHour;
- calendarTime.Minute = (sbyte)(remainingSeconds / SecondsPerMinute);
- calendarTime.Second = (sbyte)(remainingSeconds % SecondsPerMinute);
- int[] ip = MonthsLengths[IsLeap((int)year)];
- for (calendarTime.Month = 0; dayOfYear >= ip[calendarTime.Month]; ++calendarTime.Month)
- {
- dayOfYear -= ip[calendarTime.Month];
- }
- calendarTime.Day = (sbyte)(dayOfYear + 1);
- calendarAdditionalInfo.IsDaySavingTime = false;
- calendarAdditionalInfo.GmtOffset = gmtOffset;
- return 0;
- }
- private static ResultCode ToCalendarTimeInternal(in TimeZoneRule rules, long time, out CalendarTimeInternal calendarTime, out CalendarAdditionalInfo calendarAdditionalInfo)
- {
- calendarTime = new CalendarTimeInternal();
- calendarAdditionalInfo = new CalendarAdditionalInfo();
- ResultCode result;
- if ((rules.GoAhead && time < rules.Ats[0]) || (rules.GoBack && time > rules.Ats[rules.TimeCount - 1]))
- {
- long newTime = time;
- long seconds;
- long years;
- if (time < rules.Ats[0])
- {
- seconds = rules.Ats[0] - time;
- }
- else
- {
- seconds = time - rules.Ats[rules.TimeCount - 1];
- }
- seconds -= 1;
- years = (seconds / SecondsPerRepeat + 1) * YearsPerRepeat;
- seconds = years * AverageSecondsPerYear;
- if (time < rules.Ats[0])
- {
- newTime += seconds;
- }
- else
- {
- newTime -= seconds;
- }
- if (newTime < rules.Ats[0] && newTime > rules.Ats[rules.TimeCount - 1])
- {
- return ResultCode.TimeNotFound;
- }
- result = ToCalendarTimeInternal(in rules, newTime, out calendarTime, out calendarAdditionalInfo);
- if (result != 0)
- {
- return result;
- }
- if (time < rules.Ats[0])
- {
- calendarTime.Year -= years;
- }
- else
- {
- calendarTime.Year += years;
- }
- return ResultCode.Success;
- }
- int ttiIndex;
- if (rules.TimeCount == 0 || time < rules.Ats[0])
- {
- ttiIndex = rules.DefaultType;
- }
- else
- {
- int low = 1;
- int high = rules.TimeCount;
- while (low < high)
- {
- int mid = (low + high) >> 1;
- if (time < rules.Ats[mid])
- {
- high = mid;
- }
- else
- {
- low = mid + 1;
- }
- }
- ttiIndex = rules.Types[low - 1];
- }
- result = CreateCalendarTime(time, rules.Ttis[ttiIndex].GmtOffset, out calendarTime, out calendarAdditionalInfo);
- if (result == 0)
- {
- calendarAdditionalInfo.IsDaySavingTime = rules.Ttis[ttiIndex].IsDaySavingTime;
- ReadOnlySpan<byte> timeZoneAbbreviation = rules.Chars[rules.Ttis[ttiIndex].AbbreviationListIndex..];
- int timeZoneSize = Math.Min(StringUtils.LengthCstr(timeZoneAbbreviation), 8);
- timeZoneAbbreviation[..timeZoneSize].CopyTo(calendarAdditionalInfo.TimezoneName.AsSpan());
- }
- return result;
- }
- private static ResultCode ToPosixTimeInternal(in TimeZoneRule rules, CalendarTimeInternal calendarTime, out long posixTime)
- {
- posixTime = 0;
- int hour = calendarTime.Hour;
- int minute = calendarTime.Minute;
- if (NormalizeOverflow32(ref hour, ref minute, MinutesPerHour))
- {
- return ResultCode.Overflow;
- }
- calendarTime.Minute = (sbyte)minute;
- int day = calendarTime.Day;
- if (NormalizeOverflow32(ref day, ref hour, HoursPerDays))
- {
- return ResultCode.Overflow;
- }
- calendarTime.Day = (sbyte)day;
- calendarTime.Hour = (sbyte)hour;
- long year = calendarTime.Year;
- long month = calendarTime.Month;
- if (NormalizeOverflow64(ref year, ref month, MonthsPerYear))
- {
- return ResultCode.Overflow;
- }
- calendarTime.Month = (sbyte)month;
- if (IncrementOverflow64(ref year, YearBase))
- {
- return ResultCode.Overflow;
- }
- while (day <= 0)
- {
- if (IncrementOverflow64(ref year, -1))
- {
- return ResultCode.Overflow;
- }
- long li = year;
- if (1 < calendarTime.Month)
- {
- li++;
- }
- day += YearLengths[IsLeap((int)li)];
- }
- while (day > DaysPerLYear)
- {
- long li = year;
- if (1 < calendarTime.Month)
- {
- li++;
- }
- day -= YearLengths[IsLeap((int)li)];
- if (IncrementOverflow64(ref year, 1))
- {
- return ResultCode.Overflow;
- }
- }
- while (true)
- {
- int i = MonthsLengths[IsLeap((int)year)][calendarTime.Month];
- if (day <= i)
- {
- break;
- }
- day -= i;
- calendarTime.Month += 1;
- if (calendarTime.Month >= MonthsPerYear)
- {
- calendarTime.Month = 0;
- if (IncrementOverflow64(ref year, 1))
- {
- return ResultCode.Overflow;
- }
- }
- }
- calendarTime.Day = (sbyte)day;
- if (IncrementOverflow64(ref year, -YearBase))
- {
- return ResultCode.Overflow;
- }
- calendarTime.Year = year;
- int savedSeconds;
- if (calendarTime.Second >= 0 && calendarTime.Second < SecondsPerMinute)
- {
- savedSeconds = 0;
- }
- else if (year + YearBase < EpochYear)
- {
- int second = calendarTime.Second;
- if (IncrementOverflow32(ref second, 1 - SecondsPerMinute))
- {
- return ResultCode.Overflow;
- }
- savedSeconds = second;
- calendarTime.Second = 1 - SecondsPerMinute;
- }
- else
- {
- savedSeconds = calendarTime.Second;
- calendarTime.Second = 0;
- }
- long low = long.MinValue;
- long high = long.MaxValue;
- while (true)
- {
- long pivot = low / 2 + high / 2;
- if (pivot < low)
- {
- pivot = low;
- }
- else if (pivot > high)
- {
- pivot = high;
- }
- int direction;
- ResultCode result = ToCalendarTimeInternal(in rules, pivot, out CalendarTimeInternal candidateCalendarTime, out _);
- if (result != 0)
- {
- if (pivot > 0)
- {
- direction = 1;
- }
- else
- {
- direction = -1;
- }
- }
- else
- {
- direction = candidateCalendarTime.CompareTo(calendarTime);
- }
- if (direction == 0)
- {
- long timeResult = pivot + savedSeconds;
- if ((timeResult < pivot) != (savedSeconds < 0))
- {
- return ResultCode.Overflow;
- }
- posixTime = timeResult;
- break;
- }
- else
- {
- if (pivot == low)
- {
- if (pivot == long.MaxValue)
- {
- return ResultCode.TimeNotFound;
- }
- pivot += 1;
- low += 1;
- }
- else if (pivot == high)
- {
- if (pivot == long.MinValue)
- {
- return ResultCode.TimeNotFound;
- }
- pivot -= 1;
- high -= 1;
- }
- if (low > high)
- {
- return ResultCode.TimeNotFound;
- }
- if (direction > 0)
- {
- high = pivot;
- }
- else
- {
- low = pivot;
- }
- }
- }
- return ResultCode.Success;
- }
- internal static ResultCode ToCalendarTime(in TimeZoneRule rules, long time, out CalendarInfo calendar)
- {
- ResultCode result = ToCalendarTimeInternal(in rules, time, out CalendarTimeInternal calendarTime, out CalendarAdditionalInfo calendarAdditionalInfo);
- calendar = new CalendarInfo()
- {
- Time = new CalendarTime()
- {
- Year = (short)calendarTime.Year,
- // NOTE: Nintendo's month range is 1-12, internal range is 0-11.
- Month = (sbyte)(calendarTime.Month + 1),
- Day = calendarTime.Day,
- Hour = calendarTime.Hour,
- Minute = calendarTime.Minute,
- Second = calendarTime.Second
- },
- AdditionalInfo = calendarAdditionalInfo
- };
- return result;
- }
- internal static ResultCode ToPosixTime(in TimeZoneRule rules, CalendarTime calendarTime, out long posixTime)
- {
- CalendarTimeInternal calendarTimeInternal = new CalendarTimeInternal()
- {
- Year = calendarTime.Year,
- // NOTE: Nintendo's month range is 1-12, internal range is 0-11.
- Month = (sbyte)(calendarTime.Month - 1),
- Day = calendarTime.Day,
- Hour = calendarTime.Hour,
- Minute = calendarTime.Minute,
- Second = calendarTime.Second
- };
- return ToPosixTimeInternal(in rules, calendarTimeInternal, out posixTime);
- }
- }
- }
|