gopkg.in/essentialkaos/ek.v3@v3.5.1/fsutil/fs_time_darwin.go (about)

     1  // +build darwin
     2  
     3  package fsutil
     4  
     5  // ////////////////////////////////////////////////////////////////////////////////// //
     6  //                                                                                    //
     7  //                     Copyright (c) 2009-2016 Essential Kaos                         //
     8  //      Essential Kaos Open Source License <http://essentialkaos.com/ekol?en>         //
     9  //                                                                                    //
    10  // ////////////////////////////////////////////////////////////////////////////////// //
    11  
    12  import (
    13  	"errors"
    14  	"syscall"
    15  	"time"
    16  
    17  	PATH "pkg.re/essentialkaos/ek.v3/path"
    18  )
    19  
    20  // ////////////////////////////////////////////////////////////////////////////////// //
    21  
    22  // GetTimes return time of access, modification and creation at once
    23  func GetTimes(path string) (time.Time, time.Time, time.Time, error) {
    24  	if path == "" {
    25  		return time.Time{}, time.Time{}, time.Time{}, errors.New("Path is empty")
    26  	}
    27  
    28  	path = PATH.Clean(path)
    29  
    30  	var stat = &syscall.Stat_t{}
    31  
    32  	err := syscall.Stat(path, stat)
    33  
    34  	if err != nil {
    35  		return time.Time{}, time.Time{}, time.Time{}, err
    36  	}
    37  
    38  	return time.Unix(int64(stat.Atimespec.Sec), int64(stat.Atimespec.Nsec)),
    39  		time.Unix(int64(stat.Mtimespec.Sec), int64(stat.Mtimespec.Nsec)),
    40  		time.Unix(int64(stat.Ctimespec.Sec), int64(stat.Ctimespec.Nsec)),
    41  		nil
    42  }
    43  
    44  // GetTimestamps return time of access, modification and creation at once as linux timestamp
    45  func GetTimestamps(path string) (int64, int64, int64, error) {
    46  	if path == "" {
    47  		return -1, -1, -1, errors.New("Path is empty")
    48  	}
    49  
    50  	path = PATH.Clean(path)
    51  
    52  	var stat = &syscall.Stat_t{}
    53  
    54  	err := syscall.Stat(path, stat)
    55  
    56  	if err != nil {
    57  		return -1, -1, -1, err
    58  	}
    59  
    60  	return int64(stat.Atimespec.Sec),
    61  		int64(stat.Mtimespec.Sec),
    62  		int64(stat.Ctimespec.Sec),
    63  		nil
    64  }