浏览代码

pkg/system: add note about maxTime

This code caused me some head-scratches, and initially I wondered
if this was a bug, but it looks to be intentional to set nsec, not
sec, as time.Unix() internally divides nsec, and sets sec accordingly;
https://github.com/golang/go/blob/go1.19.2/src/time/time.go#L1364-L1380

    // Unix returns the local Time corresponding to the given Unix time,
    // sec seconds and nsec nanoseconds since January 1, 1970 UTC.
    // It is valid to pass nsec outside the range [0, 999999999].
    // Not all sec values have a corresponding time value. One such
    // value is 1<<63-1 (the largest int64 value).
    func Unix(sec int64, nsec int64) Time {
        if nsec < 0 || nsec >= 1e9 {
            n := nsec / 1e9
            sec += n
            nsec -= n * 1e9
            if nsec < 0 {
                nsec += 1e9
                sec--
            }
        }
        return unixTime(sec, int32(nsec))
    }

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn 3 年之前
父节点
当前提交
2c9684e35c
共有 1 个文件被更改,包括 4 次插入0 次删除
  1. 4 0
      pkg/system/chtimes.go

+ 4 - 0
pkg/system/chtimes.go

@@ -14,6 +14,10 @@ func init() {
 	if unsafe.Sizeof(syscall.Timespec{}.Nsec) == 8 {
 		// This is a 64 bit timespec
 		// os.Chtimes limits time to the following
+		//
+		// Note that this intentionally sets nsec (not sec), which sets both sec
+		// and nsec internally in time.Unix();
+		// https://github.com/golang/go/blob/go1.19.2/src/time/time.go#L1364-L1380
 		maxTime = time.Unix(0, 1<<63-1)
 	} else {
 		// This is a 32 bit timespec