golang 調(diào)用win32api 對(duì)windows系統(tǒng)時(shí)間進(jìn)行調(diào)用,主要參考的是微軟的win32api文檔,根據(jù)官方文檔,有兩者設(shè)置方式:
setlocaltime:設(shè)置當(dāng)前的本地時(shí)間和日期。https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-setlocaltime
setsystemtime:設(shè)置當(dāng)前系統(tǒng)時(shí)間和日期。系統(tǒng)時(shí)間以協(xié)調(diào)世界時(shí) (UTC) 表示https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-setsystemtime
// 根據(jù)官方文檔中的描述,setlocaltime 需要傳一個(gè)輸入?yún)?shù),該參數(shù)是一個(gè)名為SYSTEMTIME的指針結(jié)構(gòu)體
BOOL SetLocalTime(
[in] const SYSTEMTIME *lpSystemTime
);
// SYSTEMTIME 定義:
typedef struct _SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;
// word 類型在golang 里面可以看作是 uint16
// SetLocalTime的返回值,如果設(shè)置成功,則返回值不是0,反之則為0
golang 調(diào)用win32api的方式 是通過?syscall 庫去調(diào)用相應(yīng)的 windows .dll 文件,根據(jù)上述文檔描述,兩個(gè)接口都是存在于?Kernel32.dll 這個(gè)文件當(dāng)中,因此我們可以使用如下代碼去實(shí)現(xiàn)調(diào)用接口:
dll2 := syscall.NewLazyDLL("kernel32.dll")
proc := dll2.NewProc("SetLocalTime")
具體實(shí)現(xiàn)方式如下:?
// 定義對(duì)應(yīng)的結(jié)構(gòu)體 type SYSTEMTIME struct { WYear uint16 WMonth uint16 WDayOfWeek uint16 WDay uint16 WHour uint16 WMinute uint16 WSecond uint16 WMilliseconds uint16 } // 設(shè)置本地系統(tǒng)時(shí)間 func SetLocalTime(year, month, day, hour, minute, seconds uint16) error { s := SYSTEMTIME{ WYear: year, WMonth: month, WDay: day, WHour: hour, WMinute: minute, WSecond: seconds, } fmt.Println("s", s) dll2 := syscall.NewLazyDLL("kernel32.dll") proc := dll2.NewProc("SetLocalTime") // 調(diào)用接口,傳參 r, _, err := proc.Call(uintptr(unsafe.Pointer(&s))) if r <= 0 { return err } else { return nil } }
最后執(zhí)行的話,需要在使用管理員權(quán)限在cmd 中執(zhí)行才會(huì)成功.文章來源:http://www.zghlxwxcb.cn/news/detail-770633.html
下面是關(guān)于輸入時(shí)間格式的一個(gè)校驗(yàn),因?yàn)槭褂?SetLocalTime,對(duì)于輸入時(shí)間系統(tǒng)不會(huì)做過多校驗(yàn).我代碼主要框架是gin.就不做過多修改了文章來源地址http://www.zghlxwxcb.cn/news/detail-770633.html
func setTime(c *gin.Context) {
var t struct {
Year uint16 `json:"year"`
Month uint16 `json:"month"`
Day uint16 `json:"day"`
Hour uint16 `json:"hour"`
Minute uint16 `json:"minute"`
Seconds uint16 `json:"seconds"`
}
err := c.Bind(&t)
if err != nil {
c.JSON(200, utils.Returns(utils.ParamErr, err.Error(), c, nil, nil))
return
}
msg := []string{}
monthMap := map[uint16]string{
1: "Jan", 2: "Feb", 3: "Mar", 4: "Apr", 5: "May", 6: "Jun", 7: "Jul",
8: "Aug", 9: "Sep", 10: "Oct", 11: "Nov", 12: "Dec",
}
if t.Year < 1601 || t.Year > 30827 {
msg = append(msg, "year range is from 1601 to 30827")
}
switch t.Month {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if t.Day < 1 || t.Day > 31 {
msg = append(msg, fmt.Sprintf("%d/%s of days range is from 1 to 31", t.Year, monthMap[t.Month]))
}
case 2:
if t.Year%4 == 0 && t.Year%100 != 0 || t.Year%400 == 0 { // 閏年
if t.Day < 1 || t.Day > 29 {
msg = append(msg, fmt.Sprintf("%d/%s of days range is from 1 to 29", t.Year, monthMap[t.Month]))
}
} else { // 平年
if t.Day < 1 || t.Day > 28 {
msg = append(msg, fmt.Sprintf("%d/%s of days range is from 1 to 28", t.Year, monthMap[t.Month]))
}
}
case 4:
case 6:
case 9:
case 11:
if t.Day < 1 || t.Day > 30 {
msg = append(msg, fmt.Sprintf("%d/%s of days is ranges 1-30", t.Year, monthMap[t.Month]))
}
default:
msg = append(msg, "month range is from 1 to 12")
}
if t.Hour < 0 || t.Hour > 59 {
msg = append(msg, "hour range is from 0 to 59")
}
if t.Minute < 0 || t.Minute > 59 {
msg = append(msg, "minute range is from 0 to 59")
}
if t.Seconds < 0 || t.Seconds > 59 {
msg = append(msg, "seconds range is from 0 to 59")
}
if len(msg) > 0 {
c.JSON(200, utils.Returns(utils.TimeRangeErr, strings.Join(msg, ","), c, nil, nil))
return
}
err = utils.SetLocalTime(t.Year, t.Month, t.Day, t.Hour, t.Minute, t.Seconds)
if err != nil {
c.JSON(200, utils.Returns(utils.UpdateErr, err.Error(), c, nil, nil))
return
}
c.JSON(200, utils.Returns(utils.Succ, "", c, nil, nil))
}
到了這里,關(guān)于golang-Windows 設(shè)置系統(tǒng)本地時(shí)間的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!