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
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.