mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-11-01 10:32:35 +01:00
Uptime: detect boot time
This commit is contained in:
@@ -5,6 +5,12 @@
|
||||
|
||||
#include "fastfetch.h"
|
||||
|
||||
const char* ffDetectUptime(uint64_t* result);
|
||||
typedef struct FFUptimeResult
|
||||
{
|
||||
uint64_t bootTime;
|
||||
uint64_t uptime;
|
||||
} FFUptimeResult;
|
||||
|
||||
const char* ffDetectUptime(FFUptimeResult* result);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -4,18 +4,28 @@
|
||||
#include <sys/sysctl.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
const char* ffDetectUptime(uint64_t* result)
|
||||
const char* ffDetectUptime(FFUptimeResult* result)
|
||||
{
|
||||
struct timeval bootTime;
|
||||
size_t bootTimeSize = sizeof(bootTime);
|
||||
if(sysctl(
|
||||
(int[]) {CTL_KERN, KERN_BOOTTIME}, 2,
|
||||
&bootTime, &bootTimeSize,
|
||||
NULL, 0
|
||||
) != 0)
|
||||
return "sysctl({CTL_KERN, KERN_BOOTTIME}) failed";
|
||||
{
|
||||
struct timeval bootTime;
|
||||
size_t bootTimeSize = sizeof(bootTime);
|
||||
if(sysctl(
|
||||
(int[]) {CTL_KERN, KERN_BOOTTIME}, 2,
|
||||
&bootTime, &bootTimeSize,
|
||||
NULL, 0
|
||||
) != 0)
|
||||
return "sysctl({CTL_KERN, KERN_BOOTTIME}) failed";
|
||||
|
||||
*result = (uint64_t) difftime(time(NULL), bootTime.tv_sec);
|
||||
result->bootTime = (uint64_t) bootTime.tv_sec * 1000 + (uint64_t) bootTime.tv_usec / 1000;
|
||||
}
|
||||
|
||||
{
|
||||
struct timespec realTime;
|
||||
if(clock_gettime(CLOCK_REALTIME, &realTime) != 0)
|
||||
return "clock_gettime(CLOCK_REALTIME) failed";
|
||||
uint64_t now = (uint64_t) realTime.tv_sec * 1000 + (uint64_t) realTime.tv_nsec / 1000000;
|
||||
result->uptime = now - result->bootTime;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,20 @@
|
||||
#include "uptime.h"
|
||||
|
||||
#include <sys/sysinfo.h>
|
||||
#include <time.h>
|
||||
|
||||
const char* ffDetectUptime(uint64_t* result)
|
||||
const char* ffDetectUptime(FFUptimeResult* result)
|
||||
{
|
||||
struct sysinfo info;
|
||||
if(sysinfo(&info) != 0)
|
||||
return "sysinfo() failed";
|
||||
*result = (uint64_t) info.uptime;
|
||||
struct timespec uptime;
|
||||
if (clock_gettime(CLOCK_BOOTTIME, &uptime) != 0)
|
||||
return "clock_gettime(CLOCK_BOOTTIME) failed";
|
||||
|
||||
result->uptime = (uint64_t) uptime.tv_sec * 1000 + (uint64_t) uptime.tv_nsec / 1000000;
|
||||
|
||||
struct timespec realTime;
|
||||
if(clock_gettime(CLOCK_REALTIME, &realTime) != 0)
|
||||
return "clock_gettime(CLOCK_REALTIME) failed";
|
||||
|
||||
result->bootTime = (uint64_t) realTime.tv_sec * 1000 + (uint64_t) realTime.tv_nsec / 1000000 + result->uptime;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -2,8 +2,17 @@
|
||||
|
||||
#include <sysinfoapi.h>
|
||||
|
||||
const char* ffDetectUptime(uint64_t* result)
|
||||
static inline uint64_t to_ms(uint64_t ret)
|
||||
{
|
||||
*result = GetTickCount64() / 1000;
|
||||
ret -= 116444736000000000ull;
|
||||
return ret / 10000ull;
|
||||
}
|
||||
|
||||
const char* ffDetectUptime(FFUptimeResult* result)
|
||||
{
|
||||
result->uptime = GetTickCount64();
|
||||
FILETIME fileTime;
|
||||
GetSystemTimeAsFileTime(&fileTime);
|
||||
result->bootTime = to_ms(*(uint64_t*) &fileTime) - result->uptime;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <windows.h>
|
||||
#include <wtsapi32.h>
|
||||
|
||||
static uint64_t to_ms(uint64_t ret)
|
||||
static inline uint64_t to_ms(uint64_t ret)
|
||||
{
|
||||
ret -= 116444736000000000ull;
|
||||
return ret / 10000ull;
|
||||
|
||||
Reference in New Issue
Block a user