Debian Codegraph @

debian/missing-sources/jiff/PLATFORM.md

raw symbols SCIP
1 # Platform support
2
3 This document describes Jiff's platform support. That is, it describes the
4 interaction points between this library and its environment. Most of the
5 details in this document are written down elsewhere on individual APIs, but
6 this document serves to centralize everything in one place.
7
8 As a general rule, interaction with the environment requires that Jiff's
9 `std` feature is enabled. The `std` feature is what allows Jiff to read
10 environment variables and files, for example.
11
12 Before starting, let's cover some vocabulary first.
13
14 ## Vocabulary
15
16 This section defines the key terms used below when describing platform support.
17 We also try to contextualize the concepts to make their meaning concrete in a
18 way that hopefully relates to your lived experience.
19
20 * [Civil time]: The time you see on your clock. And in general, the time that
21 the humans in your approximate geographic vicinity also see. That is, civil
22 time is a human coordinated agreement for communicating time in a particular
23 geographic region. Civil time is also known as: local time, plain time, naive
24 time, clock time and others.
25 * [Time zone]: A set of rules for determining the civil (or "local") time,
26 via an offset from UTC, in a particular geographic region. In many cases, the
27 offset in a particular time zone can vary over the course of a year through
28 transitions into and out of [daylight saving time]. A time zone is necessary
29 to convert civil time into a precise unambiguous instant in time.
30 * [IANA Time Zone Database]: A directory on your system containing a store of
31 files, one per time zone, which encode the time at which transitions between
32 UTC offsets occur in a specific geographic region. In effect, each time zone
33 file provides a mapping between civil (or "local") time and UTC. The format
34 of each file is called TZif and is specified by [RFC 8536]. This database is
35 typically found at `/usr/share/zoneinfo` and only on Unix systems (including
36 macOS). Other environments, like Windows and WASM, do not have a standard copy
37 of the Time Zone Database. (Jiff will instead embed it into your program by
38 default on these platforms.)
39 * [IANA time zone identifier]: A short ASCII human readable string identifying
40 a time zone in the IANA Time Zone Database. The time zone for where I live,
41 `America/New_York`, has an entry at `/usr/share/zoneinfo/America/New_York`
42 on my system. IANA time zone identifiers are used by Jiff's `Zoned` type
43 to losslessly roundtrip datetimes via an interchange format specified by
44 [Temporal] that draws inspiration from [RFC 3339], [RFC 9557] and [ISO 8601].
45
46 ## Environment variables
47
48 Jiff generally only reads two environment variables. These variables are
49 read on all platforms that support environment variables. So for example,
50 Jiff will respect `TZ` on Windows. Note though that some environments, like
51 `wasm32-wasip1` or `wasm32-unknown-emscripten`, are sandboxed by default. A
52 sandboxed environment typically makes reading environment variables set outside
53 the sandbox impossible (or require opt-in support, such as [wasmtime]'s
54 `-S inherit-env` or `--env` flags).
55
56 Jiff may read additional environment variables for platform specific
57 integration.
58
59 ### `TZDIR`
60
61 The `TZDIR` environment variable tells Jiff where to look for the
62 [IANA Time Zone Database]. When it isn't set, Jiff will check a few standard
63 locations for the database. It's usually found at `/usr/share/zoneinfo`.
64
65 It can be useful to set this for non-standard environments or when you
66 specifically want Jiff to prefer using a non-system copy of the database.
67 (If you want Jiff to _only_ use a non-system copy of the database, then you'll
68 need to use `TimeZoneDatabase::from_dir` and use the resulting handle
69 explicitly.)
70
71 If a IANA Time Zone Database could not be found at `TZDIR`, then Jiff will
72 still attempt to look for a database at the standard locations (like
73 `/usr/share/zoneinfo`).
74
75 ### `TZ`
76
77 The `TZ` environment variable overrides and sets the default system time zone.
78 It is [specified by POSIX][POSIX TZ]. Jiff implements the POSIX specification
79 (even on non-POSIX platforms like Windows) with some common extensions.
80
81 It is useful to set `TZ` when Jiff could not detect (or had a problem
82 detecting) the system time zone, or if the system time zone is wrong in a
83 specific circumstance.
84
85 Summarizing POSIX (and common extensions supported by GNU libc and musl), the
86 `TZ` environment variable accepts these kinds of values:
87
88 * `America/New_York` sets the time zone via a IANA time zone identifier.
89 * `/usr/share/zoneinfo/America/New_York` sets the time zone by providing a path
90 to a TZif formatted file.
91 * `EST5EDT,M3.2.0,M11.1.0` sets the time zone using a POSIX daylight saving
92 time rule. The rule shown here is for `US/Eastern` at time of writing (2024).
93 This is useful for specifying a custom time zone without generating TZif data,
94 but is rarely used in practice.
95
96 When `TZ` isn't set, then Jiff uses heuristics to detect the system's
97 configured time zone. If this automatic detection fails, please first check
98 for an [existing issue for your platform][issue-platform], and if one doesn't
99 exist, please [file a new issue][issue-new]. Otherwise, setting `TZ` should be
100 considered as a work-around.
101
102 ### `ANDROID_ROOT` and `ANDROID_DATA`
103
104 These environment variables are read to help determine the location of
105 Android's [Concatenated Time Zone Database]. If `ANDROID_ROOT` is not defined,
106 then Jiff uses `/system` as its default value. If `ANDROID_DATA` is not
107 defined, then Jiff uses `/data/misc` as its default value.
108
109 Note that these environment variables are not necessarily only read on
110 Android, although they likely only make sense in the context of an Android
111 environment. This is because Jiff's support for the Concatenated Time
112 Zone Database is platform independent. For example, Jiff will let users
113 create a database from a Concatenated Time Zone Database file via the
114 `TimeZoneDatabase::from_concatenated_path` API on _any_ platform. This is
115 intended to enable maximum flexibility, and because there is no specific reason
116 to make the Concatenated Time Zone Database format Android-specific.
117
118 ## Platforms
119
120 This section lists the platforms that Jiff has explicit support for. Support
121 may not be perfect, so if something isn't working as it should, check the
122 list of [existing platform related issues][issue-platform]. If you can't find
123 one that matches your specific problem, [create a new issue][issue-new].
124
125 For each platform, there are generally three things to consider:
126
127 1. Whether getting the current time is supported.
128 2. How Jiff finds the IANA Time Zone Database.
129 3. How Jiff finds the system configured time zone.
130
131 We answer these questions for each platform.
132
133 ### Unix
134
135 #### Current time
136
137 All Unix platforms should be supported in terms of getting the current time.
138 This support comes from Rust's standard library.
139
140 #### IANA Time Zone Database
141
142 The vast majority of Unix systems, including macOS, store a copy of the IANA
143 time zone database at `/usr/share/zoneinfo`, which Jiff will automatically
144 detect. If your Unix system uses a different directory, you may try to submit
145 a PR adding support for it in Jiff proper, or just set the `TZDIR` environment
146 variable.
147
148 The existence of `/usr/share/zoneinfo` is not guaranteed in all Unix
149 environments. For example, stripped down Docker containers might omit a full
150 copy of the time zone database. Jiff will still work in such environments, but
151 all IANA time zone identifier lookups will fail. To fix this, you can either
152 install the IANA Time Zone Database into your environment, or you can enable
153 the Jiff crate feature `tzdb-bundle-always`. This compile time setting will
154 cause Jiff to depend on `jiff-tzdb`, which includes a complete copy of the IANA
155 Time Zone Database embedded into the compiled artifact.
156
157 Bundling the IANA Time Zone Database should only be done as a last resort.
158 Especially on Unix systems, it is greatly preferred to use the system copy of
159 the database, as the database is typically updated a few times each year. By
160 using the system copy, Jiff will automatically pick up updates without needing
161 to be recompiled.
162
163 But if bundling is needed, it is a fine solution. It just means that Jiff will
164 need to be re-compiled after `jiff-tzdb` is updated when a new IANA Time Zone
165 Database release is published.
166
167 #### System time zone
168
169 On most Unix systems, the system configured time zone manifests as a symbolic
170 link at `/etc/localtime`. The symbolic link usually points to a file in
171 your system copy of the IANA Time Zone Database. For example, on my Linux
172 system:
173
174 ```text
175 $ ls -l /etc/localtime
176 lrwxrwxrwx 1 root root 36 Jul 15 20:26 /etc/localtime -> /usr/share/zoneinfo/America/New_York
177 ```
178
179 And my macOS system:
180
181 ```text
182 $ ls -l /etc/localtime
183 lrwxr-xr-x 1 root wheel 42 Jun 20 07:13 /etc/localtime -> /var/db/timezone/zoneinfo/America/New_York
184 ```
185
186 Jiff examines the symbolic link metadata to extract the IANA time zone
187 identifier from the file path. In the above two examples, that would be
188 `America/New_York`. The identifier is then used to do a lookup in the system
189 copy of the IANA Time Zone Database.
190
191 If `/etc/localtime` is not a symbolic link, then Jiff reads it directly as a
192 TZif file. When this happens, Jiff cannot feasibly know the IANA time zone
193 identifier. While arithmetic on the resulting `Zoned` value will still be DST
194 safe, one cannot losslessly serialize and deserialize it since Jiff won't be
195 able to include the IANA time zone identifier in the serialized format. When
196 such a `Zoned` value is serialized, the offset of the datetime will be used
197 in lieu of the IANA time zone identifier.
198
199 (NOTE: Not all Unix systems follow this pattern. If your system uses a
200 different way to configure the system time zone, please check [available
201 platform issues][issue-platform] for a related issue. If one doesn't exist,
202 please [create a new issue][issue-new].)
203
204 ### Android
205
206 #### Current time
207
208 All Android platforms should be supported in terms of getting the current time.
209 This support comes from Rust's standard library.
210
211 #### IANA Time Zone Database
212
213 Unlike effectively every other Unix system, Android has its own special time
214 zone database format. While it still makes use of TZif formatted data for
215 defining time zone transitions themselves, it does not use the `zoneinfo`
216 directory format (where there is one file per time zone). Instead, it
217 _concatenates_ all time zone files into one single file. This is combined with
218 some meta data that makes it quick to search for time zones by their IANA time
219 zone identifier.
220
221 This format is technically unnamed, but Jiff refers to it as the [Concatenated
222 Time Zone Database] format. It has no formal specification. Jiff's
223 implementation was done by inferring the format implemented by the Android
224 Platform and also the implementation in [Go's standard library]. In practice
225 this tends to work well, although there are obviously no guarantees. This is
226 a practical trade-off given that there doesn't appear to be any obvious
227 alternative. Moreover, others (such as Go, a project maintained by the same
228 company that maintains Android) are already doing it, so it seems likely that
229 if Android decides to make breaking changes to the format, they'll need to
230 version it in some way to avoid breaking the ecosystem.
231
232 Note that Jiff supports reading this format on all platforms, not just Android.
233 For example, Jiff users can use the `TimeZoneDatabase::from_concatenated_path`
234 API to create a `TimeZoneDatabase` from a concatenated `tzdata` file on any
235 platform.
236
237 If users of Jiff are uncomfortable relying on Android's "unstable" time zone
238 database format, then there are a few options available to them after disabling
239 the `tzdb-concatenated` crate feature:
240
241 * They can own the responsibility of putting a standard `zoneinfo` database
242 installation into their environment. Then set the `TZDIR` environment variable
243 to point at it, and Jiff will automatically use it.
244 * Enable the `tzdb-bundle-always` crate feature. This will cause the entire
245 time zone database to be compiled into your binary. Nothing else needs to be
246 done. Jiff will automatically use the bundled copy.
247 * Manually create `TimeZone` values via `TimeZone::tzif` from TZif formatted
248 data. With this approach, you may need to change how you use Jiff in some
249 cases. For example, any `in_tz` method will need to be changed to use the
250 `to_zoned` equivalent.
251 * Embed specific time zones into your binary with `jiff::tz::get` or
252 `jiff::tz::include`. This requires enabling Jiff's `static` feature.
253
254 #### System time zone
255
256 The system time zone on Android is discovered by reading the
257 `persist.sys.timezone` property.
258
259 Note that in addition to Android developers citing the [Concatenated Time Zone
260 Database] format as unstable, they also discourage the discovery of the system
261 time zone through properties as well. (See [chrono#1018] and [chrono#1148]
262 for some discussion on this topic.) For Jiff at least, there is no feasible
263 alternative. Apparently, the blessed API is to use their Java libraries, but
264 that doesn't seem feasible to me inside of Jiff since I (Jiff's author) is
265 unaware of a mechanism for easily calling Java code from Rust. The only option
266 left is to use their `libc` APIs, which they did at least improve to make them
267 thread safe, but this isn't enough for Jiff. For Jiff, we really want the
268 actual IANA time zone identifier, and it isn't clear how to discover this from
269 their `libc` APIs. Moreover, Jiff supports far more sophisticated operations on
270 a time zone (like dealing with discontinuities in civil time) that cannot be
271 implemented on top of `libc`-style APIs. Using Android's `libc` APIs for time
272 handling would be a huge regression compared to all other platforms.
273
274 It's worth noting that all other popular Unix systems provide at least some
275 reliable means of both querying the time zone database _and_ discovering the
276 system-wide IANA time zone identifier. Why Android is incapable of following
277 the existing conventions for Unix systems is unclear.
278
279 If users of Jiff are uncomfortable relying on Android's `persist.sys.timezone`
280 property, then they should avoid APIs like `Zoned::now` and `TimeZone::system`.
281 Instead, they can use `TimeZone::unknown()`, which is what the fallback time
282 zone would be when the system time zone cannot be discovered.
283
284 ### Windows
285
286 #### Current time
287
288 All Windows platforms should be supported in terms of getting the current time.
289 This support comes from Rust's standard library.
290
291 #### IANA Time Zone Database
292
293 Windows does not have a canonical installation of the IANA Time Zone Database
294 like Unix. Because of this, and because of the importance of time zone support
295 to Jiff's design, Jiff will automatically embed an entire copy of the IANA Time
296 Zone Database into your binary on Windows.
297
298 The automatic bundling is done via the Jiff crate feature
299 `tzdb-bundle-platform`. This is a _target activated feature_. Namely, it is
300 enabled by default, but only results in a bundled database on an enumerated set
301 of platforms (where Windows is one of them). If you want to opt out of bundling
302 the database on Windows, you'll need to disable this feature.
303
304 Bundling the IANA Time Zone Database is not ideal, since after a new release of
305 the database, you'll need to wait for the `jiff-tzdb` crate to be updated. Then
306 you'll need to update your dependency version and re-compile your software to
307 get the database updates.
308
309 One alternative is to point Jiff to a copy of the IANA Time Zone Database via
310 the `TZDIR` environment variable. Even on Windows, Jiff will attempt to read
311 the directory specified as a time zone database. But you'll likely need to
312 manage the database yourself.
313
314 #### System time zone
315
316 Jiff currently uses [`GetDynamicTimeZoneInformation`] from the Windows C API
317 to query the current time zone information. This provides a value of type
318 [`DYNAMIC_TIME_ZONE_INFORMATION`]. Jiff uses the `TimeZoneKeyName` member
319 of that type to do a lookup in Unicode's [CLDR XML data] that maps Windows
320 time zone names to IANA time zone identifiers. The resulting IANA time zone
321 identifier is then used as a key to find a time zone in the configured IANA
322 Time Zone Database.
323
324 ### WASM
325
326 There are a variety of WASM targets available for Rust that service different
327 use cases. Here is a possibly incomplete list of those targets and a short
328 imprecise blurb about them:
329
330 * `wasm32-unknown-emscripten`: Sandboxed and emulates Unix as much as possible.
331 * `wasm32-wasi` and `wasm32-wasip1`: Provides a sandbox with capability-based
332 security. This is not typically used in web browsers. [wasmtime] is an example
333 of a runtime that can run programs compiled for these targets.
334 * `wasm{32,64}-unknown-unknown`: Typically used for web deployments to run in
335 a browser via `wasm-pack`. But, crucially, not exclusively so.
336
337 Jiff supports all of these targets, but the nature of that support varies. Each
338 target is discussed in the sections below.
339
340 #### The `js` crate feature
341
342 Jiff comes with a `js` crate feature that is disabled by default. It is a
343 _target activated feature_ that enables dependencies on the `js-sys` and
344 `wasm-bindgen` crates. This feature is intended to be enabled only in binaries,
345 tests and benchmarks when it is known that the code will be running in a
346 web context. Consequently, this feature only activates this support for the
347 `wasm{32,64}-unknown-unknown` targets. It has no effect on any other target,
348 including other WASM targets.
349
350 Library crates should generally never enable Jiff's `js` feature or even
351 forward it. Applications using your library can depend on Jiff directly and
352 enable the feature.
353
354 #### Current time
355
356 * `wasm32-unknown-emscripten`: Supported via Rust's standard library.
357 * `wasm32-wasi*`: Supported via Rust's standard library.
358 * `wasm{32,64}-unknown-unknown`: `std::time::SystemTime::now()`, and thus
359 `Zoned::now()`, panics in Jiff's default configuration. Enabling Jiff's `js`
360 feature will cause Jiff to assume a web context and use JavaScript's
361 [`Date.now`] API to determine the current time.
362
363 #### IANA Time Zone Database
364
365 None of the WASM targets have a canonical installation of the IANA Time Zone
366 Database. Because of this, and because of the importance of time zone support
367 to Jiff's design, Jiff will automatically embed an entire copy of the IANA
368 Time Zone Database into your binary on all WASM targets.
369
370 The automatic bundling is done via the Jiff crate feature
371 `tzdb-bundle-platform`. This is a _target activated feature_. Namely, it is
372 enabled by default, but only results in a bundled database on an enumerated set
373 of platforms (where WASM is one of them). If you want to opt out of bundling
374 the database on WASM targets, you'll need to disable this feature.
375
376 Bundling the IANA Time Zone Database is not ideal, since after a new release of
377 the database, you'll need to wait for the `jiff-tzdb` crate to be updated. Then
378 you'll need to update your dependency version and re-compile your software to
379 get the database updates.
380
381 Some WASM targets, like `wasm32-wasip1`, can actually read the host's
382 IANA Time Zone Database (e.g., on Unix), but this requires relaxing its
383 sandbox restrictions so that the code can read system directories like
384 `/usr/share/zoneinfo`. That is, it won't work out of the box. The same applies
385 to the `wasm32-unknown-emscripten` target. (Although this author could not
386 figure out how to relax emscripten's sandbox.)
387
388 #### System time zone
389
390 * `wasm32-unknown-emscripten`: Unsupported.
391 * `wasm32-wasi*`: Unsupported. But you may set the `TZ` environment variable
392 via your WASM runtime, and Jiff will respect it. For example, with [wasmtime],
393 that's `--env TZ=America/New_York`.
394 * `wasm{32,64}-unknown-unknown`: Unsupported in Jiff's default configuration.
395 Enabling Jiff's `js` feature will cause Jiff to assume a web context and use
396 JavaScript's [`Intl.DateTimeFormat`] API to determine the system configured
397 IANA time zone identifier. This time zone identifier is then used to look up
398 the time zone in Jiff's configured IANA Time Zone Database.
399
400 [Civil time]: https://en.wikipedia.org/wiki/Civil_time
401 [Time zone]: https://en.wikipedia.org/wiki/Time_zone
402 [daylight saving time]: https://en.wikipedia.org/wiki/Daylight_saving_time
403 [IANA Time Zone Database]: https://en.wikipedia.org/wiki/Tz_database
404 [IANA time zone identifier]: https://data.iana.org/time-zones/theory.html#naming
405 [RFC 8536]: https://datatracker.ietf.org/doc/html/rfc8536
406 [wasmtime]: https://wasmtime.dev/
407 [POSIX TZ]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html
408 [RFC 3339]: https://www.rfc-editor.org/rfc/rfc3339
409 [RFC 9557]: https://www.rfc-editor.org/rfc/rfc9557.html
410 [ISO 8601]: https://www.iso.org/iso-8601-date-and-time-format.html
411 [Temporal]: https://tc39.es/proposal-temporal
412 [issue-platform]: https://github.com/BurntSushi/jiff/issues?q=label%3Aplatform
413 [issue-new]: https://github.com/BurntSushi/jiff/issues/new
414 [`GetDynamicTimeZoneInformation`]: https://learn.microsoft.com/en-us/windows/win32/api/timezoneapi/nf-timezoneapi-getdynamictimezoneinformation
415 [`DYNAMIC_TIME_ZONE_INFORMATION`]: https://learn.microsoft.com/en-us/windows/win32/api/timezoneapi/ns-timezoneapi-dynamic_time_zone_information
416 [CLDR XML data]: https://github.com/unicode-org/cldr/raw/main/common/supplemental/windowsZones.xml
417 [`Intl.DateTimeFormat`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/resolvedOptions#timezone
418 [`Date.now`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now
419 [Concatenated Time Zone Database]: https://android.googlesource.com/platform/libcore/+/jb-mr2-release/luni/src/main/java/libcore/util/ZoneInfoDB.java
420 [Go's standard library]: https://github.com/golang/go/blob/19e923182e590ae6568c2c714f20f32512aeb3e3/src/time/zoneinfo_android.go
421 [chrono#1018]: https://github.com/chronotope/chrono/pull/1018
422 [chrono#1148]: https://github.com/chronotope/chrono/pull/1148