File: filetime.go

package info (click to toggle)
golang-github-bodgit-windows 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 96 kB
  • sloc: makefile: 2
file content (44 lines) | stat: -rw-r--r-- 1,471 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Package windows is a collection of types native to Windows platforms but
// are useful on non-Windows platforms.
package windows

// Taken from golang.org/x/sys/windows

const offset int64 = 116444736000000000

// Filetime mirrors the Windows FILETIME structure which represents time
// as the number of 100-nanosecond intervals that have elapsed since
// 00:00:00 UTC, January 1, 1601. This code is taken from the
// golang.org/x/sys/windows package where it's not available for non-Windows
// platforms however various file formats and protocols pass this structure
// about so it's useful to have it available for interoperability purposes.
type Filetime struct {
	LowDateTime  uint32
	HighDateTime uint32
}

// Nanoseconds returns Filetime ft in nanoseconds
// since Epoch (00:00:00 UTC, January 1, 1970).
func (ft *Filetime) Nanoseconds() int64 {
	// 100-nanosecond intervals since January 1, 1601
	nsec := int64(ft.HighDateTime)<<32 + int64(ft.LowDateTime)
	// change starting time to the Epoch (00:00:00 UTC, January 1, 1970)
	nsec -= offset
	// convert into nanoseconds
	nsec *= 100

	return nsec
}

// NsecToFiletime converts nanoseconds to the equivalent Filetime type.
func NsecToFiletime(nsec int64) (ft Filetime) {
	// convert into 100-nanosecond
	nsec /= 100
	// change starting time to January 1, 1601
	nsec += offset
	// split into high / low
	ft.LowDateTime = uint32(nsec & 0xffffffff)
	ft.HighDateTime = uint32(nsec >> 32 & 0xffffffff)

	return ft
}