Skip to content

Commit 7f70ae0

Browse files
committed
Expose C timespec properties until UTCClock can be used
1 parent 90bb24e commit 7f70ae0

File tree

2 files changed

+150
-105
lines changed

2 files changed

+150
-105
lines changed

Sources/System/FileSystem/Stat.swift

Lines changed: 81 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -410,119 +410,146 @@ public struct Stat: RawRepresentable, Sendable {
410410
512 * blocksAllocated
411411
}
412412

413-
// TODO: jflat - Change time properties to UTCClock.Instant when possible.
414-
415-
/// Time of last access, given as a `Duration` since the Epoch
413+
/// Time of last access, given as a C `timespec` since the Epoch.
416414
///
417415
/// The corresponding C property is `st_atim` (or `st_atimespec` on Darwin).
418-
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
419-
public var accessTime: Duration {
416+
@_alwaysEmitIntoClient
417+
public var st_atim: timespec {
420418
get {
421419
#if SYSTEM_PACKAGE_DARWIN
422-
let timespec = rawValue.st_atimespec
420+
rawValue.st_atimespec
423421
#else
424-
let timespec = rawValue.st_atim
422+
rawValue.st_atim
425423
#endif
426-
return .seconds(timespec.tv_sec) + .nanoseconds(timespec.tv_nsec)
427424
}
428425
set {
429-
let (seconds, attoseconds) = newValue.components
430-
let timespec = timespec(
431-
tv_sec: numericCast(seconds),
432-
tv_nsec: numericCast(attoseconds / 1_000_000_000)
433-
)
434426
#if SYSTEM_PACKAGE_DARWIN
435-
rawValue.st_atimespec = timespec
427+
rawValue.st_atimespec = newValue
436428
#else
437-
rawValue.st_atim = timespec
429+
rawValue.st_atim = newValue
438430
#endif
439431
}
440432
}
441433

442-
/// Time of last modification, given as a `Duration` since the Epoch
434+
/// Time of last modification, given as a C `timespec` since the Epoch.
443435
///
444436
/// The corresponding C property is `st_mtim` (or `st_mtimespec` on Darwin).
445-
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
446-
public var modificationTime: Duration {
437+
@_alwaysEmitIntoClient
438+
public var st_mtim: timespec {
447439
get {
448440
#if SYSTEM_PACKAGE_DARWIN
449-
let timespec = rawValue.st_mtimespec
441+
rawValue.st_mtimespec
450442
#else
451-
let timespec = rawValue.st_mtim
443+
rawValue.st_mtim
452444
#endif
453-
return .seconds(timespec.tv_sec) + .nanoseconds(timespec.tv_nsec)
454445
}
455446
set {
456-
let (seconds, attoseconds) = newValue.components
457-
let timespec = timespec(
458-
tv_sec: numericCast(seconds),
459-
tv_nsec: numericCast(attoseconds / 1_000_000_000)
460-
)
461447
#if SYSTEM_PACKAGE_DARWIN
462-
rawValue.st_mtimespec = timespec
448+
rawValue.st_mtimespec = newValue
463449
#else
464-
rawValue.st_mtim = timespec
450+
rawValue.st_mtim = newValue
465451
#endif
466452
}
467453
}
468454

469-
/// Time of last status (inode) change, given as a `Duration` since the Epoch
455+
/// Time of last status (inode) change, given as a C `timespec` since the Epoch.
470456
///
471457
/// The corresponding C property is `st_ctim` (or `st_ctimespec` on Darwin).
472-
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
473-
public var changeTime: Duration {
458+
@_alwaysEmitIntoClient
459+
public var st_ctim: timespec {
474460
get {
475461
#if SYSTEM_PACKAGE_DARWIN
476-
let timespec = rawValue.st_ctimespec
462+
rawValue.st_ctimespec
477463
#else
478-
let timespec = rawValue.st_ctim
464+
rawValue.st_ctim
479465
#endif
480-
return .seconds(timespec.tv_sec) + .nanoseconds(timespec.tv_nsec)
481466
}
482467
set {
483-
let (seconds, attoseconds) = newValue.components
484-
let timespec = timespec(
485-
tv_sec: numericCast(seconds),
486-
tv_nsec: numericCast(attoseconds / 1_000_000_000)
487-
)
488468
#if SYSTEM_PACKAGE_DARWIN
489-
rawValue.st_ctimespec = timespec
469+
rawValue.st_ctimespec = newValue
490470
#else
491-
rawValue.st_ctim = timespec
471+
rawValue.st_ctim = newValue
492472
#endif
493473
}
494474
}
495475

496476
#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD)
497-
/// Time of file creation, given as a `Duration` since the Epoch
477+
/// Time of file creation, given as a C `timespec` since the Epoch.
498478
///
499479
/// The corresponding C property is `st_birthtim` (or `st_birthtimespec` on Darwin).
500480
/// - Note: Only available on Darwin and FreeBSD.
501-
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
502-
public var creationTime: Duration {
481+
@_alwaysEmitIntoClient
482+
public var st_birthtim: timespec {
503483
get {
504484
#if SYSTEM_PACKAGE_DARWIN
505-
let timespec = rawValue.st_birthtimespec
485+
rawValue.st_birthtimespec
506486
#else
507-
let timespec = rawValue.st_birthtim
487+
rawValue.st_birthtim
508488
#endif
509-
return .seconds(timespec.tv_sec) + .nanoseconds(timespec.tv_nsec)
510489
}
511490
set {
512-
let (seconds, attoseconds) = newValue.components
513-
let timespec = timespec(
514-
tv_sec: numericCast(seconds),
515-
tv_nsec: numericCast(attoseconds / 1_000_000_000)
516-
)
517491
#if SYSTEM_PACKAGE_DARWIN
518-
rawValue.st_birthtimespec = timespec
492+
rawValue.st_birthtimespec = newValue
519493
#else
520-
rawValue.st_birthtim = timespec
494+
rawValue.st_birthtim = newValue
521495
#endif
522496
}
523497
}
524498
#endif
525499

500+
// TODO: jflat - Change time properties to UTCClock.Instant when possible.
501+
502+
// /// Time of last access, given as a `UTCClock.Instant`
503+
// ///
504+
// /// The corresponding C property is `st_atim` (or `st_atimespec` on Darwin).
505+
// public var accessTime: UTCClock.Instant {
506+
// get {
507+
// UTCClock.systemEpoch.advanced(by: Duration(st_atim))
508+
// }
509+
// set {
510+
// st_atim = timespec(UTCClock.systemEpoch.duration(to: newValue))
511+
// }
512+
// }
513+
//
514+
// /// Time of last modification, given as a `UTCClock.Instant`
515+
// ///
516+
// /// The corresponding C property is `st_mtim` (or `st_mtimespec` on Darwin).
517+
// public var modificationTime: UTCClock.Instant {
518+
// get {
519+
// UTCClock.systemEpoch.advanced(by: Duration(st_mtim))
520+
// }
521+
// set {
522+
// st_mtim = timespec(UTCClock.systemEpoch.duration(to: newValue))
523+
// }
524+
// }
525+
//
526+
// /// Time of last status (inode) change, given as a `UTCClock.Instant`
527+
// ///
528+
// /// The corresponding C property is `st_ctim` (or `st_ctimespec` on Darwin).
529+
// public var changeTime: UTCClock.Instant {
530+
// get {
531+
// UTCClock.systemEpoch.advanced(by: Duration(st_ctim))
532+
// }
533+
// set {
534+
// st_ctim = timespec(UTCClock.systemEpoch.duration(to: newValue))
535+
// }
536+
// }
537+
//
538+
// #if SYSTEM_PACKAGE_DARWIN || os(FreeBSD)
539+
// /// Time of file creation, given as a `UTCClock.Instant`
540+
// ///
541+
// /// The corresponding C property is `st_birthtim` (or `st_birthtimespec` on Darwin).
542+
// /// - Note: Only available on Darwin and FreeBSD.
543+
// public var creationTime: UTCClock.Instant {
544+
// get {
545+
// UTCClock.systemEpoch.advanced(by: Duration(st_birthtim))
546+
// }
547+
// set {
548+
// st_birthtim = timespec(UTCClock.systemEpoch.duration(to: newValue))
549+
// }
550+
// }
551+
// #endif
552+
526553
#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD) || os(OpenBSD)
527554
/// File flags
528555
///

0 commit comments

Comments
 (0)