date
date copied to clipboard
Support Android for get current_zone()
Hello.
In Android, the timezone is not stored in /etc/localtime or any other file. We have to look for the value of the system property persist.sys.timezone instead.
This PR add this check for Android in current_zone()
function.
@ndusart @HowardHinnant is there any plan towards merging it to master branch?
We encountered a runtime_error
related to date::current_zone()
on Android device and now thinking around possible workarounds with that.
Does this patch fix your problem?
@HowardHinnant this patch really fixes the issue for me. Of course it should be noted that the library only works when an external TZ database is provided as it is unable to parse the one provided by Android.
However the PR as it is now have some issues.
- The
std::string result
is not used, so it can be removed. - The ifdef guards should be changed to
#if defined(ANDROID) || defined(__ANDROID__)
@TheStormN you are totally right, a TZ database needs to be included for this to work
But I wonder, since this library has been ported in the standard library, isn't this transparently supported by the std::chrono in latest NDK ?
@ndusart The only compilers which have support for time zones are MSVC 2022 and GCC 14. Clang libc++(used in NDK) is still very far from ready, even in their master branch. They have some code for Linux only. The TZ database which comes integrated with Android however is in binary format(single file) which of course can be parsed, there is C code available for that, but someone will have to port that parser to libc++.
P.S. If you are interested in that I can give you some hints:
- The code which is used to locate the TZ database on Android is here: https://android.googlesource.com/platform/bionic/+/refs/heads/main/libc/tzcode/bionic.cpp Good thing is that file is accessible from any app, no special permissions are needed.
- The code which have the binary format parser is here: https://android.googlesource.com/platform/bionic/+/refs/heads/main/libc/tzcode/localtime.c
Thanks for the information, that's interesting.
I rebased this PR to integrate the suggestions by @TheStormN.
Thanks a lot @ndusart ! Hopefully @HowardHinnant will merge it.
P.S. the check >= 1
is a bit strange. It could be just > 0
. Zero seems more natural number than one which looks like a magic number, but that's really a minor thing. :)