StackExchange / wmi

WMI for Go
http://godoc.org/github.com/StackExchange/wmi
MIT License
433 stars 173 forks source link

[Helper] WMI Date to datetime conversion #54

Closed yusufozturk closed 3 years ago

yusufozturk commented 3 years ago

There are some WMI properties like "Win32_PnPSignedDriver.DriverDate" or "Win32_OperatingSystem.InstallDate" and they are in WMI Date format.

Library detects them as string value. So if you need to convert it to golang date time, you can use following conversion:

func wmiDateToDate(s string) (time.Time, bool) {
    // yyyymmddhhnnss.zzzzzzsUUU  +60 means 60 mins of UTC time
    // 20030709091030.686000+060
    // 1234567890123456789012345

    // Check if value is WMI date
    if len(s) != 25 {
        return time.Time{}, false
    }

    // Check if value is WMI date
    wmiDate := strings.Split(s, ".")
    if len(wmiDate) != 2 {
        return time.Time{}, false
    }

    // Process Full Date
    layout := "20060102150405"
    t, err := time.Parse(layout, wmiDate[0])
    if err != nil {
        return time.Time{}, false
    }

    return t, true
}

I will close this issue so if someone searches it in the future, they can use the function.