Skip to content

Commit 451f309

Browse files
authored
build with 64-bit time_t on 32-bit platforms (#82595)
It is good practice to build with 64-bit `time_t`/timeval on 32-bit platforms to avoid the Y2038 issue. This is the default when building on Yocto for armv7, for example. Unfortunately `suseconds_t` is not an alias to a type of the correct width (unlike time_t). Question: on release/6.1, tv_usec is assumed to be `Int32`, but on main it is `Int`, but appears to be the same commit hash? #### git blame main stdlib/public/Platform/Platform.swift ``` e675b31 (Philippe Hausler 2022-02-17 09:32:46 -0800 350) @available(SwiftStdlib 5.7, *) e675b31 (Philippe Hausler 2022-02-17 09:32:46 -0800 351) extension timeval { e675b31 (Philippe Hausler 2022-02-17 09:32:46 -0800 352) @available(SwiftStdlib 5.7, *) e675b31 (Philippe Hausler 2022-02-17 09:32:46 -0800 353) public init(_ duration: Duration) { e675b31 (Philippe Hausler 2022-02-17 09:32:46 -0800 354) let comps = duration.components e675b31 (Philippe Hausler 2022-02-17 09:32:46 -0800 355) // Linux platforms define timeval as Int/Int e675b31 (Philippe Hausler 2022-02-17 09:32:46 -0800 356) self.init(tv_sec: Int(comps.seconds), e675b31 (Philippe Hausler 2022-02-17 09:32:46 -0800 357) tv_usec: Int(comps.attoseconds / 1_000_000_000_000)) e675b31 (Philippe Hausler 2022-02-17 09:32:46 -0800 358) } e675b31 (Philippe Hausler 2022-02-17 09:32:46 -0800 359) } ``` #### git blame release/6.1 stdlib/public/Platform/Platform.swift ``` e675b31 (Philippe Hausler 2022-02-17 09:32:46 -0800 455) @available(SwiftStdlib 5.7, *) e675b31 (Philippe Hausler 2022-02-17 09:32:46 -0800 456) extension timeval { e675b31 (Philippe Hausler 2022-02-17 09:32:46 -0800 457) @available(SwiftStdlib 5.7, *) e675b31 (Philippe Hausler 2022-02-17 09:32:46 -0800 458) public init(_ duration: Duration) { e675b31 (Philippe Hausler 2022-02-17 09:32:46 -0800 459) let comps = duration.components e675b31 (Philippe Hausler 2022-02-17 09:32:46 -0800 460) #if os(Linux) e675b31 (Philippe Hausler 2022-02-17 09:32:46 -0800 461) // Linux platforms define timeval as Int/Int e675b31 (Philippe Hausler 2022-02-17 09:32:46 -0800 462) self.init(tv_sec: Int(comps.seconds), e675b31 (Philippe Hausler 2022-02-17 09:32:46 -0800 463) tv_usec: Int(comps.attoseconds / 1_000_000_000_000)) e675b31 (Philippe Hausler 2022-02-17 09:32:46 -0800 464) #else e675b31 (Philippe Hausler 2022-02-17 09:32:46 -0800 465) // Darwin platforms define timeval as Int/Int32 e675b31 (Philippe Hausler 2022-02-17 09:32:46 -0800 466) self.init(tv_sec: Int(comps.seconds), e675b31 (Philippe Hausler 2022-02-17 09:32:46 -0800 467) tv_usec: Int32(comps.attoseconds / 1_000_000_000_000)) e675b31 (Philippe Hausler 2022-02-17 09:32:46 -0800 468) #endif e675b31 (Philippe Hausler 2022-02-17 09:32:46 -0800 469) } e675b31 (Philippe Hausler 2022-02-17 09:32:46 -0800 470) } ```
1 parent bec4ebd commit 451f309

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

stdlib/public/Platform/Platform.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ extension timespec {
334334
@available(SwiftStdlib 5.7, *)
335335
public init(_ duration: Duration) {
336336
let comps = duration.components
337-
self.init(tv_sec: Int(comps.seconds),
337+
self.init(tv_sec: time_t(comps.seconds),
338338
tv_nsec: Int(comps.attoseconds / 1_000_000_000))
339339
}
340340
}
@@ -352,9 +352,19 @@ extension timeval {
352352
@available(SwiftStdlib 5.7, *)
353353
public init(_ duration: Duration) {
354354
let comps = duration.components
355-
// Linux platforms define timeval as Int/Int
356-
self.init(tv_sec: Int(comps.seconds),
357-
tv_usec: Int(comps.attoseconds / 1_000_000_000_000))
355+
#if os(Linux)
356+
// Linux platforms define timeval as Int/Int, except on 32-bit platforms
357+
// where _TIME_BITS=64 is defined. Abuse time_t as an alias for the correct
358+
// suseconds_t type, as it is not an alias to the 64-bit type on 32-bit
359+
// platforms.
360+
typealias _Seconds = time_t
361+
typealias _Microseconds = time_t
362+
#else
363+
typealias _Seconds = Int
364+
typealias _Microseconds = Int // note: was Int32 in release/6.1
365+
#endif
366+
self.init(tv_sec: _Seconds(comps.seconds),
367+
tv_usec: _Microseconds(comps.attoseconds / 1_000_000_000_000))
358368
}
359369
}
360370

0 commit comments

Comments
 (0)