Skip to content

Commit aa055ab

Browse files
ashvindeodharYongli Chen
authored andcommitted
Change the implementation of Get boot time (#304)
1 parent 67debca commit aa055ab

File tree

1 file changed

+15
-45
lines changed

1 file changed

+15
-45
lines changed

platform/os_windows.go

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"bytes"
88
"fmt"
99
"os/exec"
10-
"strconv"
1110
"strings"
1211
"time"
1312

@@ -44,57 +43,28 @@ func GetOSInfo() string {
4443

4544
// GetLastRebootTime returns the last time the system rebooted.
4645
func GetLastRebootTime() (time.Time, error) {
47-
var systemBootTime string
48-
out, err := exec.Command("cmd", "/c", "systeminfo").Output()
46+
out, err := exec.Command("cmd", "/c", "wmic os get lastbootuptime").Output()
4947
if err != nil {
50-
log.Printf("Failed to query systeminfo, err: %v", err)
48+
log.Printf("Failed to query wmic os get lastbootuptime, err: %v", err)
5149
return time.Time{}.UTC(), err
5250
}
5351

54-
systemInfo := strings.Split(string(out), "\n")
55-
for _, systemProperty := range systemInfo {
56-
if strings.Contains(systemProperty, "Boot Time") {
57-
systemBootTime = strings.TrimSpace(strings.Split(systemProperty, "System Boot Time:")[1])
58-
}
52+
lastBootupTime := strings.Split(strings.TrimSpace(string(out)), "\n")
53+
if strings.TrimSpace(lastBootupTime[0]) != "LastBootUpTime" || len(lastBootupTime) != 2 {
54+
log.Printf("Failed to retrieve boot time")
55+
return time.Time{}.UTC(), fmt.Errorf("Failed to retrieve boot time with 'wmic os get lastbootuptime'")
5956
}
57+
systemBootupTime := strings.Split(lastBootupTime[1], ".")[0]
6058

61-
if len(strings.TrimSpace(systemBootTime)) == 0 {
62-
log.Printf("Failed to retrieve boot time from systeminfo")
63-
return time.Time{}.UTC(), fmt.Errorf("Failed to retrieve boot time from systeminfo")
64-
}
65-
66-
log.Printf("Boot time: %s", systemBootTime)
67-
// The System Boot Time is in the following format "01/02/2006, 03:04:05 PM"
68-
// Formulate the Boot Time in the format: "2006-01-02 15:04:05"
69-
bootDate := strings.Split(systemBootTime, " ")[0]
70-
bootTime := strings.Split(systemBootTime, " ")[1]
71-
bootPM := strings.Contains(strings.Split(systemBootTime, " ")[2], "PM")
72-
73-
month := strings.Split(bootDate, "/")[0]
74-
if len(month) < 2 {
75-
month = "0" + month
76-
}
77-
78-
day := strings.Split(bootDate, "/")[1]
79-
if len(day) < 2 {
80-
day = "0" + day
81-
}
82-
83-
year := strings.Split(bootDate, "/")[2]
84-
year = strings.Trim(year, ",")
85-
hour := strings.Split(bootTime, ":")[0]
86-
hourInt, _ := strconv.Atoi(hour)
87-
min := strings.Split(bootTime, ":")[1]
88-
sec := strings.Split(bootTime, ":")[2]
89-
90-
if bootPM && hourInt < 12 {
91-
hourInt += 12
92-
} else if !bootPM && hourInt == 12 {
93-
hourInt = 0
94-
}
59+
// The systembootuptime is in the format YYYYMMDDHHMMSS
60+
bootYear := systemBootupTime[0:4]
61+
bootMonth := systemBootupTime[4:6]
62+
bootDay := systemBootupTime[6:8]
63+
bootHour := systemBootupTime[8:10]
64+
bootMin := systemBootupTime[10:12]
65+
bootSec := systemBootupTime[12:14]
66+
systemBootTime := bootYear + "-" + bootMonth + "-" + bootDay + " " + bootHour + ":" + bootMin + ":" + bootSec
9567

96-
hour = strconv.Itoa(hourInt)
97-
systemBootTime = year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec
9868
log.Printf("Formatted Boot time: %s", systemBootTime)
9969

10070
// Parse the boot time.

0 commit comments

Comments
 (0)