Completed
Push — master ( a834e8...53c2a3 )
by Marcio
15s
created

Boottime::convertWmicOffset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
rs 9.4285
1
<?php
2
3
namespace Uptime\Runtime\Windows;
4
5
use Uptime\Runtime\RuntimeInterface;
6
7
class Boottime implements RuntimeInterface
8
{
9
    /**
10
     * Reads the wmic command which returns the last bootup time and converts it to a unix timestamp
11
     *
12
     * @param string $command wmic command
13
     * @return int
14
     */
15
    public function read($command = 'wmic os get lastbootuptime')
16
    {
17
        $wmicString = trim(explode("\n", shell_exec($command))[1]);
18
19
        $dateTime = \DateTime::createFromFormat(
20
            'YmdHis.uO',
21
            $this->convertWmicOffset($wmicString)
22
        );
23
        return $dateTime->getTimestamp();
24
    }
25
26
    /**
27
     * Takes the output of wmic os get lastbootuptime and converts the offset given in minutes to an HHMM offset acceptable by PHP createFromFormat 'O'
28
     *
29
     * @param string $wmicString string output given by wmic command
30
     * @return string
31
     */
32
    private function convertWmicOffset($wmicString)
33
    {
34
        $offset = substr($wmicString, -3);
35
        $hours = floor($offset / 60);
36
        $minutes = ($offset % 60);
37
        return substr($wmicString, 0, -3) . sprintf('%02d%02d', $hours, $minutes);
38
    }
39
}
40