-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add LinuxLocalTimeFormatter, LinuxTimeZoneInfoProvider
- Loading branch information
1 parent
0b6d63b
commit ec9510d
Showing
17 changed files
with
285 additions
and
35 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
47 changes: 47 additions & 0 deletions
47
sqlite-tests/sqlite-test-utils/src/linuxMain/kotlin/TimeZoneExt.kt
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,47 @@ | ||
/* | ||
* Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file | ||
* for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package ru.pixnews.wasm.sqlite.test.utils | ||
|
||
import kotlinx.cinterop.toKStringFromUtf8 | ||
import platform.posix.errno | ||
import platform.posix.getenv | ||
import platform.posix.setenv | ||
import platform.posix.tzset | ||
import platform.posix.unsetenv | ||
|
||
public fun <R : Any> withTimeZone( | ||
timeZone: String, | ||
block: () -> R, | ||
) { | ||
val oldTz = getenv("TZ")?.toKStringFromUtf8() | ||
setEnvOrThrow("TZ", timeZone) | ||
tzset() | ||
try { | ||
block() | ||
} finally { | ||
if (oldTz == null) { | ||
val unsetEnvResult = unsetenv("TZ") | ||
if (unsetEnvResult < 0) { | ||
error("Can not unset TZ. Errno: $errno") | ||
} | ||
} else { | ||
setEnvOrThrow("TZ", oldTz) | ||
} | ||
tzset() | ||
} | ||
} | ||
|
||
private fun setEnvOrThrow( | ||
name: String, | ||
value: String, | ||
replace: Boolean = true, | ||
) { | ||
val setEnvResult = setenv(name, value, if (replace) 1 else 0) | ||
if (setEnvResult < 0) { | ||
error("Can not set $name to `$value`. Errno: $errno") | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
wasi-emscripten-host/src/commonMain/kotlin/internal/EmptyCommandArgsProvider.kt
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,13 @@ | ||
/* | ||
* Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file | ||
* for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package ru.pixnews.wasm.sqlite.open.helper.host.internal | ||
|
||
import ru.pixnews.wasm.sqlite.open.helper.host.EmbedderHost | ||
|
||
internal object EmptyCommandArgsProvider : EmbedderHost.CommandArgsProvider { | ||
override fun getCommandArgs(): List<String> = emptyList() | ||
} |
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
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
30 changes: 30 additions & 0 deletions
30
wasi-emscripten-host/src/linuxMain/kotlin/linux/LinuxSystemEnvProvider.kt
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,30 @@ | ||
/* | ||
* Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file | ||
* for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package ru.pixnews.wasm.sqlite.open.helper.host.linux | ||
|
||
import kotlinx.cinterop.ByteVarOf | ||
import kotlinx.cinterop.CPointer | ||
import kotlinx.cinterop.CPointerVarOf | ||
import kotlinx.cinterop.get | ||
import kotlinx.cinterop.toKStringFromUtf8 | ||
import platform.posix.__environ | ||
import ru.pixnews.wasm.sqlite.open.helper.host.EmbedderHost.SystemEnvProvider | ||
import ru.pixnews.wasm.sqlite.open.helper.host.native.parsePosixEnvironToEnvMap | ||
|
||
internal object LinuxSystemEnvProvider : SystemEnvProvider { | ||
override fun getSystemEnv(): Map<String, String> { | ||
val envVariables: MutableList<String> = mutableListOf() | ||
val env: CPointer<CPointerVarOf<CPointer<ByteVarOf<Byte>>>> = __environ ?: return emptyMap() | ||
var index = 0 | ||
while (true) { | ||
val current = env[index] ?: break | ||
envVariables += current.toKStringFromUtf8() | ||
index += 1 | ||
} | ||
return parsePosixEnvironToEnvMap(envVariables) | ||
} | ||
} |
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,10 @@ | ||
/* | ||
* Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file | ||
* for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
@file:Suppress("FILE_CONTAINS_ONLY_COMMENTS", "FILE_NO_BLANK_LINE_BETWEEN_BLOCKS") | ||
|
||
// Workaround for https://youtrack.jetbrains.com/issue/KTIJ-15797 | ||
package ru.pixnews.wasm.sqlite.open.helper.host |
36 changes: 36 additions & 0 deletions
36
wasi-emscripten-host/src/linuxTest/kotlin/linux/LinuxLocalTimeFormatterTest.kt
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,36 @@ | ||
/* | ||
* Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file | ||
* for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package ru.pixnews.wasm.sqlite.open.helper.host.linux | ||
|
||
import assertk.assertThat | ||
import assertk.assertions.isEqualTo | ||
import ru.pixnews.wasm.sqlite.open.helper.host.include.StructTm | ||
import ru.pixnews.wasm.sqlite.test.utils.withTimeZone | ||
import kotlin.test.Test | ||
|
||
class LinuxLocalTimeFormatterTest { | ||
@Test | ||
@Suppress("MagicNumber") | ||
fun formatter_should_work() = withTimeZone("Asia/Novosibirsk") { | ||
val tm: StructTm = LinuxLocalTimeFormatter.format(1_724_702_567) | ||
assertThat(tm).isEqualTo( | ||
StructTm( | ||
tm_sec = 47, | ||
tm_min = 2, | ||
tm_hour = 3, | ||
tm_mday = 27, | ||
tm_mon = 7, | ||
tm_year = 124, | ||
tm_wday = 2, | ||
tm_yday = 239, | ||
tm_isdst = 0, | ||
tm_gmtoff = 7 * 60 * 60, | ||
tm_zone = "+07", | ||
), | ||
) | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
wasi-emscripten-host/src/linuxTest/kotlin/linux/LinuxSystemEnvProviderTest.kt
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,33 @@ | ||
/* | ||
* Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file | ||
* for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package ru.pixnews.wasm.sqlite.open.helper.host.linux | ||
|
||
import assertk.all | ||
import assertk.assertThat | ||
import assertk.assertions.contains | ||
import assertk.assertions.isTrue | ||
import platform.posix.setenv | ||
import platform.posix.unsetenv | ||
import kotlin.test.Test | ||
|
||
class LinuxSystemEnvProviderTest { | ||
@Test | ||
fun env_provider_should_return_values() { | ||
val testEnvVarKey = "TESTENVVARKEY" | ||
val testEnvVarValue = " test = value" | ||
setenv(testEnvVarKey, testEnvVarValue, 1) | ||
try { | ||
val env = LinuxSystemEnvProvider.getSystemEnv() | ||
assertThat(env).all { | ||
contains(testEnvVarKey, testEnvVarValue) | ||
transform { it.containsKey("USER") }.isTrue() | ||
} | ||
} finally { | ||
unsetenv(testEnvVarKey) | ||
} | ||
} | ||
} |
Oops, something went wrong.