Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android][libraries] TimeZoneInfo Android imp #54845

Merged
merged 82 commits into from
Jul 21, 2021

Conversation

mdh1418
Copy link
Member

@mdh1418 mdh1418 commented Jun 28, 2021

Fixes #41867

Android has removed zone.tab, so TimeZoneInfo.Unix.cs will no longer work properly on Android as GetTimeZoneIds directly depends on the zone.tab file. The chain of dependency is as follows
 
GetSystemTimeZones -> PopulateAllSystemTimeZones -> GetTimeZoneIds -> zone.tab
                TZI.cs                                  TZI.Unix.cs                        TZI.Unix.cs         TZI.Unix.cs
 Where TZI is TimeZoneInfo

zone.tab is a file that is found on the unix system under /usr/share/zoneinfo/
GetTimeZoneIds reads zone.tab to obtain the TimeZoneId in that file
PopulateAllSystemTimeZones caches all the TimeZone Ids in cachedData
GetSystemTimeZones returns a ReadOnlyCollection containing all valid TimeZone’s from the local machine, and the entries are sorted by their DisplayName. It relies on cachedData._systemTimeZones being populated.


The problem is that the time zone data for Android can be found in the file tzdata at the possible locations

/apex/com.android.tzdata/etc/tz/tzdata
/apex/com.android.runtime/etc/tz/tzdata
/data/misc/zoneinfo/tzdata
/system/usr/share/zoneinfo/tzdata

The rest of unix the time zone data can be found in the file zone.tab at

 /usr/share/zoneinfo/zone.tab

Android's TimeZoneInfo implementation should read time zone data from its locations instead of the general /usr/share/zoneinfo/zone.tab path. Moreover, tzdata contains all timezones byte data.

This PR achieves the following:

  1. Splits TimeZoneInfo.Unix.cs into TimeZoneInfo.Unix.cs, TimeZoneInfo.Unix.NonAndroid.cs (non-Android), and TimeZoneInfo.Unix.Android.cs (Android specific)
  2. Adds an interop to obtain the default time zone on Android based on persist.sys.timezone
  3. Implements GetLocalTimeZoneCore TryGetTimeZoneFromLocalMachineCore and GetTimeZoneIds for Android based on mono/mono implementation https://github.com/mono/mono/blob/main/mcs/class/corlib/System/TimeZoneInfo.Android.cs
  4. Adds new string resources to throw exceptions
  5. Refactors the mono/mono implementation of parsing tzdata
Android tzdata files are found in the format of
Header <Beginning of Entry Index> Entry Entry Entry ... Entry <Beginning of Data Index> <TZDATA>

https://github.com/aosp-mirror/platform_bionic/blob/master/libc/tzcode/bionic.cpp

The header (24 bytes) contains the following information
signature - 12 bytes of the form "tzdata2012f\0" where 2012f is subject to change
index offset - 4 bytes that denotes the offset at which the index of the tzdata file starts
data offset - 4 bytes that denotes the offset at which the data of the tzdata file starts
final offset - 4 bytes that used to denote the final offset, which we don't use but will note.

Each Data Entry (52 bytes) can be used to generate a TimeZoneInfo and contain the following information
id - 40 bytes that contain the id of the time zone data entry timezone<id>
byte offset - 4 bytes that denote the offset from the data offset timezone<id> data can be found
length - 4 bytes that denote the length of the data for timezone<id>
unused - 4 bytes that used to be raw GMT offset, but now is always 0 since tzdata2014f (L).

When GetLocalTimeZoneCore TryGetTimeZoneFromLocalMachineCore or GetTimeZoneIds are called, an android timezone data instance is instantiated and loaded by attempting to load a tzdata file that can be found at four locations mentioned earlier. The file is parsed by first loading the header which contains information about where the data index and data begin. The data index is then parsed to obtain the timezone and the corresponding bytes location in the file to fill the three arrays _ids _byteOffsets _lengths. These arrays are referenced to obtain the corresponding byte data for a timezone, and functions from TimeZoneInfo.Unix.cs are leveraged to create a TimeZoneInfo from there.

@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label.

@mdh1418 mdh1418 marked this pull request as ready for review June 28, 2021 18:13
@mdh1418
Copy link
Member Author

mdh1418 commented Jun 28, 2021

Unlike other Unix implementations where zone.tab contains meta-data and specific timezone files (Europe/London) contain the actual timezone data, tzdata on Android combines all timezone data together with the metadata into tzdata. As such, more mono implementation is needed to be ported over in order to get TryGetTimeZoneFromLocalMachine to work.

@steveisok
Copy link
Member

@mdh1418 As I suspected, there's a little bit more to this. I have more local and we can talk tomorrow about the details.

}
}

return Environment.GetEnvironmentVariable("ANDROID_ROOT") + DefaultTimeZoneDirectory;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This look odd. Are you sure we should mix 2 rooted locations?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ghost
Copy link

ghost commented Jun 29, 2021

Tagging subscribers to this area: @tarekgh, @safern
See info in area-owners.md if you want to be subscribed.

Issue Details

Fixes #41867

Android has removed zone.tab, so TimeZoneInfo.Unix.cs will no longer work properly on Android as GetTimeZoneIds directly depends on the zone.tab file. The chain of dependency is as follows
 
GetSystemTimeZones -> PopulateAllSystemTimeZones -> GetTimeZoneIds -> zone.tab
                TZI.cs                                  TZI.Unix.cs                        TZI.Unix.cs         TZI.Unix.cs
 Where TZI is TimeZoneInfo

zone.tab is a file that is found on the unix system under /usr/share/zoneinfo/
GetTimeZoneIds reads zone.tab to obtain the TimeZoneId in that file
PopulateAllSystemTimeZones caches all the TimeZone Ids in cachedData
GetSystemTimeZones returns a ReadOnlyCollection containing all valid TimeZone’s from the local machine, and the entries are sorted by their DisplayName. It relies on cachedData._systemTimeZones being populated.


The problem is that the time zone data for Android can be found in the file tzdata at the possible locations

/apex/com.android.tzdata/etc/tz/tzdata
/apex/com.android.runtime/etc/tz/tzdata
/data/misc/zoneinfo/tzdata
/system/usr/share/zoneinfo/tzdata

The rest of unix the time zone data can be found in the file zone.tab at

 /usr/share/zoneinfo/zone.tab

As zone.tab and tzdata have the same format and contents, Android's TimeZoneInfo implementation should read time zone data from its locations instead of the general /usr/share/zoneinfo/zone.tab path.

This PR achieves the following:

  1. Splits TimeZoneInfo.Unix.cs into TimeZoneInfo.AnyUnix.cs, TimeZoneInfo.Unix.cs (non-Android), and TimeZoneInfo.Android.cs (Android specific)
  2. Modifies which directory is used in PopulateAllSystemTimeZones GetTimeZoneDirectory() following the order of https://github.com/mono/mono/blob/7791e4d94206dd63acff49268b56a01c0b69983c/mcs/class/corlib/System/TimeZoneInfo.Android.cs#L68-L73
  3. Renames ZoneTabFileName to TimeZoneFileName and split definitions for Android or non-Android (TimeZoneInfo.Unix.cs)
Author: mdh1418
Assignees: -
Labels:

area-System.Globalization

Milestone: -

@ghost
Copy link

ghost commented Jun 29, 2021

Tagging subscribers to 'arch-android': @steveisok, @akoeplinger
See info in area-owners.md if you want to be subscribed.

Issue Details

Fixes #41867

Android has removed zone.tab, so TimeZoneInfo.Unix.cs will no longer work properly on Android as GetTimeZoneIds directly depends on the zone.tab file. The chain of dependency is as follows
 
GetSystemTimeZones -> PopulateAllSystemTimeZones -> GetTimeZoneIds -> zone.tab
                TZI.cs                                  TZI.Unix.cs                        TZI.Unix.cs         TZI.Unix.cs
 Where TZI is TimeZoneInfo

zone.tab is a file that is found on the unix system under /usr/share/zoneinfo/
GetTimeZoneIds reads zone.tab to obtain the TimeZoneId in that file
PopulateAllSystemTimeZones caches all the TimeZone Ids in cachedData
GetSystemTimeZones returns a ReadOnlyCollection containing all valid TimeZone’s from the local machine, and the entries are sorted by their DisplayName. It relies on cachedData._systemTimeZones being populated.


The problem is that the time zone data for Android can be found in the file tzdata at the possible locations

/apex/com.android.tzdata/etc/tz/tzdata
/apex/com.android.runtime/etc/tz/tzdata
/data/misc/zoneinfo/tzdata
/system/usr/share/zoneinfo/tzdata

The rest of unix the time zone data can be found in the file zone.tab at

 /usr/share/zoneinfo/zone.tab

As zone.tab and tzdata have the same format and contents, Android's TimeZoneInfo implementation should read time zone data from its locations instead of the general /usr/share/zoneinfo/zone.tab path.

This PR achieves the following:

  1. Splits TimeZoneInfo.Unix.cs into TimeZoneInfo.AnyUnix.cs, TimeZoneInfo.Unix.cs (non-Android), and TimeZoneInfo.Android.cs (Android specific)
  2. Modifies which directory is used in PopulateAllSystemTimeZones GetTimeZoneDirectory() following the order of https://github.com/mono/mono/blob/7791e4d94206dd63acff49268b56a01c0b69983c/mcs/class/corlib/System/TimeZoneInfo.Android.cs#L68-L73
  3. Renames ZoneTabFileName to TimeZoneFileName and split definitions for Android or non-Android (TimeZoneInfo.Unix.cs)
Author: mdh1418
Assignees: -
Labels:

os-android

Milestone: -

@ghost
Copy link

ghost commented Jun 29, 2021

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

Issue Details

Fixes #41867

Android has removed zone.tab, so TimeZoneInfo.Unix.cs will no longer work properly on Android as GetTimeZoneIds directly depends on the zone.tab file. The chain of dependency is as follows
 
GetSystemTimeZones -> PopulateAllSystemTimeZones -> GetTimeZoneIds -> zone.tab
                TZI.cs                                  TZI.Unix.cs                        TZI.Unix.cs         TZI.Unix.cs
 Where TZI is TimeZoneInfo

zone.tab is a file that is found on the unix system under /usr/share/zoneinfo/
GetTimeZoneIds reads zone.tab to obtain the TimeZoneId in that file
PopulateAllSystemTimeZones caches all the TimeZone Ids in cachedData
GetSystemTimeZones returns a ReadOnlyCollection containing all valid TimeZone’s from the local machine, and the entries are sorted by their DisplayName. It relies on cachedData._systemTimeZones being populated.


The problem is that the time zone data for Android can be found in the file tzdata at the possible locations

/apex/com.android.tzdata/etc/tz/tzdata
/apex/com.android.runtime/etc/tz/tzdata
/data/misc/zoneinfo/tzdata
/system/usr/share/zoneinfo/tzdata

The rest of unix the time zone data can be found in the file zone.tab at

 /usr/share/zoneinfo/zone.tab

As zone.tab and tzdata have the same format and contents, Android's TimeZoneInfo implementation should read time zone data from its locations instead of the general /usr/share/zoneinfo/zone.tab path.

This PR achieves the following:

  1. Splits TimeZoneInfo.Unix.cs into TimeZoneInfo.AnyUnix.cs, TimeZoneInfo.Unix.cs (non-Android), and TimeZoneInfo.Android.cs (Android specific)
  2. Modifies which directory is used in PopulateAllSystemTimeZones GetTimeZoneDirectory() following the order of https://github.com/mono/mono/blob/7791e4d94206dd63acff49268b56a01c0b69983c/mcs/class/corlib/System/TimeZoneInfo.Android.cs#L68-L73
  3. Renames ZoneTabFileName to TimeZoneFileName and split definitions for Android or non-Android (TimeZoneInfo.Unix.cs)
Author: mdh1418
Assignees: -
Labels:

area-System.Runtime, os-android

Milestone: -

…Zone into separate Unix and Android functions.
@steveisok steveisok added the NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) label Jun 29, 2021
*/
private sealed class AndroidTzData
{
private unsafe struct AndroidTzDataHeader
Copy link
Member

@eerhardt eerhardt Jul 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private unsafe struct AndroidTzDataHeader
private readonly struct AndroidTzDataHeader

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error CS8340: Instance fields of readonly structs must be readonly.
/Users/mdhwang/runtime_droid/src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.Unix.Android.cs(314,17): error CS0191: A readonly field cannot be assigned to (except in a constructor or init-only setter of the type in which the field is defined or a variable initializer)

I went ahead and removed this struct altogether, using out parameters for indexOffset and dataOffset in LoadTzFile and LoadHeader

Comment on lines 412 to 414
Span<byte> buffer = stackalloc byte[length];
ReadTzDataIntoBuffer(File.OpenRead(_tzFilePath), offset, buffer);
byte[] tzBuffer = buffer.ToArray();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No reason to make a stack buffer here, copy the file into the buffer, and then ToArray it.

  1. It is unnecessary copying
  2. We don't know that the length of the file will fit on the stack. In general, stackalloc's should not be unbounded.
Suggested change
Span<byte> buffer = stackalloc byte[length];
ReadTzDataIntoBuffer(File.OpenRead(_tzFilePath), offset, buffer);
byte[] tzBuffer = buffer.ToArray();
byte[] tzBuffer = new byte[length];
ReadTzDataIntoBuffer(File.OpenRead(_tzFilePath), offset, tzBuffer);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I see, was it unnecessary copying because we need to still return a byte[] here, whereas in LoadTzFile we didn't "need" to use a byte[] and a Span<byte> would suffice?

Is there a general rule for how large a stackalloc should be?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was it unnecessary copying because we need to still return a byte[] here, whereas in LoadTzFile we didn't "need" to use a byte[] and a Span would suffice?

Correct. In LoadTzFile, you just need a buffer to read the file data into, and then you are parsing the values out of it from the buffer. The buffer didn't need to stick around.

Here, since the method expects you to return a byte[], you might as well just read directly into the byte[] you are going to return.

Is there a general rule for how large a stackalloc should be?

Looking around the codebase, we usually don't let a stackalloc go above 1K of memory.

@mdh1418
Copy link
Member Author

mdh1418 commented Jul 19, 2021

Made a few more changes, including parsing the data entry bytes to construct a string immediately, removing the structs all together, removed unsafe keywords, and updated the description of this PR. Can I get another review @eerhardt @jkotas @steveisok

@steveisok steveisok merged commit 46d9b31 into dotnet:main Jul 21, 2021
@mdh1418 mdh1418 deleted the port_android_timezoneinfo branch July 26, 2021 12:58
@ghost ghost locked as resolved and limited conversation to collaborators Aug 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Port Android APEX timezone location lookup from mono/mono
9 participants