ziglang/zig
262
5.9k
Fork
You've already forked zig
763

A plan for 64-bit time_t in the standard library #31414

Open
opened 2026年03月07日 12:32:37 +01:00 by The-King-of-Toasters · 2 comments

Prevously:

  • #4726.
  • #21440 (symbol shenanigans for glibc and musl).

The Zig standard library is not time64 clean, but since most of the development/deployment is done on 64-bit targets it hasn't become an issue. For the standard library to be time64 clean, two things need to be done for each target:

  1. Use the most up-to-date types.
  2. Pass them to the right functions.

Sounds simple, but after looking into this I've come up with a list of issues:

  • Some targets have no concept of "time" - think of the freestanding/real-time OSs where time-keeping functionality can be stripped out.
  • Retrieving time differs between OSs: C/POSIX-compatibles use time_t, timespec and clock_gettime whilst Windows and Plan 9 use completely different APIs.
  • The POSIX-compatibles may have multiple versions of their types/APIs thanks to backwards-compatibility.

Since most of the issues stem from the Nixen, where time_t/timespec are set in the ABI, this issue is focused on them. Reformulating the tasks then:

  • If the target requires using the C standard library and/or is POSIX-compatible:
    1. Use the largest time_t/timespec possible for the cpu-os-abi combo.
    2. Pass these types to the proper time64-compatible APIs.

Since time_t, timespec and friends show up in many places (stating, sleeping, signaling etc.), any existing structures that use these types will need to be audited and modified if necessary.

The following sections describe how each Nixen the standard library supports handles 64-bit time.

  • macOS/Darwin: time_t -> __darwin_time_t -> long for x86 and arm; no time64 support for the ILP32 targets, which AFAIK is only an issue on the Apple Watch (it uses a ILP32 userspace).
  • OpenBSD: Broke ABI to use 64-bit time in 2013. No special types/APIs needed.
  • FreeBSD: Currently, time_t is 32-bit on x86_32 and 64-bit on all other targets. FreeBSD 15 dropped support for 32-bit targets along with MIPS64 and big endian PowerPC, so this only matters for x86-freebsd in user-space.
  • DragonFlyBSD: time_t is 64-bit since x86_64 is the only supported arch.
  • NetBSD: Migrated targets to use 64-bit time in NetBSD 6. The time64 symbols to use are of the form __{function}50.
  • Illumos: Solaris (and thus Illumos) never added time64 support for the 32-bit targets. None of this matters though since Illumos only supports x86_64.
  • Linux: Oh boy...

Linux - Kernel

The kernel is generally considered time64 ready since 5.6. It achieves this by adding new types and syscalls (__kernel_time64_t, __kernel_timespec). The catch is that while older 32-bit targets have two versions of a syscall, newer ones like riscv32/loongarch32 only use the newer ones. The process then for std.os.linux is:

  • Only define the modern types (e.g. __kernel_timespec).
  • Pass them to the proper syscalls per-target:
    • If it's a 64-bit target, do nothing.
    • If it's a 32-bit target with time32 and time64 syscalls, use the time64 ones.
    • If it's a 32-bit target with only time64 syscalls, use the time64 ones.
  • Implement older syscalls via newer ones if a target doesn't include them (utimes -> utimensat).

Linux - Musl

Musl added complete time64 in Musl 1.2.0 without changing the ABI. It does this by using ASM Labels to alter symbol names, but only on 32-bit targets that support the time32 syscalls (grep for _REDIR_TIME64 in arch/{arch}/bits/alltypes.h.in). On these targets:

  • The time32 compat library under compat/time32 is compiled in.
  • The symbols for the compat functions are redirected to their normal form; e.g. __nanosleep_time32 -> nanosleep.
  • Inversely, the symbols for the normal functions are redirected to have a time64 suffix; e.g. nanosleep -> __nanosleep_time64.

This was done to maintain compatibility with programs dynamically linking to musl before the 1.2.0 release.
The solution is to perform the same redirection in std.c.

Linux - glibc

Glibc uses the same ASM label trick based on the macros _TIME_BITS, __TIMESIZE and __USE_TIME_BITS64:

  • _TIME_BITS overrides the time abi in use. As of 2.35 this is time64 by default.
  • __TIMESIZE is the bit-size of __kernel_old_time_t if the target needs it (i.e. old 32-bit ones).
  • __USE_TIME_BITS64 is set to 0 or 1 depending if _TIME_BITS==64 or __TIMESIZE==64.
  • 32bit compat functions are compiled in using the normal name if __TIMESIZE==32.

Unfortunately, musl and glibc don't use the same naming scheme for their symbols: glibc uses __nanosleep64 whilst musl uses __nanosleep_time64.

Prevously: - [#4726][4726]. - [#21440](https://github.com/ziglang/zig/pull/21440) (symbol shenanigans for glibc and musl). [4726]: https://github.com/ziglang/zig/issues/4726 --- The Zig standard library is not [time64 clean][4726], but since most of the development/deployment is done on 64-bit targets it hasn't become an issue. For the standard library to be time64 clean, two things need to be done for each target: 1. Use the most up-to-date types. 2. Pass them to the right functions. Sounds simple, but after looking into this I've come up with a list of issues: - Some targets have no concept of "time" - think of the freestanding/real-time OSs where time-keeping functionality can be stripped out. - Retrieving time differs between OSs: C/POSIX-compatibles use `time_t`, `timespec` and `clock_gettime` whilst Windows and Plan 9 use completely different APIs. - The POSIX-compatibles may have multiple versions of their types/APIs thanks to backwards-compatibility. Since most of the issues stem from the Nixen, where `time_t`/`timespec` are set in the ABI, this issue is focused on them. Reformulating the tasks then: - *If* the target requires using the C standard library and/or is POSIX-compatible: 1. Use the largest `time_t`/`timespec` possible for the cpu-os-abi combo. 2. Pass these types to the proper time64-compatible APIs. Since `time_t`, `timespec` and friends show up in many places (`stat`ing, `sleep`ing, `signal`ing etc.), any existing structures that use these types will need to be audited and modified if necessary. The following sections describe how each Nixen the standard library supports handles 64-bit time. - **macOS/Darwin**: `time_t -> __darwin_time_t -> long` for x86 and arm; no time64 support for the ILP32 targets, which AFAIK is only an issue on the Apple Watch (it uses a ILP32 userspace). - **OpenBSD**: [Broke ABI][openbsd] to use 64-bit time in 2013. No special types/APIs needed. - **FreeBSD**: Currently, `time_t` is [32-bit on `x86_32`][freebsd] and 64-bit on all other targets. FreeBSD 15 dropped support for 32-bit targets along with MIPS64 and big endian PowerPC, so this only matters for `x86-freebsd` in user-space. - **DragonFlyBSD**: `time_t` is [64-bit][dragonfly] since `x86_64` is the only supported arch. - **NetBSD**: [Migrated targets][netbsd] to use 64-bit time in NetBSD 6. The time64 symbols to use are of the form `__{function}50`. - **Illumos**: Solaris (and thus Illumos) [never added time64 support][solaris-blog] for the 32-bit targets. None of this matters though since Illumos only supports x86_64. - **Linux**: Oh boy... [openbsd]: https://undeadly.org/cgi?action=article;sid=20130813072244 [freebsd]: https://github.com/freebsd/freebsd-src/blob/178d0b5b8da7480f455273aedf40dd8f1e785d3f/sys/x86/include/_types.h#L70-L78 [dragonfly]: https://github.com/DragonFlyBSD/DragonFlyBSD/blob/065436f551fb2a156f3f7a6b58f96b074662c835/sys/cpu/x86_64/include/stdint.h#L122 [netbsd]: https://wiki.netbsd.org/symbol_versions/ [solaris-blog]: https://blogs.oracle.com/solaris/moving-oracle-solaris-to-lp64-bit-by-bit#lp64-abi-changes ## Linux - Kernel The kernel is [generally considered][musl-time64] time64 ready since 5.6. It achieves this by adding new types and syscalls (`__kernel_time64_t`, `__kernel_timespec`). The catch is that while older 32-bit targets have two versions of a syscall, newer ones like `riscv32`/`loongarch32` only use the newer ones. The process then for `std.os.linux` is: [musl-time64]: https://musl.libc.org/time64.html - Only define the modern types (e.g. `__kernel_timespec`). - Pass them to the proper syscalls per-target: - If it's a 64-bit target, do nothing. - If it's a 32-bit target with time32 and time64 syscalls, use the time64 ones. - If it's a 32-bit target with only time64 syscalls, use the time64 ones. - Implement older syscalls via newer ones if a target doesn't include them (`utimes -> utimensat`). ## Linux - Musl Musl added complete time64 in [Musl 1.2.0][musl-time64] without changing the ABI. It does this by using [ASM Labels][asm-label] to alter symbol names, but only on 32-bit targets that support the time32 syscalls (grep for `_REDIR_TIME64` in `arch/{arch}/bits/alltypes.h.in`). On these targets: [asm-label]: https://gcc.gnu.org/onlinedocs/gcc/Asm-Labels.html - The `time32` compat library under `compat/time32` is compiled in. - The symbols for the compat functions are redirected to their normal form; e.g. `__nanosleep_time32 -> nanosleep`. - Inversely, the symbols for the normal functions are redirected to have a `time64` suffix; e.g. `nanosleep -> __nanosleep_time64`. This was done to maintain compatibility with programs dynamically linking to musl before the 1.2.0 release. The solution is to perform the same redirection in `std.c`. ## Linux - glibc Glibc uses the same ASM label trick based on the macros `_TIME_BITS`, `__TIMESIZE` and `__USE_TIME_BITS64`: - `_TIME_BITS` overrides the time abi in use. As of 2.35 this is time64 by default. - `__TIMESIZE` is the bit-size of `__kernel_old_time_t` if the target needs it (i.e. old 32-bit ones). - `__USE_TIME_BITS64` is set to `0` or `1` depending if `_TIME_BITS==64` or `__TIMESIZE==64`. - 32bit compat functions are compiled in using the normal name if `__TIMESIZE==32`. Unfortunately, musl and glibc don't use the same naming scheme for their symbols: glibc uses `__nanosleep64` whilst musl uses `__nanosleep_time64`.
alexrp added this to the Urgent milestone 2026年03月08日 20:32:34 +01:00

@The-King-of-Toasters wrote in #31414 (comment):

  • macOS/Darwin: time_t -> __darwin_time_t -> long for x86 and arm; no time64 support for the ILP32 targets, which AFAIK is only an issue on the Apple Watch (it uses a ILP32 userspace).

Newer watches have moved to fully 64-bit userland, so I don't think that'll be a concern by 2038.

  • FreeBSD: Currently, time_t is 32-bit on x86_32 and 64-bit on all other targets. FreeBSD 15 dropped support for 32-bit targets along with MIPS64 and big endian PowerPC, so this only matters for x86-freebsd in user-space.

armv7 is still a supported platform. What's the situation there?

@The-King-of-Toasters wrote in https://codeberg.org/ziglang/zig/issues/31414#issue-3742852: > * **macOS/Darwin**: `time_t -> __darwin_time_t -> long` for x86 and arm; no time64 support for the ILP32 targets, which AFAIK is only an issue on the Apple Watch (it uses a ILP32 userspace). Newer watches have moved to fully 64-bit userland, so I don't think that'll be a concern by 2038. > * **FreeBSD**: Currently, `time_t` is [32-bit on `x86_32`](https://github.com/freebsd/freebsd-src/blob/178d0b5b8da7480f455273aedf40dd8f1e785d3f/sys/x86/include/_types.h#L70-L78) and 64-bit on all other targets. FreeBSD 15 dropped support for 32-bit targets along with MIPS64 and big endian PowerPC, so this only matters for `x86-freebsd` in user-space. armv7 is still a supported platform. What's the situation there?
Author
Contributor
Copy link

See arch(7):

time_t is 8 bytes on all supported architectures except i386.

See [arch(7)](https://man.freebsd.org/cgi/man.cgi?arch): > `time_t` is 8 bytes on all supported architectures except i386.
alexrp modified the milestone from Urgent to 0.17.0 2026年03月24日 09:33:19 +01:00
alexrp modified the milestone from 0.17.0 to 0.18.0 2026年04月21日 23:01:32 +02:00
Sign in to join this conversation.
No Branch/Tag specified
master
std.zig.Ast.ParseOptions
elfv2
spork8
restricted
0.16.x
panic-rewrite
parking-futex-lockfree
windows-Io-cleanup
Io-watch
ParseCommandLineOptions
windows-async-files
poll-ring
debug-file-leaks-differently
debug-file-leaks
ProcessPrng
elfv2-dyn
jobserver
threadtheft
io-threaded-no-queue
0.15.x
Io.net
comptime-allocator
restricted-function-pointers
cli
wasm-linker-writer
wrangle-writer-buffering
sha1-stream
async-await-demo
fixes
0.14.x
ast-node-methods
macos-debug-info
make-vs-configure
fuzz-macos
sans-aro
ArrayList-reserve
incr-bug
llvm-ir-nosanitize-metadata
ci-tarballs
ci-scripts
threadpool
0.12.x
new-pkg-hash
json-diagnostics
more-doctests
rework-comptime-mutation
0.11.x
ci-perf-comment
stage2-async
0.10.x
autofix
0.9.x
aro
hcs
0.8.x
0.7.x
0.16.0
0.15.2
0.15.1
0.15.0
0.14.1
0.14.0
0.12.1
0.13.0
0.12.0
0.11.0
0.10.1
0.10.0
0.9.1
0.9.0
0.8.1
0.8.0
0.7.1
0.7.0
0.6.0
0.5.0
0.4.0
0.3.0
0.2.0
0.1.1
0.1.0
Labels
Clear labels
abi/f32
abi/ilp32
abi/sf
accepted
This proposal is planned.
arch/21k
arch/6502
arch/aarch64
arch/alpha
arch/amdgcn
arch/arc
arch/arc32
arch/arc64
arch/arm
arch/avr
arch/bfin
arch/bpf
arch/colossus
arch/cris
arch/csky
arch/dlx
arch/epiphany
arch/fr30
arch/frv
arch/hexagon
arch/hppa
arch/hppa64
arch/ia64
arch/kalimba
arch/kvx
arch/lanai
arch/lm32
arch/loongarch32
arch/loongarch64
arch/m32r
arch/m68k
arch/m88k
arch/mcore
arch/microblaze
arch/mips
arch/mips64
arch/mmix
arch/moxie
arch/mrisc32
arch/msp430
arch/nds32
arch/ns32k
arch/nvptx
arch/or1k
arch/powerpc
arch/powerpc64
arch/propeller
arch/riscv32
arch/riscv64
arch/rl78
arch/rx
arch/s390x
arch/sh
arch/sparc
arch/sparc64
arch/spirv
arch/spu
arch/tricore
arch/v850
arch/vax
arch/vc4
arch/ve
arch/wasm
arch/x86
arch/x86_64
arch/xcore
arch/xtensa
autodoc
The web application for interactive documentation and generation of its assets.
backend/c
The C backend outputs C source code.
backend/llvm
The LLVM backend outputs an LLVM bitcode module.
backend/self-hosted
The self-hosted backends produce machine code directly.
binutils
Zig's included binary utilities: zig ar, zig dlltool, zig lib, zig ranlib, zig objcopy, and zig rc.
breaking
Implementing this issue could cause existing code to no longer compile or have different behavior.
build system
The Zig build system - zig build, std.Build, the build runner, and package management.
debug info
An issue related to debug information (e.g. DWARF) produced by the Zig compiler.
docs
An issue with documentation, e.g. the language reference or standard library doc comments.
error message
This issue points out an error message that is unhelpful and should be improved.
frontend
Tokenization, parsing, AstGen, ZonGen, Sema, Legalize, and Liveness.
fuzzing
An issue related to Zig's integrated fuzz testing.
incremental
Reuse of internal compiler state for faster compilation.
lib/c
This issue relates to Zig's libc implementation and/or vendored libcs.
lib/compiler-rt
This issue relates to Zig's compiler-rt library.
lib/cxx
This issue relates to Zig's vendored libc++ and/or libc++abi.
lib/std
This issue relates to Zig's standard library.
lib/tsan
This issue relates to Zig's vendored libtsan.
lib/ubsan-rt
This issue relates to Zig's ubsan-rt library.
lib/unwind
This issue relates to Zig's vendored libunwind.
linking
Zig's integrated object file and incremental linker.
miscompilation
The compiler reports success but produces semantically incorrect code.
os/android
os/contiki
os/dragonfly
os/driverkit
os/emscripten
os/freebsd
os/fuchsia
os/haiku
os/hermit
os/hurd
os/illumos
os/ios
os/linux
os/maccatalyst
os/macos
os/managarm
os/netbsd
os/ohos
os/openbsd
os/plan9
os/redox
os/rtems
os/serenity
os/tvos
os/uefi
os/visionos
os/wasi
os/watchos
os/windows
proposal
This issue suggests language modifications. If it also has the "accepted" label then it is planned.
release notes
This issue or pull request should be mentioned in the release notes.
testing
This issue is related to testing the compiler, standard library, or other parts of Zig.
zig cc
Zig as a drop-in C-family compiler.
zig fmt
The Zig source code formatter.
zig reduce
The Zig source code reduction tool.
bounty
https://ziglang.org/news/announcing-donor-bounties
bug
Observed behavior contradicts documented or intended behavior.
contributor-friendly
This issue is limited in scope and/or knowledge of project internals.
downstream
An issue with a third-party project that uses this project.
enhancement
Solving this issue will likely involve adding new logic or components to the codebase.
infra
An issue related to project infrastructure, e.g. continuous integration.
optimization
A task to improve performance and/or resource usage.
question
No questions on the issue tracker; use a community space instead.
regression
Something that used to work in a previous version stopped working
upstream
An issue with a third-party project that this project uses.
use case
Describes a real use case that is difficult or impossible, but does not propose a solution.
No labels
abi/f32
abi/ilp32
abi/sf
accepted
arch/21k
arch/6502
arch/aarch64
arch/alpha
arch/amdgcn
arch/arc
arch/arc32
arch/arc64
arch/arm
arch/avr
arch/bfin
arch/bpf
arch/colossus
arch/cris
arch/csky
arch/dlx
arch/epiphany
arch/fr30
arch/frv
arch/hexagon
arch/hppa
arch/hppa64
arch/ia64
arch/kalimba
arch/kvx
arch/lanai
arch/lm32
arch/loongarch32
arch/loongarch64
arch/m32r
arch/m68k
arch/m88k
arch/mcore
arch/microblaze
arch/mips
arch/mips64
arch/mmix
arch/moxie
arch/mrisc32
arch/msp430
arch/nds32
arch/ns32k
arch/nvptx
arch/or1k
arch/powerpc
arch/powerpc64
arch/propeller
arch/riscv32
arch/riscv64
arch/rl78
arch/rx
arch/s390x
arch/sh
arch/sparc
arch/sparc64
arch/spirv
arch/spu
arch/tricore
arch/v850
arch/vax
arch/vc4
arch/ve
arch/wasm
arch/x86
arch/x86_64
arch/xcore
arch/xtensa
autodoc
backend/c
backend/llvm
backend/self-hosted
binutils
breaking
build system
debug info
docs
error message
frontend
fuzzing
incremental
lib/c
lib/compiler-rt
lib/cxx
lib/std
lib/tsan
lib/ubsan-rt
lib/unwind
linking
miscompilation
os/android
os/contiki
os/dragonfly
os/driverkit
os/emscripten
os/freebsd
os/fuchsia
os/haiku
os/hermit
os/hurd
os/illumos
os/ios
os/linux
os/maccatalyst
os/macos
os/managarm
os/netbsd
os/ohos
os/openbsd
os/plan9
os/redox
os/rtems
os/serenity
os/tvos
os/uefi
os/visionos
os/wasi
os/watchos
os/windows
proposal
release notes
testing
zig cc
zig fmt
zig reduce
bounty
bug
contributor-friendly
downstream
enhancement
infra
optimization
question
regression
upstream
use case
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ziglang/zig#31414
Reference in a new issue
ziglang/zig
No description provided.
Delete branch "%!s()"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?