Debian Codegraph @

mstypes/filetime.go

raw symbols SCIP
1 // Package mstypes implements representations of Microsoft types for PAC processing.
2 package mstypes
3
4 import (
5 "encoding/binary"
6 "time"
7
8 "gopkg.in/jcmturner/rpc.v0/ndr"
9 )
10
11 const unixEpochDiff = 116444736000000000
12
13 // FileTime implements the Microsoft FILETIME type https://msdn.microsoft.com/en-us/library/cc230324.aspx
14 type FileTime struct {
15 LowDateTime uint32
16 HighDateTime uint32
17 }
18
19 // Time return a golang Time type from the FileTime
20 func (ft FileTime) Time() time.Time {
21 ns := (ft.MSEpoch() - unixEpochDiff) * 100
22 return time.Unix(0, int64(ns)).UTC()
23 }
24
25 // MSEpoch returns the FileTime as a Microsoft epoch, the number of 100 nano second periods elapsed from January 1, 1601 UTC.
26 func (ft FileTime) MSEpoch() int64 {
27 return (int64(ft.HighDateTime) << 32) + int64(ft.LowDateTime)
28 }
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
35 // GetFileTime returns a FileTime type from the provided Golang Time type.
36 func GetFileTime(t time.Time) FileTime {
37 ns := t.UnixNano()
38 fp := (ns / 100) + unixEpochDiff
39 hd := fp >> 32
40 ld := fp - (hd << 32)
41 return FileTime{
42 LowDateTime: uint32(ld),
43 HighDateTime: uint32(hd),
44 }
45 }
46
47 // ReadFileTime reads a FileTime from the bytes slice.
48 func ReadFileTime(b *[]byte, p *int, e *binary.ByteOrder) FileTime {
49 l := ndr.ReadUint32(b, p, e)
50 h := ndr.ReadUint32(b, p, e)
51 return FileTime{
52 LowDateTime: l,
53 HighDateTime: h,
54 }
55 }