Skip to content

Commit

Permalink
Add FILETIME.Time() and SYSTEMTIME.Time()
Browse files Browse the repository at this point in the history
Also change FILETIME.ToUint64() to just FILETIME.Uint64() which is more
common in Go.
  • Loading branch information
lars committed Feb 22, 2021
1 parent 8e6db4f commit ef48825
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion types.go
Expand Up @@ -6,6 +6,7 @@ package w32

import (
"syscall"
"time"
"unicode/utf16"
"unsafe"
)
Expand Down Expand Up @@ -649,10 +650,23 @@ type FILETIME struct {
DwHighDateTime uint32
}

func (t FILETIME) ToUint64() uint64 {
func (t FILETIME) Uint64() uint64 {
return uint64(t.DwHighDateTime)<<32 | uint64(t.DwLowDateTime)
}

func (t FILETIME) Time() time.Time {
// https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime
ref := time.Date(1601, time.January, 1, 0, 0, 0, 0, time.UTC)
const tick = 100 * time.Nanosecond
// The FILETIME is a uint64 of 100-nanosecond intervals since 1601.
// Unfortunately time.Duration is really an int64 so if we cast our uint64
// to a time.Duration it becomes negative. Thus we do it in 2 steps, adding
// half the time each step to avoid overflow.
return ref.
Add(time.Duration(t.Uint64()) * (tick / 2)).
Add(time.Duration(t.Uint64()) * (tick / 2))
}

// http://msdn.microsoft.com/en-us/library/windows/desktop/ms682119.aspx
type COORD struct {
X, Y int16
Expand Down Expand Up @@ -828,6 +842,19 @@ type SYSTEMTIME struct {
Milliseconds uint16
}

func (t SYSTEMTIME) Time() time.Time {
return time.Date(
int(t.Year),
time.Month(t.Month),
int(t.Day),
int(t.Hour),
int(t.Minute),
int(t.Second),
int(t.Milliseconds)*int(time.Millisecond/time.Nanosecond),
time.UTC,
)
}

// http://msdn.microsoft.com/en-us/library/windows/desktop/ms644967(v=vs.85).aspx
type KBDLLHOOKSTRUCT struct {
VkCode DWORD
Expand Down

2 comments on commit ef48825

@DoomGuy9K
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please tag one of the recent commits (i.e. with v1.0.1)? That would allow depending repos (i.e. gonutz/wui) to work as a new go module without any trouble. That is because GO111MODULEs and "go mod init" sets all dependency to their latest tag or release which is pretty outdated v1.0.0 (-> building wui fails). This can be fixed by manually setting dependency to a recent tag of w32, but that is unnecessary hassle. You may spare the redundant vendor/... code in gonutz/wui, too, when tagging.

@gonutz
Copy link
Owner

@gonutz gonutz commented on ef48825 Feb 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, sorry for the inconvenience, I will learn up about modules and add support for them in all my active repositories. See my comment on a related issue for a rant :-)

This might however take a couple of days, I feel modules are very complicated. Thus I want to get them right and not have to add and delete tags all over the place. I have a network of inter-connected repos like d3d9, w32, draw, mixer and all ld* games and I want to update them all at once to use modules so there are no surprises for new users and for me in case I forget one.

Please sign in to comment.