Skip to content

time: Now() faster than reported timestamps from filesystem #33510

@taoyuanyuan

Description

@taoyuanyuan

What version of Go are you using (go version)?

$ go version go1.12.6 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
linux 3.10.0
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/lieyuan/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/lieyuan/gogo"
GOPROXY=""
GORACE=""
GOROOT="/home/lieyuan/go"
GOTMPDIR=""
GOTOOLDIR="/home/lieyuan/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build567923190=/tmp/go-build -gno-record-gcc-switches"

What did you do?

time.Now() faster than current system time

package main

import (
    "fmt"
    "os"
    "time"
)

func main() {
    for {
        time.Sleep(900*time.Millisecond)
        os.Remove("/tmp/1")
        var now time.Time
        for {
            now = time.Now()
            if now.Nanosecond() == 0 {
                break
            }
        }
        file, _ := os.OpenFile("/tmp/1", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
        stat, _ := file.Stat()
        if now.Second() > stat.ModTime().Second() {
            fmt.Println(now.Unix(), stat.ModTime().Unix(), time.Now().Unix())
            return
        }
    }
}

output:

1565151693 1565151692 1565151693

stat file:

$ stat /tmp/1
  File: ‘/tmp/1’
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 803h/2051d	Inode: 1703974     Links: 1
Access: (0644/-rw-r--r--)  Uid: (50051/ lieyuan)   Gid: (  100/   users)
Access: 2019-08-07 12:21:32.998566664 +0800
Modify: 2019-08-07 12:21:32.998566664 +0800
Change: 2019-08-07 12:21:32.998566664 +0800
 Birth: -
$ date -d @1565151693
Wed Aug  7 12:21:33 CST 2019

What did you expect to see?

time.Now() returns the current system time

What did you see instead?

time.Now() faster than current system time

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions