Skip to content

Commit dde0c22

Browse files
committed
Disk: add keys years, days-of-year and years-fraction
Fix #1847
1 parent 1311a80 commit dde0c22

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

src/modules/disk/disk.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk, uint32_t index
125125
bool isHidden = !!(disk->type & FF_DISK_VOLUME_TYPE_HIDDEN_BIT);
126126
bool isReadOnly = !!(disk->type & FF_DISK_VOLUME_TYPE_READONLY_BIT);
127127

128-
uint64_t duration = ffTimeGetNow() - disk->createTime;
128+
uint64_t now = ffTimeGetNow();
129+
uint64_t duration = now - disk->createTime;
129130
uint32_t milliseconds = (uint32_t) (duration % 1000);
130131
duration /= 1000;
131132
uint32_t seconds = (uint32_t) (duration % 60);
@@ -136,6 +137,33 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk, uint32_t index
136137
duration /= 24;
137138
uint32_t days = (uint32_t) duration;
138139

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+
139167
FF_PRINT_FORMAT_CHECKED(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY, ((FFformatarg[]) {
140168
FF_FORMAT_ARG(usedPretty, "size-used"),
141169
FF_FORMAT_ARG(totalPretty, "size-total"),
@@ -158,6 +186,9 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk, uint32_t index
158186
FF_FORMAT_ARG(milliseconds, "milliseconds"),
159187
FF_FORMAT_ARG(disk->mountpoint, "mountpoint"),
160188
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"),
161192
}));
162193
}
163194
}
@@ -523,6 +554,9 @@ static FFModuleBaseInfo ffModuleInfo = {
523554
{"Milliseconds after creation", "milliseconds"},
524555
{"Mount point / drive letter", "mountpoint"},
525556
{"Mount from (device path)", "mount-from"},
557+
{"Years integer after creation", "years"},
558+
{"Days of year after creation", "days-of-year"},
559+
{"Years fraction after creation", "years-fraction"},
526560
}))
527561
};
528562

0 commit comments

Comments
 (0)