未验证 提交 e52ddd41 编写于 作者: M Mitchell Hwang 提交者: GitHub

[libs][Android] Filter out backwards timezones only with valid tzlookup file (#89933)

* [libs][Android] Filter out backwards timezones only with valid tzlookup file

* Address feedback

* [libs][Android] Skip NoBackwardTimeZones on Android API under 26
上级 c6572756
......@@ -265,12 +265,16 @@ public AndroidTzData()
//
// Once the timezone cache is populated with the IDs, we reference tzlookup id tags
// to determine if an id is backwards and label it as such if they are.
private static void FilterBackwardIDs(string tzFileDir, out HashSet<string> tzLookupIDs)
private static HashSet<string>? FilterBackwardIDs(string tzFileDir)
{
tzLookupIDs = new HashSet<string>();
string tzLookupFilePath = Path.Combine(tzFileDir, "tzlookup.xml");
if (!File.Exists(tzLookupFilePath))
return null;
HashSet<string>? tzLookupIDs = null;
try
{
using (StreamReader sr = File.OpenText(Path.Combine(tzFileDir, "tzlookup.xml")))
using (StreamReader sr = File.OpenText(tzLookupFilePath))
{
string? tzLookupLine;
while (sr.Peek() >= 0)
......@@ -285,12 +289,20 @@ private static void FilterBackwardIDs(string tzFileDir, out HashSet<string> tzLo
// Either the start tag <id ... > or the end tag </id> are not found
continue;
}
tzLookupIDs ??= new HashSet<string>();
string id = tzLookupLine.Substring(idStart, idLength);
tzLookupIDs.Add(id);
}
}
}
catch {}
catch
{
return null;
}
return tzLookupIDs;
}
[MemberNotNullWhen(true, nameof(_ids))]
......@@ -367,7 +379,7 @@ private void ReadIndex(string tzFileDir, Stream fs, int indexOffset, int dataOff
_ids = new string[entryCount];
_lengths = new int[entryCount];
_isBackwards = new bool[entryCount];
FilterBackwardIDs(tzFileDir, out HashSet<string> tzLookupIDs);
HashSet<string>? tzLookupIDs = FilterBackwardIDs(tzFileDir);
for (int i = 0; i < entryCount; ++i)
{
LoadEntryAt(fs, indexOffset + (entrySize*i), out string id, out int byteOffset, out int length);
......@@ -375,7 +387,8 @@ private void ReadIndex(string tzFileDir, Stream fs, int indexOffset, int dataOff
_byteOffsets[i] = byteOffset + dataOffset;
_ids[i] = id;
_lengths[i] = length;
_isBackwards[i] = !tzLookupIDs.Contains(id);
if (tzLookupIDs != null)
_isBackwards[i] = !tzLookupIDs.Contains(id);
if (length < 24) // Header Size
{
......
......@@ -3162,6 +3162,11 @@ public static void AdjustmentRuleBaseUtcOffsetDeltaTest()
[ActiveIssue("https://github.com/dotnet/runtime/issues/64111", TestPlatforms.Linux)]
public static void NoBackwardTimeZones()
{
if (OperatingSystem.IsAndroid() && !OperatingSystem.IsAndroidVersionAtLeast(26))
{
throw new SkipTestException("This test won't work on API level < 26");
}
ReadOnlyCollection<TimeZoneInfo> tzCollection = TimeZoneInfo.GetSystemTimeZones();
HashSet<String> tzDisplayNames = new HashSet<String>();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册