| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT license.
- // See the LICENSE file in the project root for more information.
- using System.Buffers;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Globalization;
- using System.IO;
- using System.Text;
- using System.Threading;
- using System.Security;
- using System.Runtime.InteropServices;
- using Internal.IO;
- namespace System
- {
- public sealed partial class TimeZoneInfo
- {
- private const string DefaultTimeZoneDirectory = "/usr/share/zoneinfo/";
- private const string ZoneTabFileName = "zone.tab";
- private const string TimeZoneEnvironmentVariable = "TZ";
- private const string TimeZoneDirectoryEnvironmentVariable = "TZDIR";
- private TimeZoneInfo(byte[] data, string id, bool dstDisabled)
- {
- TZifHead t;
- DateTime[] dts;
- byte[] typeOfLocalTime;
- TZifType[] transitionType;
- string zoneAbbreviations;
- bool[] StandardTime;
- bool[] GmtTime;
- string futureTransitionsPosixFormat;
- // parse the raw TZif bytes; this method can throw ArgumentException when the data is malformed.
- TZif_ParseRaw(data, out t, out dts, out typeOfLocalTime, out transitionType, out zoneAbbreviations, out StandardTime, out GmtTime, out futureTransitionsPosixFormat);
- _id = id;
- _displayName = LocalId;
- _baseUtcOffset = TimeSpan.Zero;
- // find the best matching baseUtcOffset and display strings based on the current utcNow value.
- // NOTE: read the display strings from the tzfile now in case they can't be loaded later
- // from the globalization data.
- DateTime utcNow = DateTime.UtcNow;
- for (int i = 0; i < dts.Length && dts[i] <= utcNow; i++)
- {
- int type = typeOfLocalTime[i];
- if (!transitionType[type].IsDst)
- {
- _baseUtcOffset = transitionType[type].UtcOffset;
- _standardDisplayName = TZif_GetZoneAbbreviation(zoneAbbreviations, transitionType[type].AbbreviationIndex);
- }
- else
- {
- _daylightDisplayName = TZif_GetZoneAbbreviation(zoneAbbreviations, transitionType[type].AbbreviationIndex);
- }
- }
- if (dts.Length == 0)
- {
- // time zones like Africa/Bujumbura and Etc/GMT* have no transition times but still contain
- // TZifType entries that may contain a baseUtcOffset and display strings
- for (int i = 0; i < transitionType.Length; i++)
- {
- if (!transitionType[i].IsDst)
- {
- _baseUtcOffset = transitionType[i].UtcOffset;
- _standardDisplayName = TZif_GetZoneAbbreviation(zoneAbbreviations, transitionType[i].AbbreviationIndex);
- }
- else
- {
- _daylightDisplayName = TZif_GetZoneAbbreviation(zoneAbbreviations, transitionType[i].AbbreviationIndex);
- }
- }
- }
- _displayName = _standardDisplayName;
- GetDisplayName(Interop.Globalization.TimeZoneDisplayNameType.Generic, ref _displayName);
- GetDisplayName(Interop.Globalization.TimeZoneDisplayNameType.Standard, ref _standardDisplayName);
- GetDisplayName(Interop.Globalization.TimeZoneDisplayNameType.DaylightSavings, ref _daylightDisplayName);
- if (_standardDisplayName == _displayName)
- {
- if (_baseUtcOffset >= TimeSpan.Zero)
- _displayName = $"(UTC+{_baseUtcOffset:hh\\:mm}) {_standardDisplayName}";
- else
- _displayName = $"(UTC-{_baseUtcOffset:hh\\:mm}) {_standardDisplayName}";
- }
- // TZif supports seconds-level granularity with offsets but TimeZoneInfo only supports minutes since it aligns
- // with DateTimeOffset, SQL Server, and the W3C XML Specification
- if (_baseUtcOffset.Ticks % TimeSpan.TicksPerMinute != 0)
- {
- _baseUtcOffset = new TimeSpan(_baseUtcOffset.Hours, _baseUtcOffset.Minutes, 0);
- }
- if (!dstDisabled)
- {
- // only create the adjustment rule if DST is enabled
- TZif_GenerateAdjustmentRules(out _adjustmentRules, _baseUtcOffset, dts, typeOfLocalTime, transitionType, StandardTime, GmtTime, futureTransitionsPosixFormat);
- }
- ValidateTimeZoneInfo(_id, _baseUtcOffset, _adjustmentRules, out _supportsDaylightSavingTime);
- }
- private unsafe void GetDisplayName(Interop.Globalization.TimeZoneDisplayNameType nameType, ref string displayName)
- {
- if (GlobalizationMode.Invariant)
- {
- displayName = _standardDisplayName;
- return;
- }
- string timeZoneDisplayName;
- bool result = Interop.CallStringMethod(
- (buffer, locale, id, type) =>
- {
- fixed (char* bufferPtr = buffer)
- {
- return Interop.Globalization.GetTimeZoneDisplayName(locale, id, type, bufferPtr, buffer.Length);
- }
- },
- CultureInfo.CurrentUICulture.Name,
- _id,
- nameType,
- out timeZoneDisplayName);
- // If there is an unknown error, don't set the displayName field.
- // It will be set to the abbreviation that was read out of the tzfile.
- if (result)
- {
- displayName = timeZoneDisplayName;
- }
- }
- /// <summary>
- /// Returns a cloned array of AdjustmentRule objects
- /// </summary>
- public AdjustmentRule[] GetAdjustmentRules()
- {
- if (_adjustmentRules == null)
- {
- return Array.Empty<AdjustmentRule>();
- }
- // The rules we use in Unix care mostly about the start and end dates but don't fill the transition start and end info.
- // as the rules now is public, we should fill it properly so the caller doesn't have to know how we use it internally
- // and can use it as it is used in Windows
- AdjustmentRule[] rules = new AdjustmentRule[_adjustmentRules.Length];
- for (int i = 0; i < _adjustmentRules.Length; i++)
- {
- var rule = _adjustmentRules[i];
- var start = rule.DateStart.Kind == DateTimeKind.Utc ?
- // At the daylight start we didn't start the daylight saving yet then we convert to Local time
- // by adding the _baseUtcOffset to the UTC time
- new DateTime(rule.DateStart.Ticks + _baseUtcOffset.Ticks, DateTimeKind.Unspecified) :
- rule.DateStart;
- var end = rule.DateEnd.Kind == DateTimeKind.Utc ?
- // At the daylight saving end, the UTC time is mapped to local time which is already shifted by the daylight delta
- // we calculate the local time by adding _baseUtcOffset + DaylightDelta to the UTC time
- new DateTime(rule.DateEnd.Ticks + _baseUtcOffset.Ticks + rule.DaylightDelta.Ticks, DateTimeKind.Unspecified) :
- rule.DateEnd;
- var startTransition = TimeZoneInfo.TransitionTime.CreateFixedDateRule(new DateTime(1, 1, 1, start.Hour, start.Minute, start.Second), start.Month, start.Day);
- var endTransition = TimeZoneInfo.TransitionTime.CreateFixedDateRule(new DateTime(1, 1, 1, end.Hour, end.Minute, end.Second), end.Month, end.Day);
- rules[i] = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(start.Date, end.Date, rule.DaylightDelta, startTransition, endTransition);
- }
- return rules;
- }
- private static void PopulateAllSystemTimeZones(CachedData cachedData)
- {
- Debug.Assert(Monitor.IsEntered(cachedData));
- string timeZoneDirectory = GetTimeZoneDirectory();
- foreach (string timeZoneId in GetTimeZoneIds(timeZoneDirectory))
- {
- TimeZoneInfo value;
- Exception ex;
- TryGetTimeZone(timeZoneId, false, out value, out ex, cachedData, alwaysFallbackToLocalMachine: true); // populate the cache
- }
- }
- /// <summary>
- /// Helper function for retrieving the local system time zone.
- /// May throw COMException, TimeZoneNotFoundException, InvalidTimeZoneException.
- /// Assumes cachedData lock is taken.
- /// </summary>
- /// <returns>A new TimeZoneInfo instance.</returns>
- private static TimeZoneInfo GetLocalTimeZone(CachedData cachedData)
- {
- Debug.Assert(Monitor.IsEntered(cachedData));
- // Without Registry support, create the TimeZoneInfo from a TZ file
- return GetLocalTimeZoneFromTzFile();
- }
- private static TimeZoneInfoResult TryGetTimeZoneFromLocalMachine(string id, out TimeZoneInfo value, out Exception e)
- {
- value = null;
- e = null;
- string timeZoneDirectory = GetTimeZoneDirectory();
- string timeZoneFilePath = Path.Combine(timeZoneDirectory, id);
- byte[] rawData;
- try
- {
- rawData = File.ReadAllBytes(timeZoneFilePath);
- }
- catch (UnauthorizedAccessException ex)
- {
- e = ex;
- return TimeZoneInfoResult.SecurityException;
- }
- catch (FileNotFoundException ex)
- {
- e = ex;
- return TimeZoneInfoResult.TimeZoneNotFoundException;
- }
- catch (DirectoryNotFoundException ex)
- {
- e = ex;
- return TimeZoneInfoResult.TimeZoneNotFoundException;
- }
- catch (IOException ex)
- {
- e = new InvalidTimeZoneException(SR.Format(SR.InvalidTimeZone_InvalidFileData, id, timeZoneFilePath), ex);
- return TimeZoneInfoResult.InvalidTimeZoneException;
- }
- value = GetTimeZoneFromTzData(rawData, id);
- if (value == null)
- {
- e = new InvalidTimeZoneException(SR.Format(SR.InvalidTimeZone_InvalidFileData, id, timeZoneFilePath));
- return TimeZoneInfoResult.InvalidTimeZoneException;
- }
- return TimeZoneInfoResult.Success;
- }
- /// <summary>
- /// Returns a collection of TimeZone Id values from the zone.tab file in the timeZoneDirectory.
- /// </summary>
- /// <remarks>
- /// Lines that start with # are comments and are skipped.
- /// </remarks>
- private static List<string> GetTimeZoneIds(string timeZoneDirectory)
- {
- List<string> timeZoneIds = new List<string>();
- try
- {
- using (StreamReader sr = new StreamReader(Path.Combine(timeZoneDirectory, ZoneTabFileName), Encoding.UTF8))
- {
- string zoneTabFileLine;
- while ((zoneTabFileLine = sr.ReadLine()) != null)
- {
- if (!string.IsNullOrEmpty(zoneTabFileLine) && zoneTabFileLine[0] != '#')
- {
- // the format of the line is "country-code \t coordinates \t TimeZone Id \t comments"
- int firstTabIndex = zoneTabFileLine.IndexOf('\t');
- if (firstTabIndex != -1)
- {
- int secondTabIndex = zoneTabFileLine.IndexOf('\t', firstTabIndex + 1);
- if (secondTabIndex != -1)
- {
- string timeZoneId;
- int startIndex = secondTabIndex + 1;
- int thirdTabIndex = zoneTabFileLine.IndexOf('\t', startIndex);
- if (thirdTabIndex != -1)
- {
- int length = thirdTabIndex - startIndex;
- timeZoneId = zoneTabFileLine.Substring(startIndex, length);
- }
- else
- {
- timeZoneId = zoneTabFileLine.Substring(startIndex);
- }
- if (!string.IsNullOrEmpty(timeZoneId))
- {
- timeZoneIds.Add(timeZoneId);
- }
- }
- }
- }
- }
- }
- }
- catch (IOException) { }
- catch (UnauthorizedAccessException) { }
- return timeZoneIds;
- }
- /// <summary>
- /// Gets the tzfile raw data for the current 'local' time zone using the following rules.
- /// 1. Read the TZ environment variable. If it is set, use it.
- /// 2. Look for the data in /etc/localtime.
- /// 3. Look for the data in GetTimeZoneDirectory()/localtime.
- /// 4. Use UTC if all else fails.
- /// </summary>
- private static bool TryGetLocalTzFile(out byte[] rawData, out string id)
- {
- rawData = null;
- id = null;
- string tzVariable = GetTzEnvironmentVariable();
- // If the env var is null, use the localtime file
- if (tzVariable == null)
- {
- return
- TryLoadTzFile("/etc/localtime", ref rawData, ref id) ||
- TryLoadTzFile(Path.Combine(GetTimeZoneDirectory(), "localtime"), ref rawData, ref id);
- }
- // If it's empty, use UTC (TryGetLocalTzFile() should return false).
- if (tzVariable.Length == 0)
- {
- return false;
- }
- // Otherwise, use the path from the env var. If it's not absolute, make it relative
- // to the system timezone directory
- string tzFilePath;
- if (tzVariable[0] != '/')
- {
- id = tzVariable;
- tzFilePath = Path.Combine(GetTimeZoneDirectory(), tzVariable);
- }
- else
- {
- tzFilePath = tzVariable;
- }
- return TryLoadTzFile(tzFilePath, ref rawData, ref id);
- }
- private static string GetTzEnvironmentVariable()
- {
- string result = Environment.GetEnvironmentVariable(TimeZoneEnvironmentVariable);
- if (!string.IsNullOrEmpty(result))
- {
- if (result[0] == ':')
- {
- // strip off the ':' prefix
- result = result.Substring(1);
- }
- }
- return result;
- }
- private static bool TryLoadTzFile(string tzFilePath, ref byte[] rawData, ref string id)
- {
- if (File.Exists(tzFilePath))
- {
- try
- {
- rawData = File.ReadAllBytes(tzFilePath);
- if (string.IsNullOrEmpty(id))
- {
- id = FindTimeZoneIdUsingReadLink(tzFilePath);
- if (string.IsNullOrEmpty(id))
- {
- id = FindTimeZoneId(rawData);
- }
- }
- return true;
- }
- catch (IOException) { }
- catch (SecurityException) { }
- catch (UnauthorizedAccessException) { }
- }
- return false;
- }
- /// <summary>
- /// Finds the time zone id by using 'readlink' on the path to see if tzFilePath is
- /// a symlink to a file.
- /// </summary>
- private static string FindTimeZoneIdUsingReadLink(string tzFilePath)
- {
- string id = null;
- string symlinkPath = Interop.Sys.ReadLink(tzFilePath);
- if (symlinkPath != null)
- {
- // symlinkPath can be relative path, use Path to get the full absolute path.
- symlinkPath = Path.GetFullPath(symlinkPath, Path.GetDirectoryName(tzFilePath));
- string timeZoneDirectory = GetTimeZoneDirectory();
- if (symlinkPath.StartsWith(timeZoneDirectory, StringComparison.Ordinal))
- {
- id = symlinkPath.Substring(timeZoneDirectory.Length);
- }
- }
- return id;
- }
- private static string GetDirectoryEntryFullPath(ref Interop.Sys.DirectoryEntry dirent, string currentPath)
- {
- Span<char> nameBuffer = stackalloc char[Interop.Sys.DirectoryEntry.NameBufferSize];
- ReadOnlySpan<char> direntName = dirent.GetName(nameBuffer);
- if ((direntName.Length == 1 && direntName[0] == '.') ||
- (direntName.Length == 2 && direntName[0] == '.' && direntName[1] == '.'))
- return null;
- return Path.Join(currentPath.AsSpan(), direntName);
- }
- /// <summary>
- /// Enumerate files
- /// </summary>
- private static unsafe void EnumerateFilesRecursively(string path, Predicate<string> condition)
- {
- List<string> toExplore = null; // List used as a stack
- int bufferSize = Interop.Sys.GetReadDirRBufferSize();
- byte[] dirBuffer = null;
- try
- {
- dirBuffer = ArrayPool<byte>.Shared.Rent(bufferSize);
- string currentPath = path;
- fixed (byte* dirBufferPtr = dirBuffer)
- {
- for(;;)
- {
- IntPtr dirHandle = Interop.Sys.OpenDir(currentPath);
- if (dirHandle == IntPtr.Zero)
- {
- throw Interop.GetExceptionForIoErrno(Interop.Sys.GetLastErrorInfo(), currentPath, isDirectory: true);
- }
- try
- {
- // Read each entry from the enumerator
- Interop.Sys.DirectoryEntry dirent;
- while (Interop.Sys.ReadDirR(dirHandle, dirBufferPtr, bufferSize, out dirent) == 0)
- {
- string fullPath = GetDirectoryEntryFullPath(ref dirent, currentPath);
- if (fullPath == null)
- continue;
- // Get from the dir entry whether the entry is a file or directory.
- // We classify everything as a file unless we know it to be a directory.
- bool isDir;
- if (dirent.InodeType == Interop.Sys.NodeType.DT_DIR)
- {
- // We know it's a directory.
- isDir = true;
- }
- else if (dirent.InodeType == Interop.Sys.NodeType.DT_LNK || dirent.InodeType == Interop.Sys.NodeType.DT_UNKNOWN)
- {
- // It's a symlink or unknown: stat to it to see if we can resolve it to a directory.
- // If we can't (e.g. symlink to a file, broken symlink, etc.), we'll just treat it as a file.
- Interop.Sys.FileStatus fileinfo;
- if (Interop.Sys.Stat(fullPath, out fileinfo) >= 0)
- {
- isDir = (fileinfo.Mode & Interop.Sys.FileTypes.S_IFMT) == Interop.Sys.FileTypes.S_IFDIR;
- }
- else
- {
- isDir = false;
- }
- }
- else
- {
- // Otherwise, treat it as a file. This includes regular files, FIFOs, etc.
- isDir = false;
- }
- // Yield the result if the user has asked for it. In the case of directories,
- // always explore it by pushing it onto the stack, regardless of whether
- // we're returning directories.
- if (isDir)
- {
- if (toExplore == null)
- {
- toExplore = new List<string>();
- }
- toExplore.Add(fullPath);
- }
- else if (condition(fullPath))
- {
- return;
- }
- }
- }
- finally
- {
- if (dirHandle != IntPtr.Zero)
- Interop.Sys.CloseDir(dirHandle);
- }
- if (toExplore == null || toExplore.Count == 0)
- break;
- currentPath = toExplore[toExplore.Count - 1];
- toExplore.RemoveAt(toExplore.Count - 1);
- }
- }
- }
- finally
- {
- if (dirBuffer != null)
- ArrayPool<byte>.Shared.Return(dirBuffer);
- }
- }
- /// <summary>
- /// Find the time zone id by searching all the tzfiles for the one that matches rawData
- /// and return its file name.
- /// </summary>
- private static string FindTimeZoneId(byte[] rawData)
- {
- // default to "Local" if we can't find the right tzfile
- string id = LocalId;
- string timeZoneDirectory = GetTimeZoneDirectory();
- string localtimeFilePath = Path.Combine(timeZoneDirectory, "localtime");
- string posixrulesFilePath = Path.Combine(timeZoneDirectory, "posixrules");
- byte[] buffer = new byte[rawData.Length];
- try
- {
- EnumerateFilesRecursively(timeZoneDirectory, (string filePath) =>
- {
- // skip the localtime and posixrules file, since they won't give us the correct id
- if (!string.Equals(filePath, localtimeFilePath, StringComparison.OrdinalIgnoreCase)
- && !string.Equals(filePath, posixrulesFilePath, StringComparison.OrdinalIgnoreCase))
- {
- if (CompareTimeZoneFile(filePath, buffer, rawData))
- {
- // if all bytes are the same, this must be the right tz file
- id = filePath;
- // strip off the root time zone directory
- if (id.StartsWith(timeZoneDirectory, StringComparison.Ordinal))
- {
- id = id.Substring(timeZoneDirectory.Length);
- }
- return true;
- }
- }
- return false;
- });
- }
- catch (IOException) { }
- catch (SecurityException) { }
- catch (UnauthorizedAccessException) { }
- return id;
- }
- private static bool CompareTimeZoneFile(string filePath, byte[] buffer, byte[] rawData)
- {
- try
- {
- // bufferSize == 1 used to avoid unnecessary buffer in FileStream
- using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 1))
- {
- if (stream.Length == rawData.Length)
- {
- int index = 0;
- int count = rawData.Length;
- while (count > 0)
- {
- int n = stream.Read(buffer, index, count);
- if (n == 0)
- throw Error.GetEndOfFile();
- int end = index + n;
- for (; index < end; index++)
- {
- if (buffer[index] != rawData[index])
- {
- return false;
- }
- }
- count -= n;
- }
- return true;
- }
- }
- }
- catch (IOException) { }
- catch (SecurityException) { }
- catch (UnauthorizedAccessException) { }
- return false;
- }
- /// <summary>
- /// Helper function used by 'GetLocalTimeZone()' - this function wraps the call
- /// for loading time zone data from computers without Registry support.
- ///
- /// The TryGetLocalTzFile() call returns a Byte[] containing the compiled tzfile.
- /// </summary>
- private static TimeZoneInfo GetLocalTimeZoneFromTzFile()
- {
- byte[] rawData;
- string id;
- if (TryGetLocalTzFile(out rawData, out id))
- {
- TimeZoneInfo result = GetTimeZoneFromTzData(rawData, id);
- if (result != null)
- {
- return result;
- }
- }
- // if we can't find a local time zone, return UTC
- return Utc;
- }
- private static TimeZoneInfo GetTimeZoneFromTzData(byte[] rawData, string id)
- {
- if (rawData != null)
- {
- try
- {
- return new TimeZoneInfo(rawData, id, dstDisabled: false); // create a TimeZoneInfo instance from the TZif data w/ DST support
- }
- catch (ArgumentException) { }
- catch (InvalidTimeZoneException) { }
- try
- {
- return new TimeZoneInfo(rawData, id, dstDisabled: true); // create a TimeZoneInfo instance from the TZif data w/o DST support
- }
- catch (ArgumentException) { }
- catch (InvalidTimeZoneException) { }
- }
- return null;
- }
- private static string GetTimeZoneDirectory()
- {
- string tzDirectory = Environment.GetEnvironmentVariable(TimeZoneDirectoryEnvironmentVariable);
- if (tzDirectory == null)
- {
- tzDirectory = DefaultTimeZoneDirectory;
- }
- else if (!tzDirectory.EndsWith(Path.DirectorySeparatorChar))
- {
- tzDirectory += Path.DirectorySeparatorChar;
- }
- return tzDirectory;
- }
- /// <summary>
- /// Helper function for retrieving a TimeZoneInfo object by time_zone_name.
- /// This function wraps the logic necessary to keep the private
- /// SystemTimeZones cache in working order
- ///
- /// This function will either return a valid TimeZoneInfo instance or
- /// it will throw 'InvalidTimeZoneException' / 'TimeZoneNotFoundException'.
- /// </summary>
- public static TimeZoneInfo FindSystemTimeZoneById(string id)
- {
- // Special case for Utc as it will not exist in the dictionary with the rest
- // of the system time zones. There is no need to do this check for Local.Id
- // since Local is a real time zone that exists in the dictionary cache
- if (string.Equals(id, UtcId, StringComparison.OrdinalIgnoreCase))
- {
- return Utc;
- }
- if (id == null)
- {
- throw new ArgumentNullException(nameof(id));
- }
- else if (id.Length == 0 || id.Contains('\0'))
- {
- throw new TimeZoneNotFoundException(SR.Format(SR.TimeZoneNotFound_MissingData, id));
- }
- TimeZoneInfo value;
- Exception e;
- TimeZoneInfoResult result;
- CachedData cachedData = s_cachedData;
- lock (cachedData)
- {
- result = TryGetTimeZone(id, false, out value, out e, cachedData, alwaysFallbackToLocalMachine: true);
- }
- if (result == TimeZoneInfoResult.Success)
- {
- return value;
- }
- else if (result == TimeZoneInfoResult.InvalidTimeZoneException)
- {
- Debug.Assert(e is InvalidTimeZoneException,
- "TryGetTimeZone must create an InvalidTimeZoneException when it returns TimeZoneInfoResult.InvalidTimeZoneException");
- throw e;
- }
- else if (result == TimeZoneInfoResult.SecurityException)
- {
- throw new SecurityException(SR.Format(SR.Security_CannotReadFileData, id), e);
- }
- else
- {
- throw new TimeZoneNotFoundException(SR.Format(SR.TimeZoneNotFound_MissingData, id), e);
- }
- }
- // DateTime.Now fast path that avoids allocating an historically accurate TimeZoneInfo.Local and just creates a 1-year (current year) accurate time zone
- internal static TimeSpan GetDateTimeNowUtcOffsetFromUtc(DateTime time, out bool isAmbiguousLocalDst)
- {
- bool isDaylightSavings;
- // Use the standard code path for Unix since there isn't a faster way of handling current-year-only time zones
- return GetUtcOffsetFromUtc(time, Local, out isDaylightSavings, out isAmbiguousLocalDst);
- }
- // TZFILE(5) BSD File Formats Manual TZFILE(5)
- //
- // NAME
- // tzfile -- timezone information
- //
- // SYNOPSIS
- // #include "/usr/src/lib/libc/stdtime/tzfile.h"
- //
- // DESCRIPTION
- // The time zone information files used by tzset(3) begin with the magic
- // characters ``TZif'' to identify them as time zone information files, fol-
- // lowed by sixteen bytes reserved for future use, followed by four four-
- // byte values written in a ``standard'' byte order (the high-order byte of
- // the value is written first). These values are, in order:
- //
- // tzh_ttisgmtcnt The number of UTC/local indicators stored in the file.
- // tzh_ttisstdcnt The number of standard/wall indicators stored in the
- // file.
- // tzh_leapcnt The number of leap seconds for which data is stored in
- // the file.
- // tzh_timecnt The number of ``transition times'' for which data is
- // stored in the file.
- // tzh_typecnt The number of ``local time types'' for which data is
- // stored in the file (must not be zero).
- // tzh_charcnt The number of characters of ``time zone abbreviation
- // strings'' stored in the file.
- //
- // The above header is followed by tzh_timecnt four-byte values of type
- // long, sorted in ascending order. These values are written in ``stan-
- // dard'' byte order. Each is used as a transition time (as returned by
- // time(3)) at which the rules for computing local time change. Next come
- // tzh_timecnt one-byte values of type unsigned char; each one tells which
- // of the different types of ``local time'' types described in the file is
- // associated with the same-indexed transition time. These values serve as
- // indices into an array of ttinfo structures that appears next in the file;
- // these structures are defined as follows:
- //
- // struct ttinfo {
- // long tt_gmtoff;
- // int tt_isdst;
- // unsigned int tt_abbrind;
- // };
- //
- // Each structure is written as a four-byte value for tt_gmtoff of type
- // long, in a standard byte order, followed by a one-byte value for tt_isdst
- // and a one-byte value for tt_abbrind. In each structure, tt_gmtoff gives
- // the number of seconds to be added to UTC, tt_isdst tells whether tm_isdst
- // should be set by localtime(3) and tt_abbrind serves as an index into the
- // array of time zone abbreviation characters that follow the ttinfo struc-
- // ture(s) in the file.
- //
- // Then there are tzh_leapcnt pairs of four-byte values, written in standard
- // byte order; the first value of each pair gives the time (as returned by
- // time(3)) at which a leap second occurs; the second gives the total number
- // of leap seconds to be applied after the given time. The pairs of values
- // are sorted in ascending order by time.b
- //
- // Then there are tzh_ttisstdcnt standard/wall indicators, each stored as a
- // one-byte value; they tell whether the transition times associated with
- // local time types were specified as standard time or wall clock time, and
- // are used when a time zone file is used in handling POSIX-style time zone
- // environment variables.
- //
- // Finally there are tzh_ttisgmtcnt UTC/local indicators, each stored as a
- // one-byte value; they tell whether the transition times associated with
- // local time types were specified as UTC or local time, and are used when a
- // time zone file is used in handling POSIX-style time zone environment
- // variables.
- //
- // localtime uses the first standard-time ttinfo structure in the file (or
- // simply the first ttinfo structure in the absence of a standard-time
- // structure) if either tzh_timecnt is zero or the time argument is less
- // than the first transition time recorded in the file.
- //
- // SEE ALSO
- // ctime(3), time2posix(3), zic(8)
- //
- // BSD September 13, 1994 BSD
- //
- //
- //
- // TIME(3) BSD Library Functions Manual TIME(3)
- //
- // NAME
- // time -- get time of day
- //
- // LIBRARY
- // Standard C Library (libc, -lc)
- //
- // SYNOPSIS
- // #include <time.h>
- //
- // time_t
- // time(time_t *tloc);
- //
- // DESCRIPTION
- // The time() function returns the value of time in seconds since 0 hours, 0
- // minutes, 0 seconds, January 1, 1970, Coordinated Universal Time, without
- // including leap seconds. If an error occurs, time() returns the value
- // (time_t)-1.
- //
- // The return value is also stored in *tloc, provided that tloc is non-null.
- //
- // ERRORS
- // The time() function may fail for any of the reasons described in
- // gettimeofday(2).
- //
- // SEE ALSO
- // gettimeofday(2), ctime(3)
- //
- // STANDARDS
- // The time function conforms to IEEE Std 1003.1-2001 (``POSIX.1'').
- //
- // BUGS
- // Neither ISO/IEC 9899:1999 (``ISO C99'') nor IEEE Std 1003.1-2001
- // (``POSIX.1'') requires time() to set errno on failure; thus, it is impos-
- // sible for an application to distinguish the valid time value -1 (repre-
- // senting the last UTC second of 1969) from the error return value.
- //
- // Systems conforming to earlier versions of the C and POSIX standards
- // (including older versions of FreeBSD) did not set *tloc in the error
- // case.
- //
- // HISTORY
- // A time() function appeared in Version 6 AT&T UNIX.
- //
- // BSD July 18, 2003 BSD
- //
- //
- private static void TZif_GenerateAdjustmentRules(out AdjustmentRule[] rules, TimeSpan baseUtcOffset, DateTime[] dts, byte[] typeOfLocalTime,
- TZifType[] transitionType, bool[] StandardTime, bool[] GmtTime, string futureTransitionsPosixFormat)
- {
- rules = null;
- if (dts.Length > 0)
- {
- int index = 0;
- List<AdjustmentRule> rulesList = new List<AdjustmentRule>();
- while (index <= dts.Length)
- {
- TZif_GenerateAdjustmentRule(ref index, baseUtcOffset, rulesList, dts, typeOfLocalTime, transitionType, StandardTime, GmtTime, futureTransitionsPosixFormat);
- }
- rules = rulesList.ToArray();
- if (rules != null && rules.Length == 0)
- {
- rules = null;
- }
- }
- }
- private static void TZif_GenerateAdjustmentRule(ref int index, TimeSpan timeZoneBaseUtcOffset, List<AdjustmentRule> rulesList, DateTime[] dts,
- byte[] typeOfLocalTime, TZifType[] transitionTypes, bool[] StandardTime, bool[] GmtTime, string futureTransitionsPosixFormat)
- {
- // To generate AdjustmentRules, use the following approach:
- // The first AdjustmentRule will go from DateTime.MinValue to the first transition time greater than DateTime.MinValue.
- // Each middle AdjustmentRule wil go from dts[index-1] to dts[index].
- // The last AdjustmentRule will go from dts[dts.Length-1] to Datetime.MaxValue.
- // 0. Skip any DateTime.MinValue transition times. In newer versions of the tzfile, there
- // is a "big bang" transition time, which is before the year 0001. Since any times before year 0001
- // cannot be represented by DateTime, there is no reason to make AdjustmentRules for these unrepresentable time periods.
- // 1. If there are no DateTime.MinValue times, the first AdjustmentRule goes from DateTime.MinValue
- // to the first transition and uses the first standard transitionType (or the first transitionType if none of them are standard)
- // 2. Create an AdjustmentRule for each transition, i.e. from dts[index - 1] to dts[index].
- // This rule uses the transitionType[index - 1] and the whole AdjustmentRule only describes a single offset - either
- // all daylight savings, or all stanard time.
- // 3. After all the transitions are filled out, the last AdjustmentRule is created from either:
- // a. a POSIX-style timezone description ("futureTransitionsPosixFormat"), if there is one or
- // b. continue the last transition offset until DateTime.Max
- while (index < dts.Length && dts[index] == DateTime.MinValue)
- {
- index++;
- }
- if (rulesList.Count == 0 && index < dts.Length)
- {
- TZifType transitionType = TZif_GetEarlyDateTransitionType(transitionTypes);
- DateTime endTransitionDate = dts[index];
- TimeSpan transitionOffset = TZif_CalculateTransitionOffsetFromBase(transitionType.UtcOffset, timeZoneBaseUtcOffset);
- TimeSpan daylightDelta = transitionType.IsDst ? transitionOffset : TimeSpan.Zero;
- TimeSpan baseUtcDelta = transitionType.IsDst ? TimeSpan.Zero : transitionOffset;
- AdjustmentRule r = AdjustmentRule.CreateAdjustmentRule(
- DateTime.MinValue,
- endTransitionDate.AddTicks(-1),
- daylightDelta,
- default(TransitionTime),
- default(TransitionTime),
- baseUtcDelta,
- noDaylightTransitions: true);
- if (!IsValidAdjustmentRuleOffest(timeZoneBaseUtcOffset, r))
- {
- NormalizeAdjustmentRuleOffset(timeZoneBaseUtcOffset, ref r);
- }
- rulesList.Add(r);
- }
- else if (index < dts.Length)
- {
- DateTime startTransitionDate = dts[index - 1];
- TZifType startTransitionType = transitionTypes[typeOfLocalTime[index - 1]];
- DateTime endTransitionDate = dts[index];
- TimeSpan transitionOffset = TZif_CalculateTransitionOffsetFromBase(startTransitionType.UtcOffset, timeZoneBaseUtcOffset);
- TimeSpan daylightDelta = startTransitionType.IsDst ? transitionOffset : TimeSpan.Zero;
- TimeSpan baseUtcDelta = startTransitionType.IsDst ? TimeSpan.Zero : transitionOffset;
- TransitionTime dstStart;
- if (startTransitionType.IsDst)
- {
- // the TransitionTime fields are not used when AdjustmentRule.NoDaylightTransitions == true.
- // However, there are some cases in the past where DST = true, and the daylight savings offset
- // now equals what the current BaseUtcOffset is. In that case, the AdjustmentRule.DaylightOffset
- // is going to be TimeSpan.Zero. But we still need to return 'true' from AdjustmentRule.HasDaylightSaving.
- // To ensure we always return true from HasDaylightSaving, make a "special" dstStart that will make the logic
- // in HasDaylightSaving return true.
- dstStart = TransitionTime.CreateFixedDateRule(DateTime.MinValue.AddMilliseconds(2), 1, 1);
- }
- else
- {
- dstStart = default(TransitionTime);
- }
- AdjustmentRule r = AdjustmentRule.CreateAdjustmentRule(
- startTransitionDate,
- endTransitionDate.AddTicks(-1),
- daylightDelta,
- dstStart,
- default(TransitionTime),
- baseUtcDelta,
- noDaylightTransitions: true);
- if (!IsValidAdjustmentRuleOffest(timeZoneBaseUtcOffset, r))
- {
- NormalizeAdjustmentRuleOffset(timeZoneBaseUtcOffset, ref r);
- }
- rulesList.Add(r);
- }
- else
- {
- // create the AdjustmentRule that will be used for all DateTimes after the last transition
- // NOTE: index == dts.Length
- DateTime startTransitionDate = dts[index - 1];
- if (!string.IsNullOrEmpty(futureTransitionsPosixFormat))
- {
- AdjustmentRule r = TZif_CreateAdjustmentRuleForPosixFormat(futureTransitionsPosixFormat, startTransitionDate, timeZoneBaseUtcOffset);
- if (r != null)
- {
- if (!IsValidAdjustmentRuleOffest(timeZoneBaseUtcOffset, r))
- {
- NormalizeAdjustmentRuleOffset(timeZoneBaseUtcOffset, ref r);
- }
- rulesList.Add(r);
- }
- }
- else
- {
- // just use the last transition as the rule which will be used until the end of time
- TZifType transitionType = transitionTypes[typeOfLocalTime[index - 1]];
- TimeSpan transitionOffset = TZif_CalculateTransitionOffsetFromBase(transitionType.UtcOffset, timeZoneBaseUtcOffset);
- TimeSpan daylightDelta = transitionType.IsDst ? transitionOffset : TimeSpan.Zero;
- TimeSpan baseUtcDelta = transitionType.IsDst ? TimeSpan.Zero : transitionOffset;
- AdjustmentRule r = AdjustmentRule.CreateAdjustmentRule(
- startTransitionDate,
- DateTime.MaxValue,
- daylightDelta,
- default(TransitionTime),
- default(TransitionTime),
- baseUtcDelta,
- noDaylightTransitions: true);
- if (!IsValidAdjustmentRuleOffest(timeZoneBaseUtcOffset, r))
- {
- NormalizeAdjustmentRuleOffset(timeZoneBaseUtcOffset, ref r);
- }
- rulesList.Add(r);
- }
- }
- index++;
- }
- private static TimeSpan TZif_CalculateTransitionOffsetFromBase(TimeSpan transitionOffset, TimeSpan timeZoneBaseUtcOffset)
- {
- TimeSpan result = transitionOffset - timeZoneBaseUtcOffset;
- // TZif supports seconds-level granularity with offsets but TimeZoneInfo only supports minutes since it aligns
- // with DateTimeOffset, SQL Server, and the W3C XML Specification
- if (result.Ticks % TimeSpan.TicksPerMinute != 0)
- {
- result = new TimeSpan(result.Hours, result.Minutes, 0);
- }
- return result;
- }
- /// <summary>
- /// Gets the first standard-time transition type, or simply the first transition type
- /// if there are no standard transition types.
- /// </summary>>
- /// <remarks>
- /// from 'man tzfile':
- /// localtime(3) uses the first standard-time ttinfo structure in the file
- /// (or simply the first ttinfo structure in the absence of a standard-time
- /// structure) if either tzh_timecnt is zero or the time argument is less
- /// than the first transition time recorded in the file.
- /// </remarks>
- private static TZifType TZif_GetEarlyDateTransitionType(TZifType[] transitionTypes)
- {
- foreach (TZifType transitionType in transitionTypes)
- {
- if (!transitionType.IsDst)
- {
- return transitionType;
- }
- }
- if (transitionTypes.Length > 0)
- {
- return transitionTypes[0];
- }
- throw new InvalidTimeZoneException(SR.InvalidTimeZone_NoTTInfoStructures);
- }
- /// <summary>
- /// Creates an AdjustmentRule given the POSIX TZ environment variable string.
- /// </summary>
- /// <remarks>
- /// See http://man7.org/linux/man-pages/man3/tzset.3.html for the format and semantics of this POSX string.
- /// </remarks>
- private static AdjustmentRule TZif_CreateAdjustmentRuleForPosixFormat(string posixFormat, DateTime startTransitionDate, TimeSpan timeZoneBaseUtcOffset)
- {
- if (TZif_ParsePosixFormat(posixFormat,
- out ReadOnlySpan<char> standardName,
- out ReadOnlySpan<char> standardOffset,
- out ReadOnlySpan<char> daylightSavingsName,
- out ReadOnlySpan<char> daylightSavingsOffset,
- out ReadOnlySpan<char> start,
- out ReadOnlySpan<char> startTime,
- out ReadOnlySpan<char> end,
- out ReadOnlySpan<char> endTime))
- {
- // a valid posixFormat has at least standardName and standardOffset
- TimeSpan? parsedBaseOffset = TZif_ParseOffsetString(standardOffset);
- if (parsedBaseOffset.HasValue)
- {
- TimeSpan baseOffset = parsedBaseOffset.GetValueOrDefault().Negate(); // offsets are backwards in POSIX notation
- baseOffset = TZif_CalculateTransitionOffsetFromBase(baseOffset, timeZoneBaseUtcOffset);
- // having a daylightSavingsName means there is a DST rule
- if (!daylightSavingsName.IsEmpty)
- {
- TimeSpan? parsedDaylightSavings = TZif_ParseOffsetString(daylightSavingsOffset);
- TimeSpan daylightSavingsTimeSpan;
- if (!parsedDaylightSavings.HasValue)
- {
- // default DST to 1 hour if it isn't specified
- daylightSavingsTimeSpan = new TimeSpan(1, 0, 0);
- }
- else
- {
- daylightSavingsTimeSpan = parsedDaylightSavings.GetValueOrDefault().Negate(); // offsets are backwards in POSIX notation
- daylightSavingsTimeSpan = TZif_CalculateTransitionOffsetFromBase(daylightSavingsTimeSpan, timeZoneBaseUtcOffset);
- daylightSavingsTimeSpan = TZif_CalculateTransitionOffsetFromBase(daylightSavingsTimeSpan, baseOffset);
- }
- TransitionTime dstStart = TZif_CreateTransitionTimeFromPosixRule(start, startTime);
- TransitionTime dstEnd = TZif_CreateTransitionTimeFromPosixRule(end, endTime);
- return AdjustmentRule.CreateAdjustmentRule(
- startTransitionDate,
- DateTime.MaxValue,
- daylightSavingsTimeSpan,
- dstStart,
- dstEnd,
- baseOffset,
- noDaylightTransitions: false);
- }
- else
- {
- // if there is no daylightSavingsName, the whole AdjustmentRule should be with no transitions - just the baseOffset
- return AdjustmentRule.CreateAdjustmentRule(
- startTransitionDate,
- DateTime.MaxValue,
- TimeSpan.Zero,
- default(TransitionTime),
- default(TransitionTime),
- baseOffset,
- noDaylightTransitions: true);
- }
- }
- }
- return null;
- }
- private static TimeSpan? TZif_ParseOffsetString(ReadOnlySpan<char> offset)
- {
- TimeSpan? result = null;
- if (offset.Length > 0)
- {
- bool negative = offset[0] == '-';
- if (negative || offset[0] == '+')
- {
- offset = offset.Slice(1);
- }
- // Try parsing just hours first.
- // Note, TimeSpan.TryParseExact "%h" can't be used here because some time zones using values
- // like "26" or "144" and TimeSpan parsing would turn that into 26 or 144 *days* instead of hours.
- int hours;
- if (int.TryParse(offset, out hours))
- {
- result = new TimeSpan(hours, 0, 0);
- }
- else
- {
- TimeSpan parsedTimeSpan;
- if (TimeSpan.TryParseExact(offset, "g", CultureInfo.InvariantCulture, out parsedTimeSpan))
- {
- result = parsedTimeSpan;
- }
- }
- if (result.HasValue && negative)
- {
- result = result.GetValueOrDefault().Negate();
- }
- }
- return result;
- }
- private static DateTime ParseTimeOfDay(ReadOnlySpan<char> time)
- {
- DateTime timeOfDay;
- TimeSpan? timeOffset = TZif_ParseOffsetString(time);
- if (timeOffset.HasValue)
- {
- // This logic isn't correct and can't be corrected until https://github.com/dotnet/corefx/issues/2618 is fixed.
- // Some time zones use time values like, "26", "144", or "-2".
- // This allows the week to sometimes be week 4 and sometimes week 5 in the month.
- // For now, strip off any 'days' in the offset, and just get the time of day correct
- timeOffset = new TimeSpan(timeOffset.GetValueOrDefault().Hours, timeOffset.GetValueOrDefault().Minutes, timeOffset.GetValueOrDefault().Seconds);
- if (timeOffset.GetValueOrDefault() < TimeSpan.Zero)
- {
- timeOfDay = new DateTime(1, 1, 2, 0, 0, 0);
- }
- else
- {
- timeOfDay = new DateTime(1, 1, 1, 0, 0, 0);
- }
- timeOfDay += timeOffset.GetValueOrDefault();
- }
- else
- {
- // default to 2AM.
- timeOfDay = new DateTime(1, 1, 1, 2, 0, 0);
- }
- return timeOfDay;
- }
- private static TransitionTime TZif_CreateTransitionTimeFromPosixRule(ReadOnlySpan<char> date, ReadOnlySpan<char> time)
- {
- if (date.IsEmpty)
- {
- return default(TransitionTime);
- }
- if (date[0] == 'M')
- {
- // Mm.w.d
- // This specifies day d of week w of month m. The day d must be between 0(Sunday) and 6.The week w must be between 1 and 5;
- // week 1 is the first week in which day d occurs, and week 5 specifies the last d day in the month. The month m should be between 1 and 12.
- int month;
- int week;
- DayOfWeek day;
- if (!TZif_ParseMDateRule(date, out month, out week, out day))
- {
- throw new InvalidTimeZoneException(SR.Format(SR.InvalidTimeZone_UnparseablePosixMDateString, date.ToString()));
- }
- return TransitionTime.CreateFloatingDateRule(ParseTimeOfDay(time), month, week, day);
- }
- else
- {
- if (date[0] != 'J')
- {
- // should be n Julian day format which we don't support.
- //
- // This specifies the Julian day, with n between 0 and 365. February 29 is counted in leap years.
- //
- // n would be a relative number from the begining of the year. which should handle if the
- // the year is a leap year or not.
- //
- // In leap year, n would be counted as:
- //
- // 0 30 31 59 60 90 335 365
- // |-------Jan--------|-------Feb--------|-------Mar--------|....|-------Dec--------|
- //
- // while in non leap year we'll have
- //
- // 0 30 31 58 59 89 334 364
- // |-------Jan--------|-------Feb--------|-------Mar--------|....|-------Dec--------|
- //
- //
- // For example if n is specified as 60, this means in leap year the rule will start at Mar 1,
- // while in non leap year the rule will start at Mar 2.
- //
- // If we need to support n format, we'll have to have a floating adjustment rule support this case.
- throw new InvalidTimeZoneException(SR.InvalidTimeZone_NJulianDayNotSupported);
- }
- // Julian day
- TZif_ParseJulianDay(date, out int month, out int day);
- return TransitionTime.CreateFixedDateRule(ParseTimeOfDay(time), month, day);
- }
- }
- /// <summary>
- /// Parses a string like Jn or n into month and day values.
- /// </summary>
- /// <returns>
- /// true if the parsing succeeded; otherwise, false.
- /// </returns>
- private static void TZif_ParseJulianDay(ReadOnlySpan<char> date, out int month, out int day)
- {
- // Jn
- // This specifies the Julian day, with n between 1 and 365.February 29 is never counted, even in leap years.
- Debug.Assert(!date.IsEmpty);
- Debug.Assert(date[0] == 'J');
- month = day = 0;
- int index = 1;
- if (index >= date.Length || ((uint)(date[index] - '0') > '9'-'0'))
- {
- throw new InvalidTimeZoneException(SR.InvalidTimeZone_InvalidJulianDay);
- }
- int julianDay = 0;
- do
- {
- julianDay = julianDay * 10 + (int) (date[index] - '0');
- index++;
- } while (index < date.Length && ((uint)(date[index] - '0') <= '9'-'0'));
- int[] days = GregorianCalendarHelper.DaysToMonth365;
- if (julianDay == 0 || julianDay > days[days.Length - 1])
- {
- throw new InvalidTimeZoneException(SR.InvalidTimeZone_InvalidJulianDay);
- }
- int i = 1;
- while (i < days.Length && julianDay > days[i])
- {
- i++;
- }
- Debug.Assert(i > 0 && i < days.Length);
- month = i;
- day = julianDay - days[i - 1];
- }
- /// <summary>
- /// Parses a string like Mm.w.d into month, week and DayOfWeek values.
- /// </summary>
- /// <returns>
- /// true if the parsing succeeded; otherwise, false.
- /// </returns>
- private static bool TZif_ParseMDateRule(ReadOnlySpan<char> dateRule, out int month, out int week, out DayOfWeek dayOfWeek)
- {
- if (dateRule[0] == 'M')
- {
- int monthWeekDotIndex = dateRule.IndexOf('.');
- if (monthWeekDotIndex > 0)
- {
- ReadOnlySpan<char> weekDaySpan = dateRule.Slice(monthWeekDotIndex + 1);
- int weekDayDotIndex = weekDaySpan.IndexOf('.');
- if (weekDayDotIndex > 0)
- {
- if (int.TryParse(dateRule.Slice(1, monthWeekDotIndex - 1), out month) &&
- int.TryParse(weekDaySpan.Slice(0, weekDayDotIndex), out week) &&
- int.TryParse(weekDaySpan.Slice(weekDayDotIndex + 1), out int day))
- {
- dayOfWeek = (DayOfWeek)day;
- return true;
- }
- }
- }
- }
- month = 0;
- week = 0;
- dayOfWeek = default(DayOfWeek);
- return false;
- }
- private static bool TZif_ParsePosixFormat(
- ReadOnlySpan<char> posixFormat,
- out ReadOnlySpan<char> standardName,
- out ReadOnlySpan<char> standardOffset,
- out ReadOnlySpan<char> daylightSavingsName,
- out ReadOnlySpan<char> daylightSavingsOffset,
- out ReadOnlySpan<char> start,
- out ReadOnlySpan<char> startTime,
- out ReadOnlySpan<char> end,
- out ReadOnlySpan<char> endTime)
- {
- standardName = null;
- standardOffset = null;
- daylightSavingsName = null;
- daylightSavingsOffset = null;
- start = null;
- startTime = null;
- end = null;
- endTime = null;
- int index = 0;
- standardName = TZif_ParsePosixName(posixFormat, ref index);
- standardOffset = TZif_ParsePosixOffset(posixFormat, ref index);
- daylightSavingsName = TZif_ParsePosixName(posixFormat, ref index);
- if (!daylightSavingsName.IsEmpty)
- {
- daylightSavingsOffset = TZif_ParsePosixOffset(posixFormat, ref index);
- if (index < posixFormat.Length && posixFormat[index] == ',')
- {
- index++;
- TZif_ParsePosixDateTime(posixFormat, ref index, out start, out startTime);
- if (index < posixFormat.Length && posixFormat[index] == ',')
- {
- index++;
- TZif_ParsePosixDateTime(posixFormat, ref index, out end, out endTime);
- }
- }
- }
- return !standardName.IsEmpty && !standardOffset.IsEmpty;
- }
- private static ReadOnlySpan<char> TZif_ParsePosixName(ReadOnlySpan<char> posixFormat, ref int index)
- {
- bool isBracketEnclosed = index < posixFormat.Length && posixFormat[index] == '<';
- if (isBracketEnclosed)
- {
- // move past the opening bracket
- index++;
- ReadOnlySpan<char> result = TZif_ParsePosixString(posixFormat, ref index, c => c == '>');
- // move past the closing bracket
- if (index < posixFormat.Length && posixFormat[index] == '>')
- {
- index++;
- }
- return result;
- }
- else
- {
- return TZif_ParsePosixString(
- posixFormat,
- ref index,
- c => char.IsDigit(c) || c == '+' || c == '-' || c == ',');
- }
- }
- private static ReadOnlySpan<char> TZif_ParsePosixOffset(ReadOnlySpan<char> posixFormat, ref int index) =>
- TZif_ParsePosixString(posixFormat, ref index, c => !char.IsDigit(c) && c != '+' && c != '-' && c != ':');
- private static void TZif_ParsePosixDateTime(ReadOnlySpan<char> posixFormat, ref int index, out ReadOnlySpan<char> date, out ReadOnlySpan<char> time)
- {
- time = null;
- date = TZif_ParsePosixDate(posixFormat, ref index);
- if (index < posixFormat.Length && posixFormat[index] == '/')
- {
- index++;
- time = TZif_ParsePosixTime(posixFormat, ref index);
- }
- }
- private static ReadOnlySpan<char> TZif_ParsePosixDate(ReadOnlySpan<char> posixFormat, ref int index) =>
- TZif_ParsePosixString(posixFormat, ref index, c => c == '/' || c == ',');
- private static ReadOnlySpan<char> TZif_ParsePosixTime(ReadOnlySpan<char> posixFormat, ref int index) =>
- TZif_ParsePosixString(posixFormat, ref index, c => c == ',');
- private static ReadOnlySpan<char> TZif_ParsePosixString(ReadOnlySpan<char> posixFormat, ref int index, Func<char, bool> breakCondition)
- {
- int startIndex = index;
- for (; index < posixFormat.Length; index++)
- {
- char current = posixFormat[index];
- if (breakCondition(current))
- {
- break;
- }
- }
- return posixFormat.Slice(startIndex, index - startIndex);
- }
- // Returns the Substring from zoneAbbreviations starting at index and ending at '\0'
- // zoneAbbreviations is expected to be in the form: "PST\0PDT\0PWT\0\PPT"
- private static string TZif_GetZoneAbbreviation(string zoneAbbreviations, int index)
- {
- int lastIndex = zoneAbbreviations.IndexOf('\0', index);
- return lastIndex > 0 ?
- zoneAbbreviations.Substring(index, lastIndex - index) :
- zoneAbbreviations.Substring(index);
- }
- // Converts an array of bytes into an int - always using standard byte order (Big Endian)
- // per TZif file standard
- private static unsafe int TZif_ToInt32(byte[] value, int startIndex)
- {
- fixed (byte* pbyte = &value[startIndex])
- {
- return (*pbyte << 24) | (*(pbyte + 1) << 16) | (*(pbyte + 2) << 8) | (*(pbyte + 3));
- }
- }
- // Converts an array of bytes into a long - always using standard byte order (Big Endian)
- // per TZif file standard
- private static unsafe long TZif_ToInt64(byte[] value, int startIndex)
- {
- fixed (byte* pbyte = &value[startIndex])
- {
- int i1 = (*pbyte << 24) | (*(pbyte + 1) << 16) | (*(pbyte + 2) << 8) | (*(pbyte + 3));
- int i2 = (*(pbyte + 4) << 24) | (*(pbyte + 5) << 16) | (*(pbyte + 6) << 8) | (*(pbyte + 7));
- return (uint)i2 | ((long)i1 << 32);
- }
- }
- private static long TZif_ToUnixTime(byte[] value, int startIndex, TZVersion version) =>
- version != TZVersion.V1 ?
- TZif_ToInt64(value, startIndex) :
- TZif_ToInt32(value, startIndex);
- private static DateTime TZif_UnixTimeToDateTime(long unixTime) =>
- unixTime < DateTimeOffset.UnixMinSeconds ? DateTime.MinValue :
- unixTime > DateTimeOffset.UnixMaxSeconds ? DateTime.MaxValue :
- DateTimeOffset.FromUnixTimeSeconds(unixTime).UtcDateTime;
- private static void TZif_ParseRaw(byte[] data, out TZifHead t, out DateTime[] dts, out byte[] typeOfLocalTime, out TZifType[] transitionType,
- out string zoneAbbreviations, out bool[] StandardTime, out bool[] GmtTime, out string futureTransitionsPosixFormat)
- {
- // initialize the out parameters in case the TZifHead ctor throws
- dts = null;
- typeOfLocalTime = null;
- transitionType = null;
- zoneAbbreviations = string.Empty;
- StandardTime = null;
- GmtTime = null;
- futureTransitionsPosixFormat = null;
- // read in the 44-byte TZ header containing the count/length fields
- //
- int index = 0;
- t = new TZifHead(data, index);
- index += TZifHead.Length;
- int timeValuesLength = 4; // the first version uses 4-bytes to specify times
- if (t.Version != TZVersion.V1)
- {
- // move index past the V1 information to read the V2 information
- index += (int)((timeValuesLength * t.TimeCount) + t.TimeCount + (6 * t.TypeCount) + ((timeValuesLength + 4) * t.LeapCount) + t.IsStdCount + t.IsGmtCount + t.CharCount);
- // read the V2 header
- t = new TZifHead(data, index);
- index += TZifHead.Length;
- timeValuesLength = 8; // the second version uses 8-bytes
- }
- // initialize the containers for the rest of the TZ data
- dts = new DateTime[t.TimeCount];
- typeOfLocalTime = new byte[t.TimeCount];
- transitionType = new TZifType[t.TypeCount];
- zoneAbbreviations = string.Empty;
- StandardTime = new bool[t.TypeCount];
- GmtTime = new bool[t.TypeCount];
- // read in the UTC transition points and convert them to Windows
- //
- for (int i = 0; i < t.TimeCount; i++)
- {
- long unixTime = TZif_ToUnixTime(data, index, t.Version);
- dts[i] = TZif_UnixTimeToDateTime(unixTime);
- index += timeValuesLength;
- }
- // read in the Type Indices; there is a 1:1 mapping of UTC transition points to Type Indices
- // these indices directly map to the array index in the transitionType array below
- //
- for (int i = 0; i < t.TimeCount; i++)
- {
- typeOfLocalTime[i] = data[index];
- index += 1;
- }
- // read in the Type table. Each 6-byte entry represents
- // {UtcOffset, IsDst, AbbreviationIndex}
- //
- // each AbbreviationIndex is a character index into the zoneAbbreviations string below
- //
- for (int i = 0; i < t.TypeCount; i++)
- {
- transitionType[i] = new TZifType(data, index);
- index += 6;
- }
- // read in the Abbreviation ASCII string. This string will be in the form:
- // "PST\0PDT\0PWT\0\PPT"
- //
- Encoding enc = Encoding.UTF8;
- zoneAbbreviations = enc.GetString(data, index, (int)t.CharCount);
- index += (int)t.CharCount;
- // skip ahead of the Leap-Seconds Adjustment data. In a future release, consider adding
- // support for Leap-Seconds
- //
- index += (int)(t.LeapCount * (timeValuesLength + 4)); // skip the leap second transition times
- // read in the Standard Time table. There should be a 1:1 mapping between Type-Index and Standard
- // Time table entries.
- //
- // TRUE = transition time is standard time
- // FALSE = transition time is wall clock time
- // ABSENT = transition time is wall clock time
- //
- for (int i = 0; i < t.IsStdCount && i < t.TypeCount && index < data.Length; i++)
- {
- StandardTime[i] = (data[index++] != 0);
- }
- // read in the GMT Time table. There should be a 1:1 mapping between Type-Index and GMT Time table
- // entries.
- //
- // TRUE = transition time is UTC
- // FALSE = transition time is local time
- // ABSENT = transition time is local time
- //
- for (int i = 0; i < t.IsGmtCount && i < t.TypeCount && index < data.Length; i++)
- {
- GmtTime[i] = (data[index++] != 0);
- }
- if (t.Version != TZVersion.V1)
- {
- // read the POSIX-style format, which should be wrapped in newlines with the last newline at the end of the file
- if (data[index++] == '\n' && data[data.Length - 1] == '\n')
- {
- futureTransitionsPosixFormat = enc.GetString(data, index, data.Length - index - 1);
- }
- }
- }
- private struct TZifType
- {
- public const int Length = 6;
- public readonly TimeSpan UtcOffset;
- public readonly bool IsDst;
- public readonly byte AbbreviationIndex;
- public TZifType(byte[] data, int index)
- {
- if (data == null || data.Length < index + Length)
- {
- throw new ArgumentException(SR.Argument_TimeZoneInfoInvalidTZif, nameof(data));
- }
- UtcOffset = new TimeSpan(0, 0, TZif_ToInt32(data, index + 00));
- IsDst = (data[index + 4] != 0);
- AbbreviationIndex = data[index + 5];
- }
- }
- private struct TZifHead
- {
- public const int Length = 44;
- public readonly uint Magic; // TZ_MAGIC "TZif"
- public readonly TZVersion Version; // 1 byte for a \0 or 2 or 3
- // public byte[15] Reserved; // reserved for future use
- public readonly uint IsGmtCount; // number of transition time flags
- public readonly uint IsStdCount; // number of transition time flags
- public readonly uint LeapCount; // number of leap seconds
- public readonly uint TimeCount; // number of transition times
- public readonly uint TypeCount; // number of local time types
- public readonly uint CharCount; // number of abbreviated characters
- public TZifHead(byte[] data, int index)
- {
- if (data == null || data.Length < Length)
- {
- throw new ArgumentException("bad data", nameof(data));
- }
- Magic = (uint)TZif_ToInt32(data, index + 00);
- if (Magic != 0x545A6966)
- {
- // 0x545A6966 = {0x54, 0x5A, 0x69, 0x66} = "TZif"
- throw new ArgumentException(SR.Argument_TimeZoneInfoBadTZif, nameof(data));
- }
- byte version = data[index + 04];
- Version =
- version == '2' ? TZVersion.V2 :
- version == '3' ? TZVersion.V3 :
- TZVersion.V1; // default/fallback to V1 to guard against future, unsupported version numbers
- // skip the 15 byte reserved field
- // don't use the BitConverter class which parses data
- // based on the Endianess of the machine architecture.
- // this data is expected to always be in "standard byte order",
- // regardless of the machine it is being processed on.
- IsGmtCount = (uint)TZif_ToInt32(data, index + 20);
- IsStdCount = (uint)TZif_ToInt32(data, index + 24);
- LeapCount = (uint)TZif_ToInt32(data, index + 28);
- TimeCount = (uint)TZif_ToInt32(data, index + 32);
- TypeCount = (uint)TZif_ToInt32(data, index + 36);
- CharCount = (uint)TZif_ToInt32(data, index + 40);
- }
- }
- private enum TZVersion : byte
- {
- V1 = 0,
- V2,
- V3,
- // when adding more versions, ensure all the logic using TZVersion is still correct
- }
- }
- }
|