-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement kotlinx-datetime for the Android native targets (#344)
To test, run an Android emulator, and then, in the command line, ./gradlew androidNativeArm64TestBinaries && adb push core/build/bin/androidNativeArm64/debugTest/test.kexe /data/local/tmp/ && adb shell /data/local/tmp/test.kexe Change `Arm64` to another platfom (`X86`, `X64`, or `Arm32`) as needed.
- Loading branch information
1 parent
ab0a40d
commit 290a666
Showing
13 changed files
with
224 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright 2019-2023 JetBrains s.r.o. | ||
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. | ||
*/ | ||
|
||
@file:OptIn(ExperimentalForeignApi::class) | ||
package kotlinx.datetime.internal | ||
|
||
import kotlinx.cinterop.* | ||
import platform.posix.* | ||
|
||
internal actual val systemTzdb: TimeZoneDatabase get() = tzdb.getOrThrow() | ||
|
||
private val tzdb = runCatching { TzdbBionic() } | ||
|
||
internal actual fun currentSystemDefaultZone(): Pair<String, TimeZoneRules?> = memScoped { | ||
val name = readSystemProperty("persist.sys.timezone") | ||
?: throw IllegalStateException("The system property 'persist.sys.timezone' should contain the system timezone") | ||
return name to null | ||
} | ||
|
||
private fun readSystemProperty(name: String): String? = memScoped { | ||
// see https://android.googlesource.com/platform/bionic/+/froyo/libc/include/sys/system_properties.h | ||
val result = allocArray<ByteVar>(92) | ||
val error = __system_property_get(name, result) | ||
if (error == 0) null else result.toKString() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2019-2024 JetBrains s.r.o. and contributors. | ||
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. | ||
*/ | ||
/* | ||
* Based on the bionic project. | ||
* Copyright (C) 2017 The Android Open Source Project | ||
*/ | ||
|
||
package kotlinx.datetime.internal | ||
|
||
private class TzdbBionic(private val rules: Map<String, Entry>) : TimeZoneDatabase { | ||
override fun rulesForId(id: String): TimeZoneRules = | ||
rules[id]?.readRules() ?: throw IllegalStateException("Unknown time zone $id") | ||
|
||
override fun availableTimeZoneIds(): Set<String> = rules.keys | ||
|
||
class Entry(val file: ByteArray, val offset: Int, val length: Int) { | ||
fun readRules(): TimeZoneRules = readTzFile(file.copyOfRange(offset, offset + length)).toTimeZoneRules() | ||
} | ||
} | ||
|
||
// see https://android.googlesource.com/platform/bionic/+/master/libc/tzcode/bionic.cpp for the format | ||
internal fun TzdbBionic(): TimeZoneDatabase = TzdbBionic(buildMap<String, TzdbBionic.Entry> { | ||
for (path in listOf( | ||
Path.fromString("/system/usr/share/zoneinfo/tzdata"), // immutable fallback tzdb | ||
Path.fromString("/apex/com.android.tzdata/etc/tz/tzdata"), // an up-to-date tzdb, may not exist | ||
)) { | ||
if (path.check() == null) continue // the file does not exist | ||
// be careful to only read each file a single time and keep many references to the same ByteArray in memory. | ||
val content = path.readBytes() | ||
val header = BionicTzdbHeader.parse(content) | ||
val indexSize = header.data_offset - header.index_offset | ||
check(indexSize % 52 == 0) { "Invalid index size: $indexSize (must be a multiple of 52)" } | ||
val reader = BinaryDataReader(content, header.index_offset) | ||
repeat(indexSize / 52) { | ||
val name = reader.readNullTerminatedUtf8String(40) | ||
val start = reader.readInt() | ||
val length = reader.readInt() | ||
reader.readInt() // unused | ||
// intentionally overwrite the older entries | ||
put(name, TzdbBionic.Entry(content, header.data_offset + start, length)) | ||
} | ||
} | ||
}) | ||
|
||
// bionic_tzdata_header_t | ||
private class BionicTzdbHeader( | ||
val version: String, | ||
val index_offset: Int, | ||
val data_offset: Int, | ||
val final_offset: Int, | ||
) { | ||
override fun toString(): String = | ||
"BionicTzdbHeader(version='$version', index_offset=$index_offset, " + | ||
"data_offset=$data_offset, final_offset=$final_offset)" | ||
|
||
companion object { | ||
fun parse(content: ByteArray): BionicTzdbHeader = | ||
with(BinaryDataReader(content)) { | ||
BionicTzdbHeader( | ||
version = readNullTerminatedUtf8String(12), | ||
index_offset = readInt(), | ||
data_offset = readInt(), | ||
final_offset = readInt(), | ||
) | ||
}.apply { | ||
check(version.startsWith("tzdata") && version.length < 12) { "Unknown tzdata version: $version" } | ||
check(index_offset <= data_offset) { "Invalid data and index offsets: $data_offset and $index_offset" } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2019-2023 JetBrains s.r.o. and contributors. | ||
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. | ||
*/ | ||
|
||
@file:OptIn(ExperimentalForeignApi::class, UnsafeNumber::class) | ||
package kotlinx.datetime.internal | ||
|
||
import kotlinx.cinterop.* | ||
import platform.posix.* | ||
|
||
internal fun Path.chaseSymlinks(maxDepth: Int = 100): Path { | ||
var realPath = this | ||
var depth = maxDepth | ||
while (true) { | ||
realPath = realPath.readLink() ?: break | ||
if (depth-- == 0) throw RuntimeException("Too many levels of symbolic links") | ||
} | ||
return realPath | ||
} | ||
|
||
internal fun Path.traverseDirectory(exclude: Set<String> = emptySet(), stripLeadingComponents: Int = this.components.size, actionOnFile: (Path) -> Unit) { | ||
val handler = opendir(this.toString()) ?: return | ||
try { | ||
while (true) { | ||
val entry = readdir(handler) ?: break | ||
val name = entry.pointed.d_name.toKString() | ||
if (name == "." || name == "..") continue | ||
if (name in exclude) continue | ||
val path = Path(isAbsolute, components + name) | ||
val info = path.check() ?: continue // skip broken symlinks | ||
if (info.isDirectory) { | ||
if (!info.isSymlink) { | ||
path.traverseDirectory(exclude, stripLeadingComponents, actionOnFile) | ||
} | ||
} else { | ||
actionOnFile(Path(false, path.components.drop(stripLeadingComponents))) | ||
} | ||
} | ||
} finally { | ||
closedir(handler) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Copyright (C) 2017 The Android Open Source Project | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions | ||
are met: | ||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above copyright | ||
notice, this list of conditions and the following disclaimer in | ||
the documentation and/or other materials provided with the | ||
distribution. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | ||
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | ||
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | ||
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
SUCH DAMAGE. |