Uptime / Users: add years, days-of-year and year-fraction

Like what `Disk` has
This commit is contained in:
李通洲
2025-07-16 09:51:58 +08:00
parent a734f18fd5
commit 6851e7b082
5 changed files with 71 additions and 37 deletions
+4 -30
View File
@@ -137,33 +137,7 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk, uint32_t index
duration /= 24;
uint32_t days = (uint32_t) duration;
time_t past_s = (time_t) (disk->createTime / 1000);
struct tm past_tm;
#ifdef _WIN32
localtime_s(&past_tm, &past_s);
#else
localtime_r(&past_s, &past_tm);
#endif
time_t now_s = (time_t) (now / 1000);
struct tm now_tm;
#ifdef _WIN32
localtime_s(&now_tm, &now_s);
#else
localtime_r(&now_s, &now_tm);
#endif
uint32_t years = (uint32_t) (now_tm.tm_year - past_tm.tm_year);
if (now_tm.tm_yday < past_tm.tm_yday)
years--;
past_tm.tm_year += years;
uint32_t gap_s = (uint32_t) (now_s - mktime(&past_tm));
uint32_t daysOfYear = gap_s / (24 * 60 * 60);
uint32_t pastYear = past_tm.tm_year + 1900;
bool isLeapYear = (pastYear % 4 == 0 && pastYear % 100 != 0) || (pastYear % 400 == 0);
double yearsFraction = (double) daysOfYear / (isLeapYear ? 366 : 365) + years;
FFTimeGetAgeResult age = ffTimeGetAge(disk->createTime, now);
FF_PRINT_FORMAT_CHECKED(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY, ((FFformatarg[]) {
FF_FORMAT_ARG(usedPretty, "size-used"),
FF_FORMAT_ARG(totalPretty, "size-total"),
@@ -186,9 +160,9 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk, uint32_t index
FF_FORMAT_ARG(milliseconds, "milliseconds"),
FF_FORMAT_ARG(disk->mountpoint, "mountpoint"),
FF_FORMAT_ARG(disk->mountFrom, "mount-from"),
FF_FORMAT_ARG(years, "years"),
FF_FORMAT_ARG(daysOfYear, "days-of-year"),
FF_FORMAT_ARG(yearsFraction, "years-fraction"),
FF_FORMAT_ARG(age.years, "years"),
FF_FORMAT_ARG(age.daysOfYear, "days-of-year"),
FF_FORMAT_ARG(age.yearsFraction, "years-fraction"),
}));
}
}
+13 -5
View File
@@ -39,6 +39,8 @@ void ffPrintUptime(FFUptimeOptions* options)
uptime /= 24;
uint32_t days = (uint32_t) uptime;
FFTimeGetAgeResult age = ffTimeGetAge(result.bootTime, ffTimeGetNow());
FF_PRINT_FORMAT_CHECKED(FF_UPTIME_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]){
FF_FORMAT_ARG(days, "days"),
FF_FORMAT_ARG(hours, "hours"),
@@ -46,6 +48,9 @@ void ffPrintUptime(FFUptimeOptions* options)
FF_FORMAT_ARG(seconds, "seconds"),
FF_FORMAT_ARG(milliseconds, "milliseconds"),
{FF_FORMAT_ARG_TYPE_STRING, ffTimeToShortStr(result.bootTime), "boot-time"},
FF_FORMAT_ARG(age.years, "years"),
FF_FORMAT_ARG(age.daysOfYear, "days-of-year"),
FF_FORMAT_ARG(age.yearsFraction, "years-fraction"),
}));
}
}
@@ -110,12 +115,15 @@ static FFModuleBaseInfo ffModuleInfo = {
.generateJsonResult = (void*) ffGenerateUptimeJsonResult,
.generateJsonConfig = (void*) ffGenerateUptimeJsonConfig,
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
{"Days", "days"},
{"Hours", "hours"},
{"Minutes", "minutes"},
{"Seconds", "seconds"},
{"Milliseconds", "milliseconds"},
{"Days after boot", "days"},
{"Hours after boot", "hours"},
{"Minutes after boot", "minutes"},
{"Seconds after boot", "seconds"},
{"Milliseconds after boot", "milliseconds"},
{"Boot time in local timezone", "boot-time"},
{"Years integer after boot", "years"},
{"Days of year after boot", "days-of-year"},
{"Years fraction after boot", "years-fraction"},
}))
};
+8
View File
@@ -78,6 +78,8 @@ void ffPrintUsers(FFUsersOptions* options)
duration /= 24;
uint32_t days = (uint32_t) duration;
FFTimeGetAgeResult age = ffTimeGetAge(user->loginTime, ffTimeGetNow());
FF_PRINT_FORMAT_CHECKED(FF_USERS_MODULE_NAME, users.length == 1 ? 0 : (uint8_t) (i + 1), &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]){
FF_FORMAT_ARG(user->name, "name"),
FF_FORMAT_ARG(user->hostName, "host-name"),
@@ -89,6 +91,9 @@ void ffPrintUsers(FFUsersOptions* options)
FF_FORMAT_ARG(minutes, "minutes"),
FF_FORMAT_ARG(seconds, "seconds"),
FF_FORMAT_ARG(milliseconds, "milliseconds"),
FF_FORMAT_ARG(age.years, "years"),
FF_FORMAT_ARG(age.daysOfYear, "days-of-year"),
FF_FORMAT_ARG(age.yearsFraction, "years-fraction"),
}));
}
}
@@ -222,6 +227,9 @@ static FFModuleBaseInfo ffModuleInfo = {
{"Minutes after login", "minutes"},
{"Seconds after login", "seconds"},
{"Milliseconds after login", "milliseconds"},
{"Years integer after login", "years"},
{"Days of year after login", "days-of-year"},
{"Years fraction after login", "years-fraction"},
}))
};