Make FileTime work portably.
This commit is contained in:
		
							parent
							
								
									0bc7acbc68
								
							
						
					
					
						commit
						fe557e2202
					
				| 
						 | 
					@ -0,0 +1,37 @@
 | 
				
			||||||
 | 
					// +build freebsd darwin netbsd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					package lib
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"golang.org/x/sys/unix"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// FileTime contains the changed, modified, and accessed timestamps
 | 
				
			||||||
 | 
					// for a file.
 | 
				
			||||||
 | 
					type FileTime struct {
 | 
				
			||||||
 | 
						Changed  time.Time
 | 
				
			||||||
 | 
						Modified time.Time
 | 
				
			||||||
 | 
						Accessed time.Time
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func timeSpecToTime(ts unix.Timespec) time.Time {
 | 
				
			||||||
 | 
						return time.Unix(ts.Sec, ts.Nsec)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// LoadFileTime returns a FileTime associated with the file.
 | 
				
			||||||
 | 
					func LoadFileTime(path string) (FileTime, error) {
 | 
				
			||||||
 | 
						var ft = FileTime{}
 | 
				
			||||||
 | 
						var st = unix.Stat_t{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						err := unix.Stat(path, &st)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return ft, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ft.Changed = timeSpecToTime(st.Ctimespec)
 | 
				
			||||||
 | 
						ft.Modified = timeSpecToTime(st.Mtimespec)
 | 
				
			||||||
 | 
						ft.Accessed = timeSpecToTime(st.Atimespec)
 | 
				
			||||||
 | 
						return ft, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,37 @@
 | 
				
			||||||
 | 
					// +build unix linux openbsd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					package lib
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"golang.org/x/sys/unix"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// FileTime contains the changed, modified, and accessed timestamps
 | 
				
			||||||
 | 
					// for a file.
 | 
				
			||||||
 | 
					type FileTime struct {
 | 
				
			||||||
 | 
						Changed  time.Time
 | 
				
			||||||
 | 
						Modified time.Time
 | 
				
			||||||
 | 
						Accessed time.Time
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func timeSpecToTime(ts unix.Timespec) time.Time {
 | 
				
			||||||
 | 
						return time.Unix(ts.Sec, ts.Nsec)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// LoadFileTime returns a FileTime associated with the file.
 | 
				
			||||||
 | 
					func LoadFileTime(path string) (FileTime, error) {
 | 
				
			||||||
 | 
						var ft = FileTime{}
 | 
				
			||||||
 | 
						var st = unix.Stat_t{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						err := unix.Stat(path, &st)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return ft, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ft.Changed = timeSpecToTime(st.Ctim)
 | 
				
			||||||
 | 
						ft.Modified = timeSpecToTime(st.Mtim)
 | 
				
			||||||
 | 
						ft.Accessed = timeSpecToTime(st.Atim)
 | 
				
			||||||
 | 
						return ft, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										30
									
								
								lib/lib.go
								
								
								
								
							
							
						
						
									
										30
									
								
								lib/lib.go
								
								
								
								
							| 
						 | 
					@ -6,8 +6,6 @@ import (
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"path/filepath"
 | 
						"path/filepath"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					 | 
				
			||||||
	"golang.org/x/sys/unix"
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var progname = filepath.Base(os.Args[0])
 | 
					var progname = filepath.Base(os.Args[0])
 | 
				
			||||||
| 
						 | 
					@ -113,31 +111,3 @@ func Duration(d time.Duration) string {
 | 
				
			||||||
	s += fmt.Sprintf("%dh%s", hours, d)
 | 
						s += fmt.Sprintf("%dh%s", hours, d)
 | 
				
			||||||
	return s
 | 
						return s
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
// FileTime contains the changed, modified, and accessed timestamps
 | 
					 | 
				
			||||||
// for a file.
 | 
					 | 
				
			||||||
type FileTime struct {
 | 
					 | 
				
			||||||
	Changed  time.Time
 | 
					 | 
				
			||||||
	Modified time.Time
 | 
					 | 
				
			||||||
	Accessed time.Time
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func timeSpecToTime(ts unix.Timespec) time.Time {
 | 
					 | 
				
			||||||
	return time.Unix(ts.Sec, ts.Nsec)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// LoadFileTime returns a FileTime associated with the file.
 | 
					 | 
				
			||||||
func LoadFileTime(path string) (FileTime, error) {
 | 
					 | 
				
			||||||
	var ft = FileTime{}
 | 
					 | 
				
			||||||
	var st = unix.Stat_t{}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	err := unix.Stat(path, &st)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return ft, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	ft.Changed = timeSpecToTime(st.Ctim)
 | 
					 | 
				
			||||||
	ft.Modified = timeSpecToTime(st.Mtim)
 | 
					 | 
				
			||||||
	ft.Accessed = timeSpecToTime(st.Atim)
 | 
					 | 
				
			||||||
	return ft, nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue