Skip to content

[android] Support for time zones in Android. #2168

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

Merged
merged 1 commit into from
May 16, 2019

Conversation

drodriguez
Copy link
Contributor

@drodriguez drodriguez commented Apr 24, 2019

The time zone database in Android sits in a non-standard place, and it
can be overriden by a second location. Besides not sitting in a standard
place, it differs from other Unixes in that the database is bundled into
one binary file, and there are no folders with small files for each time
zone. Luckily the database is generated from Olson, so most of the
CFTimeZone code already handles the details.

The code implements a generic enumeration of the time zones contained in
the database (checking for both the override and the standard location).
The enumeration code is used for both enumerating the known time zones,
as well as creating time zones by name.

At many points there was code blocking the usage of time zones in
Android that has been removed.

This allows the tests in TestDateFormatter, TestCalendar and
TestTimeZone to pass on Android.

@drodriguez drodriguez requested a review from compnerd April 24, 2019 01:06
@drodriguez drodriguez force-pushed the android-time-zone-database branch from e416b11 to 76bd79d Compare April 24, 2019 01:52
@spevans
Copy link
Contributor

spevans commented Apr 24, 2019

@swift-ci test

Copy link
Contributor Author

@drodriguez drodriguez left a comment

Choose a reason for hiding this comment

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

Thanks for the feedback. I will split the PR in two, and fix all the memory management problems.

@drodriguez
Copy link
Contributor Author

This might not apply cleanly without #2172. But one can look only at the second commit, if you want the Android specific changes only.

@drodriguez drodriguez force-pushed the android-time-zone-database branch from cf58b49 to 473cd46 Compare April 24, 2019 19:01
@drodriguez
Copy link
Contributor Author

After fighting a little with the removal of __InitTZStrings and the multiple conditional compilation branches it introduces, I don’t think it is a great idea, but that's the code that it is up for review. If we want to back out of that change because it introduces complexity, we are still on time to do it.

@@ -702,7 +829,9 @@ static CFTimeZoneRef __CFTimeZoneCreateSystem(void) {
} else {
name = CFStringCreateWithBytes(kCFAllocatorSystemDefault, (uint8_t *)linkbuf, strlen(linkbuf), kCFStringEncodingUTF8, false);
}
} else {
} else
Copy link
Member

Choose a reason for hiding this comment

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

I think it might be nicer to just end the previous block with the one duplicated line for the return and then avoid the else and braces for the remainder.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it is not possible here, since there’s a block in the middle that has to be run for both branches. The return cannot be moved here.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, did I miss that? What is the block which needs to be executed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The if (name) block that starts in L890 (new version). The previous branches (the if and the else branch, only the else is valid for Android), are trying to find the name, while that last bit is find the information about the time zone with that name.

Copy link
Member

Choose a reason for hiding this comment

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

How about folding !TARGET_OS_ANDROID into the if condition on L868?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure exactly why you mean (please provide the code of what you mean), but __tzZoneInfo is used in L866, and that variable doesn't exist in Android, so it has to be compiled out.

@drodriguez drodriguez force-pushed the android-time-zone-database branch from 473cd46 to 102da92 Compare April 26, 2019 00:08
@drodriguez
Copy link
Contributor Author

@swift-ci please test

@drodriguez
Copy link
Contributor Author

@compnerd, @spevans: thanks for all the feedback already. I was wondering if there's more pieces that anyone thinks need improving, or we can plan to merge this into master. Thanks!

The time zone database in Android sits in a non-standard place, and it
can be overridedn by a second location. Besides not sitting in a standard
place, it differs from other Unixes in that the database is bundled into
one binary file, and there are no folders with small files for each time
zone. Luckily the database is generated from Olson, so most of the
CFTimeZone code already handles the details.

The code implements a generic enumeration of the time zones contained in
the database (checking for both the override and the standard location).
The enumeration code is used for both enumerating the known time zones,
as well as creating time zones by name.

At many points there was code blocking the usage of time zones in
Android that has been removed.

This allows the tests in TestDateFormatter, TestCalendar and
TestTimeZone to pass on Android.
@drodriguez drodriguez force-pushed the android-time-zone-database branch from 102da92 to 6454141 Compare May 13, 2019 21:35
@drodriguez
Copy link
Contributor Author

Fixed some merge errors for the Android code.

@swift-ci please test

@finagolfin
Copy link
Member

Heh, I had to make almost the same changes to the previous commit to get it to compile, though I didn't spot the fclose fix and it would segfault.

Now, several dozen more timezone tests pass on Android AArch64 with this latest commit, nice work.

Copy link
Member

@compnerd compnerd left a comment

Choose a reason for hiding this comment

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

Minor style things which I think would be nice, but I think this is good to go otherwise.

* - context1: The argument passed into __CFAndroidTimeZoneListEnumerate.
* - context2: The argument passed into __CFAndroidTimeZoneListEnumerate.
*/
typedef Boolean (*__CFAndroidTimeZoneListEnumerateCallback)(const char name[ANDROID_TZ_ENTRY_NAME_LENGTH], int32_t offset, int32_t length, FILE *fp, void *context1, void *context2);
Copy link
Member

Choose a reason for hiding this comment

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

I would give them proper names rather than context1 and context2. (e.g. name, data)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They are name and data in the case you are calling __CFTimeZoneDataCreateCallback, but context1 is actually a CFMutableArrayRef in the case of calling __CFCopyAndroidTimeZoneListCallback. That's why I used just one context before.

@@ -702,7 +829,9 @@ static CFTimeZoneRef __CFTimeZoneCreateSystem(void) {
} else {
name = CFStringCreateWithBytes(kCFAllocatorSystemDefault, (uint8_t *)linkbuf, strlen(linkbuf), kCFStringEncodingUTF8, false);
}
} else {
} else
Copy link
Member

Choose a reason for hiding this comment

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

How about folding !TARGET_OS_ANDROID into the if condition on L868?

@drodriguez drodriguez merged commit 7e67c02 into swiftlang:master May 16, 2019
@drodriguez drodriguez deleted the android-time-zone-database branch May 16, 2019 19:46
finagolfin added a commit to finagolfin/swift-corelibs-foundation that referenced this pull request Oct 11, 2021
Revert an Android-specific block that was already removed in swiftlang#2168 and don't
check for baseURL in _CFTimeZoneInit(), since Android doesn't use it anyway.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants