filetime.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Package mstypes implements representations of Microsoft types
  2. package mstypes
  3. import (
  4. "time"
  5. )
  6. /*
  7. FILETIME is a windows data structure.
  8. Ref: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724284%28v=vs.85%29.aspx
  9. It contains two parts that are 32bit integers:
  10. dwLowDateTime
  11. dwHighDateTime
  12. We need to combine these two into one 64bit integer.
  13. This gives the number of 100 nano second period from January 1, 1601, Coordinated Universal Time (UTC)
  14. */
  15. const unixEpochDiff = 116444736000000000
  16. // FileTime implements the Microsoft FILETIME type https://msdn.microsoft.com/en-us/library/cc230324.aspx
  17. type FileTime struct {
  18. LowDateTime uint32
  19. HighDateTime uint32
  20. }
  21. // Time return a golang Time type from the FileTime
  22. func (ft FileTime) Time() time.Time {
  23. ns := (ft.MSEpoch() - unixEpochDiff) * 100
  24. return time.Unix(0, int64(ns)).UTC()
  25. }
  26. // MSEpoch returns the FileTime as a Microsoft epoch, the number of 100 nano second periods elapsed from January 1, 1601 UTC.
  27. func (ft FileTime) MSEpoch() int64 {
  28. return (int64(ft.HighDateTime) << 32) + int64(ft.LowDateTime)
  29. }
  30. // Unix returns the FileTime as a Unix time, the number of seconds elapsed since January 1, 1970 UTC.
  31. func (ft FileTime) Unix() int64 {
  32. return (ft.MSEpoch() - unixEpochDiff) / 10000000
  33. }
  34. // GetFileTime returns a FileTime type from the provided Golang Time type.
  35. func GetFileTime(t time.Time) FileTime {
  36. ns := t.UnixNano()
  37. fp := (ns / 100) + unixEpochDiff
  38. hd := fp >> 32
  39. ld := fp - (hd << 32)
  40. return FileTime{
  41. LowDateTime: uint32(ld),
  42. HighDateTime: uint32(hd),
  43. }
  44. }