Skip to content

Commit 6851e7b

Browse files
committed
Uptime / Users: add years, days-of-year and year-fraction
Like what `Disk` has
1 parent a734f18 commit 6851e7b

File tree

5 files changed

+71
-37
lines changed

5 files changed

+71
-37
lines changed

doc/json_schema.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,11 @@
351351
"type": "string"
352352
},
353353
"uptimeFormat": {
354-
"description": "Output format of the module `Uptime`. See Wiki for formatting syntax\n 1. {days}: Days\n 2. {hours}: Hours\n 3. {minutes}: Minutes\n 4. {seconds}: Seconds\n 5. {milliseconds}: Milliseconds\n 6. {boot-time}: Boot time in local timezone",
354+
"description": "Output format of the module `Uptime`. See Wiki for formatting syntax\n 1. {days}: Days after boot\n 2. {hours}: Hours after boot\n 3. {minutes}: Minutes after boot\n 4. {seconds}: Seconds after boot\n 5. {milliseconds}: Milliseconds after boot\n 6. {boot-time}: Boot time in local timezone\n 7. {years}: Years integer after boot\n 8. {days-of-year}: Days of year after boot\n 9. {years-fraction}: Years fraction after boot",
355355
"type": "string"
356356
},
357357
"usersFormat": {
358-
"description": "Output format of the module `Users`. See Wiki for formatting syntax\n 1. {name}: User name\n 2. {host-name}: Host name\n 3. {session}: Session name\n 4. {client-ip}: Client IP\n 5. {login-time}: Login Time in local timezone\n 6. {days}: Days after login\n 7. {hours}: Hours after login\n 8. {minutes}: Minutes after login\n 9. {seconds}: Seconds after login\n 10. {milliseconds}: Milliseconds after login",
358+
"description": "Output format of the module `Users`. See Wiki for formatting syntax\n 1. {name}: User name\n 2. {host-name}: Host name\n 3. {session}: Session name\n 4. {client-ip}: Client IP\n 5. {login-time}: Login Time in local timezone\n 6. {days}: Days after login\n 7. {hours}: Hours after login\n 8. {minutes}: Minutes after login\n 9. {seconds}: Seconds after login\n 10. {milliseconds}: Milliseconds after login\n 11. {years}: Years integer after login\n 12. {days-of-year}: Days of year after login\n 13. {years-fraction}: Years fraction after login",
359359
"type": "string"
360360
},
361361
"versionFormat": {

src/common/time.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,47 @@ static inline const char* ffTimeToTimeStr(uint64_t msec)
9797
#ifdef _WIN32
9898
#pragma GCC diagnostic pop
9999
#endif
100+
101+
typedef struct FFTimeGetAgeResult
102+
{
103+
uint32_t years;
104+
uint32_t daysOfYear;
105+
double yearsFraction;
106+
} FFTimeGetAgeResult;
107+
108+
static inline FFTimeGetAgeResult ffTimeGetAge(uint64_t birthMs, uint64_t nowMs)
109+
{
110+
FFTimeGetAgeResult result = {};
111+
if (__builtin_expect(birthMs == 0 || nowMs < birthMs, 0))
112+
return result;
113+
114+
time_t birth_s = (time_t) (birthMs / 1000);
115+
struct tm birth_tm;
116+
#ifdef _WIN32
117+
localtime_s(&birth_tm, &birth_s);
118+
#else
119+
localtime_r(&birth_s, &birth_tm);
120+
#endif
121+
122+
time_t now_s = (time_t) (nowMs / 1000);
123+
struct tm now_tm;
124+
#ifdef _WIN32
125+
localtime_s(&now_tm, &now_s);
126+
#else
127+
localtime_r(&now_s, &now_tm);
128+
#endif
129+
130+
result.years = (uint32_t) (now_tm.tm_year - birth_tm.tm_year);
131+
if (now_tm.tm_yday < birth_tm.tm_yday)
132+
result.years--;
133+
134+
birth_tm.tm_year += result.years;
135+
birth_s = mktime(&birth_tm);
136+
uint32_t diff_s = (uint32_t) (now_s - birth_s);
137+
result.daysOfYear = diff_s / (24 * 60 * 60);
138+
139+
birth_tm.tm_year += 1;
140+
result.yearsFraction = (double) diff_s / (double) (mktime(&birth_tm) - birth_s) + result.years;
141+
142+
return result;
143+
}

src/modules/disk/disk.c

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -137,33 +137,7 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk, uint32_t index
137137
duration /= 24;
138138
uint32_t days = (uint32_t) duration;
139139

140-
time_t past_s = (time_t) (disk->createTime / 1000);
141-
struct tm past_tm;
142-
#ifdef _WIN32
143-
localtime_s(&past_tm, &past_s);
144-
#else
145-
localtime_r(&past_s, &past_tm);
146-
#endif
147-
148-
time_t now_s = (time_t) (now / 1000);
149-
struct tm now_tm;
150-
#ifdef _WIN32
151-
localtime_s(&now_tm, &now_s);
152-
#else
153-
localtime_r(&now_s, &now_tm);
154-
#endif
155-
156-
uint32_t years = (uint32_t) (now_tm.tm_year - past_tm.tm_year);
157-
if (now_tm.tm_yday < past_tm.tm_yday)
158-
years--;
159-
past_tm.tm_year += years;
160-
uint32_t gap_s = (uint32_t) (now_s - mktime(&past_tm));
161-
uint32_t daysOfYear = gap_s / (24 * 60 * 60);
162-
163-
uint32_t pastYear = past_tm.tm_year + 1900;
164-
bool isLeapYear = (pastYear % 4 == 0 && pastYear % 100 != 0) || (pastYear % 400 == 0);
165-
double yearsFraction = (double) daysOfYear / (isLeapYear ? 366 : 365) + years;
166-
140+
FFTimeGetAgeResult age = ffTimeGetAge(disk->createTime, now);
167141
FF_PRINT_FORMAT_CHECKED(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY, ((FFformatarg[]) {
168142
FF_FORMAT_ARG(usedPretty, "size-used"),
169143
FF_FORMAT_ARG(totalPretty, "size-total"),
@@ -186,9 +160,9 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk, uint32_t index
186160
FF_FORMAT_ARG(milliseconds, "milliseconds"),
187161
FF_FORMAT_ARG(disk->mountpoint, "mountpoint"),
188162
FF_FORMAT_ARG(disk->mountFrom, "mount-from"),
189-
FF_FORMAT_ARG(years, "years"),
190-
FF_FORMAT_ARG(daysOfYear, "days-of-year"),
191-
FF_FORMAT_ARG(yearsFraction, "years-fraction"),
163+
FF_FORMAT_ARG(age.years, "years"),
164+
FF_FORMAT_ARG(age.daysOfYear, "days-of-year"),
165+
FF_FORMAT_ARG(age.yearsFraction, "years-fraction"),
192166
}));
193167
}
194168
}

src/modules/uptime/uptime.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,18 @@ void ffPrintUptime(FFUptimeOptions* options)
3939
uptime /= 24;
4040
uint32_t days = (uint32_t) uptime;
4141

42+
FFTimeGetAgeResult age = ffTimeGetAge(result.bootTime, ffTimeGetNow());
43+
4244
FF_PRINT_FORMAT_CHECKED(FF_UPTIME_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]){
4345
FF_FORMAT_ARG(days, "days"),
4446
FF_FORMAT_ARG(hours, "hours"),
4547
FF_FORMAT_ARG(minutes, "minutes"),
4648
FF_FORMAT_ARG(seconds, "seconds"),
4749
FF_FORMAT_ARG(milliseconds, "milliseconds"),
4850
{FF_FORMAT_ARG_TYPE_STRING, ffTimeToShortStr(result.bootTime), "boot-time"},
51+
FF_FORMAT_ARG(age.years, "years"),
52+
FF_FORMAT_ARG(age.daysOfYear, "days-of-year"),
53+
FF_FORMAT_ARG(age.yearsFraction, "years-fraction"),
4954
}));
5055
}
5156
}
@@ -110,12 +115,15 @@ static FFModuleBaseInfo ffModuleInfo = {
110115
.generateJsonResult = (void*) ffGenerateUptimeJsonResult,
111116
.generateJsonConfig = (void*) ffGenerateUptimeJsonConfig,
112117
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
113-
{"Days", "days"},
114-
{"Hours", "hours"},
115-
{"Minutes", "minutes"},
116-
{"Seconds", "seconds"},
117-
{"Milliseconds", "milliseconds"},
118+
{"Days after boot", "days"},
119+
{"Hours after boot", "hours"},
120+
{"Minutes after boot", "minutes"},
121+
{"Seconds after boot", "seconds"},
122+
{"Milliseconds after boot", "milliseconds"},
118123
{"Boot time in local timezone", "boot-time"},
124+
{"Years integer after boot", "years"},
125+
{"Days of year after boot", "days-of-year"},
126+
{"Years fraction after boot", "years-fraction"},
119127
}))
120128
};
121129

src/modules/users/users.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ void ffPrintUsers(FFUsersOptions* options)
7878
duration /= 24;
7979
uint32_t days = (uint32_t) duration;
8080

81+
FFTimeGetAgeResult age = ffTimeGetAge(user->loginTime, ffTimeGetNow());
82+
8183
FF_PRINT_FORMAT_CHECKED(FF_USERS_MODULE_NAME, users.length == 1 ? 0 : (uint8_t) (i + 1), &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]){
8284
FF_FORMAT_ARG(user->name, "name"),
8385
FF_FORMAT_ARG(user->hostName, "host-name"),
@@ -89,6 +91,9 @@ void ffPrintUsers(FFUsersOptions* options)
8991
FF_FORMAT_ARG(minutes, "minutes"),
9092
FF_FORMAT_ARG(seconds, "seconds"),
9193
FF_FORMAT_ARG(milliseconds, "milliseconds"),
94+
FF_FORMAT_ARG(age.years, "years"),
95+
FF_FORMAT_ARG(age.daysOfYear, "days-of-year"),
96+
FF_FORMAT_ARG(age.yearsFraction, "years-fraction"),
9297
}));
9398
}
9499
}
@@ -222,6 +227,9 @@ static FFModuleBaseInfo ffModuleInfo = {
222227
{"Minutes after login", "minutes"},
223228
{"Seconds after login", "seconds"},
224229
{"Milliseconds after login", "milliseconds"},
230+
{"Years integer after login", "years"},
231+
{"Days of year after login", "days-of-year"},
232+
{"Years fraction after login", "years-fraction"},
225233
}))
226234
};
227235

0 commit comments

Comments
 (0)