0 | 0 |
#include <internal/facts/windows/uptime_resolver.hpp>
|
1 | |
#include <leatherman/windows/wmi.hpp>
|
|
1 |
#include <leatherman/windows/windows.hpp>
|
2 | 2 |
#include <leatherman/util/regex.hpp>
|
3 | |
#include <leatherman/logging/logging.hpp>
|
4 | 3 |
#include <leatherman/locale/locale.hpp>
|
5 | |
#include <boost/date_time/posix_time/posix_time.hpp>
|
6 | |
#include <boost/date_time/gregorian/gregorian_types.hpp>
|
7 | 4 |
|
8 | 5 |
// Mark string for translation (alias for leatherman::locale::format)
|
9 | 6 |
using leatherman::locale::_;
|
|
13 | 10 |
using namespace std;
|
14 | 11 |
using namespace leatherman::util;
|
15 | 12 |
using namespace leatherman::windows;
|
16 | |
using namespace boost::posix_time;
|
17 | |
using namespace boost::gregorian;
|
18 | 13 |
|
19 | |
uptime_resolver::uptime_resolver(shared_ptr<wmi> wmi_conn) :
|
20 | |
resolvers::uptime_resolver(),
|
21 | |
_wmi(move(wmi_conn))
|
|
14 |
uptime_resolver::uptime_resolver() :
|
|
15 |
resolvers::uptime_resolver()
|
22 | 16 |
{
|
23 | |
}
|
24 | |
|
25 | |
static ptime get_ptime(string const& wmitime)
|
26 | |
{
|
27 | |
static boost::regex wmi_regex("^(\\d{8,})(\\d{2})(\\d{2})(\\d{2})\\.");
|
28 | |
string iso_date;
|
29 | |
int hour, min, sec;
|
30 | |
if (!re_search(wmitime, wmi_regex, &iso_date, &hour, &min, &sec)) {
|
31 | |
throw runtime_error(_("failed to parse {1} as a date/time", wmitime));
|
32 | |
}
|
33 | |
|
34 | |
return ptime(from_undelimited_string(iso_date), time_duration(hour, min, sec));
|
35 | 17 |
}
|
36 | 18 |
|
37 | 19 |
int64_t uptime_resolver::get_uptime()
|
38 | 20 |
{
|
39 | |
auto vals = _wmi->query(wmi::operatingsystem, {wmi::lastbootuptime, wmi::localdatetime});
|
40 | |
if (vals.empty()) {
|
41 | |
return -1;
|
42 | |
}
|
43 | |
|
44 | |
ptime boottime = get_ptime(wmi::get(vals, wmi::lastbootuptime));
|
45 | |
ptime now = get_ptime(wmi::get(vals, wmi::localdatetime));
|
46 | |
return (now - boottime).total_seconds();
|
|
21 |
uint64_t tickCount = GetTickCount64();
|
|
22 |
return (int64_t)(tickCount / 1000); // seconds
|
47 | 23 |
}
|
48 | 24 |
|
49 | 25 |
}}} // namespace facter::facts::windows
|