@@ -63,10 +63,36 @@ public expect open class TimeZone {
63
63
*
64
64
* If the current system time zone changes, this function can reflect this change on the next invocation.
65
65
*
66
- * On Linux, this function queries the `/etc/localtime` symbolic link. If the link is missing, [UTC] is used.
67
- * If the link points to an invalid location, [IllegalTimeZoneException] is thrown.
66
+ * It is recommended to call this function once at the start of an operation and reuse the result:
67
+ * querying the system time zone may involve heavy operations like reading the system files,
68
+ * and also, querying the system time zone multiple times in one operation may lead to inconsistent results
69
+ * if the system time zone changes in the middle of the operation.
68
70
*
69
- * Always returns the `UTC` timezone on the Wasm WASI platform due to the lack of support for retrieving system timezone information.
71
+ * How exactly the time zone is acquired is system-dependent. The current implementation:
72
+ * - JVM: `java.time.ZoneId.systemDefault()` is queried.
73
+ * - Kotlin/Native:
74
+ * - Darwin: first, `NSTimeZone.resetSystemTimeZone` is called to clear the cache of the system timezone.
75
+ * Then, `NSTimeZone.systemTimeZone.name` is used to obtain the up-to-date timezone name.
76
+ * - Linux: this function checks the `/etc/localtime` symbolic link.
77
+ * If the link is missing, [UTC] is used.
78
+ * If the file is not a link but a plain file,
79
+ * the contents of `/etc/timezone` are additionally checked for the timezone name.
80
+ * [IllegalTimeZoneException] is thrown if the timezone name cannot be determined
81
+ * or is invalid.
82
+ * - Windows: the `GetDynamicTimeZoneInformation` function is used,
83
+ * with the native Windows timezone name being mapped to the corresponding IANA identifier.
84
+ * [IllegalTimeZoneException] is thrown if this mapping fails. See [of] for details.
85
+ * - JavaScript and Wasm/JS:
86
+ * - If the `@js-joda/timezone` library is loaded,
87
+ * `Intl.DateTimeFormat().resolvedOptions().timeZone` is used to obtain the timezone name.
88
+ * See https://github.com/Kotlin/kotlinx-datetime/blob/master/README.md#note-about-time-zones-in-js
89
+ * - Otherwise, a time zone with the identifier `"SYSTEM"` is returned,
90
+ * and JS `Date`'s `getTimezoneOffset()` is used to obtain the offset for the given moment.
91
+ * - Wasm/WASI: always returns the `UTC` timezone,
92
+ * as the platform does not support retrieving system timezone information.
93
+ *
94
+ * Note that the implementation of this function for various platforms may change in the future,
95
+ * in particular, the JavaScript and Wasm/JS platforms.
70
96
*
71
97
* @sample kotlinx.datetime.test.samples.TimeZoneSamples.currentSystemDefault
72
98
*/
@@ -95,12 +121,37 @@ public expect open class TimeZone {
95
121
* It is guaranteed that passing any value from [availableZoneIds] to this function will return
96
122
* a valid time zone.
97
123
*
124
+ * How exactly the region-based time zone is acquired is system-dependent. The current implementation:
125
+ * - JVM: `java.time.ZoneId.of(zoneId)` is used.
126
+ * - Kotlin/Native:
127
+ * - Darwin devices: the timezone database in `/var/db/timezone/zoneinfo` is used by default,
128
+ * and if it is not a valid timezone database, the same search procedure as on Linux is used.
129
+ * - Darwin simulators: the timezone database in `/usr/share/zoneinfo.default` is used by default,
130
+ * and if it is not a valid timezone database, the same search procedure as on Linux is used.
131
+ * - Linux: `/usr/share/zoneinfo`, `/usr/share/lib/zoneinfo`, and `/etc/zoneinfo`
132
+ * are checked in turn for the timezone database.
133
+ * If none of them is a valid timezone database, `/etc/localtime` is checked.
134
+ * If it is a symlink of the form `.../zoneinfo/...`,
135
+ * the target of the symlink with the last part stripped is used as the timezone database.
136
+ * - Windows: the contents of the
137
+ * `HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones`
138
+ * registry key are queried to obtain the timezone database.
139
+ * Then, the Windows-specific timezone name is mapped to the corresponding IANA identifier
140
+ * using the information from the CLDR project:
141
+ * https://github.com/unicode-org/cldr/blob/main/common/supplemental/windowsZones.xml
142
+ * - JavaScript and Wasm/JS:
143
+ * if the `@js-joda/timezone` library is loaded,
144
+ * it is used to obtain the timezone rules.
145
+ * Otherwise, the [IllegalTimeZoneException] is thrown.
146
+ * See https://github.com/Kotlin/kotlinx-datetime/blob/master/README.md#note-about-time-zones-in-js
147
+ * - Wasm/WASI:
148
+ * if the `kotlinx-datetime-zoneinfo` artifact is added to the project as a dependency,
149
+ * it is used to obtain the timezone rules.
150
+ * Otherwise, the [IllegalTimeZoneException] is thrown.
151
+ *
98
152
* @throws IllegalTimeZoneException if [zoneId] has an invalid format or a time-zone with the name [zoneId]
99
153
* is not found.
100
154
*
101
- * @throws IllegalTimeZoneException on the Wasm WASI platform for non-fixed-offset time zones,
102
- * unless a dependency on the `kotlinx-datetime-zoneinfo` artifact is added.
103
- *
104
155
* @sample kotlinx.datetime.test.samples.TimeZoneSamples.constructorFunction
105
156
*/
106
157
public fun of (zoneId : String ): TimeZone
0 commit comments