Go:os.Stat的ModTime显示错误的值

huangapple go评论67阅读模式
英文:

Go: os.Stat ModTime shows incorrect value

问题

我正在尝试在不同操作系统上一致地获取文件的修改时间。
我创建了一个文件,然后使用info := os.Stat(file)info.ModTime()

在Windows上,结果看起来正常 - 文件的修改时间等于当前时间。
在Linux上,看起来文件是在过去创建的(确切地说是12秒前)。

我应该提到我的Linux是一个虚拟机,使用这个vagrant box

下面是完整的脚本及其结果。

package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"time"
)

func main() {
	fmt.Println("Started   :", time.Now())
	ioutil.WriteFile("somefile.txt", []byte("hello"), 0666)
	fi, _ := os.Stat("somefile.txt")
	fmt.Println("ModTime   :", fi.ModTime())
	fmt.Println("Now       :", time.Now())
	fmt.Println("Time Since:", time.Since(fi.ModTime()).Seconds())
}

Windows结果(如预期)

Started   : 2014-10-16 16:15:54.3861206 +0300 IDT
ModTime   : 2014-10-16 16:15:54.3880908 +0300 IDT
Now       : 2014-10-16 16:15:54.3880908 +0300 IDT
Time Since: 0

Linux结果(比当前时间早12秒)

Started   : 2014-10-16 13:15:23.511700545 +0000 UTC
ModTime   : 2014-10-16 13:15:11.1473256 +0000 UTC
Now       : 2014-10-16 13:15:23.514631479 +0000 UTC
Time Since: 12.367389917

如果有任何关于我遗漏了什么的帮助,将会很棒。

编辑:
在Travis上执行无法重现这个错误。

英文:

I am trying to get the file modification time consistently across operating systems.
I am creating a file, then using info := os.Stat(file) and then info.ModTime().

On Windows, this looks as expected - file modification time equals now.
On Linux, it looks like the file was created in the past (12 seconds in the past to be exact).

I should mention my Linux is a virtual machine, using this vagrant box.

The full script and its results are below.

package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"time"
)

func main() {
	fmt.Println("Started   :", time.Now())
	ioutil.WriteFile("somefile.txt", []byte("hello"), 0666)
	fi, _ := os.Stat("somefile.txt")
	fmt.Println("ModTime   :", fi.ModTime())
	fmt.Println("Now       :", time.Now())
	fmt.Println("Time Since:", time.Since(fi.ModTime()).Seconds())
}

Windows Results (as expected)

Started   : 2014-10-16 16:15:54.3861206 +0300 IDT
ModTime   : 2014-10-16 16:15:54.3880908 +0300 IDT
Now       : 2014-10-16 16:15:54.3880908 +0300 IDT
Time Since: 0

Linux Results (12 seconds in the past)

Started   : 2014-10-16 13:15:23.511700545 +0000 UTC
ModTime   : 2014-10-16 13:15:11.1473256 +0000 UTC
Now       : 2014-10-16 13:15:23.514631479 +0000 UTC
Time Since: 12.367389917

Any assistance as to what I am missing, would be great.

EDIT:
Execution on Travis does NOT reproduce the error

答案1

得分: 1

虚拟机因为没有运行 NTP 客户端来同步时间,所以它们的时钟常常会失去同步。

如果你的工作目录是在你的实际主机上,并且通过 Vagrant 挂载到虚拟机上(在开发时通常是这样的),那么主机文件系统将确定修改时间。如果虚拟机和主机的时钟相差足够多,你将观察到正是这种行为。

在主机和虚拟机系统上同时运行 date 命令,以确定时钟相差多少。

英文:

Virtual machines are notorious for having their clocks get out of sync if you aren't running an NTP client to sync it.

If your working directory is local to your actual host machine and mounted by Vagrant through to the VM (which is typical when doing development), then the host file system will determine the modified time. If the VM and your host clocks have drifted apart enough, you will observe exactly this behavior.

Run date on both the host and VM systems at the exact same time to figure out how far apart the clocks are.

huangapple
  • 本文由 发表于 2014年10月16日 21:25:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/26405645.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定