-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d431e83
commit 0ea2687
Showing
4 changed files
with
101 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//go:build !(windows || linux || darwin) | ||
|
||
package ntp | ||
|
||
import ( | ||
"os" | ||
"time" | ||
) | ||
|
||
func SetSystemTime(nowTime time.Time) error { | ||
return os.ErrInvalid | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//go:build linux || darwin | ||
|
||
package ntp | ||
|
||
import ( | ||
"time" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func SetSystemTime(nowTime time.Time) error { | ||
timeVal := unix.NsecToTimeval(nowTime.UnixNano()) | ||
return unix.Settimeofday(&timeVal) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package ntp | ||
|
||
import ( | ||
"time" | ||
"unsafe" | ||
|
||
"golang.org/x/sys/windows" | ||
) | ||
|
||
func SetSystemTime(nowTime time.Time) error { | ||
var systemTime windows.Systemtime | ||
systemTime.Year = uint16(nowTime.Year()) | ||
systemTime.Month = uint16(nowTime.Month()) | ||
systemTime.Day = uint16(nowTime.Day()) | ||
systemTime.Hour = uint16(nowTime.Hour()) | ||
systemTime.Minute = uint16(nowTime.Minute()) | ||
systemTime.Second = uint16(nowTime.Second()) | ||
systemTime.Milliseconds = uint16(nowTime.UnixMilli() - nowTime.Unix()*1000) | ||
|
||
dllKernel32 := windows.NewLazySystemDLL("kernel32.dll") | ||
proc := dllKernel32.NewProc("SetSystemTime") | ||
|
||
_, _, err := proc.Call( | ||
uintptr(unsafe.Pointer(&systemTime)), | ||
) | ||
|
||
if err != nil && err.Error() != "The operation completed successfully." { | ||
return err | ||
} | ||
|
||
return nil | ||
} |