|
7 | 7 | "bytes" |
8 | 8 | "fmt" |
9 | 9 | "os/exec" |
10 | | - "strconv" |
11 | 10 | "strings" |
12 | 11 | "time" |
13 | 12 |
|
@@ -44,57 +43,28 @@ func GetOSInfo() string { |
44 | 43 |
|
45 | 44 | // GetLastRebootTime returns the last time the system rebooted. |
46 | 45 | 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() |
49 | 47 | 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) |
51 | 49 | return time.Time{}.UTC(), err |
52 | 50 | } |
53 | 51 |
|
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'") |
59 | 56 | } |
| 57 | + systemBootupTime := strings.Split(lastBootupTime[1], ".")[0] |
60 | 58 |
|
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 |
95 | 67 |
|
96 | | - hour = strconv.Itoa(hourInt) |
97 | | - systemBootTime = year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec |
98 | 68 | log.Printf("Formatted Boot time: %s", systemBootTime) |
99 | 69 |
|
100 | 70 | // Parse the boot time. |
|
0 commit comments