Skip to content

Commit 5d960ec

Browse files
committed
Uptime: fix non-normalized time being shown
Fix #1720
1 parent 3b36b8c commit 5d960ec

File tree

6 files changed

+117
-33
lines changed

6 files changed

+117
-33
lines changed

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,11 +1841,19 @@ if (BUILD_TESTS)
18411841
PRIVATE libfastfetch
18421842
)
18431843

1844+
add_executable(fastfetch-test-duration
1845+
tests/duration.c
1846+
)
1847+
target_link_libraries(fastfetch-test-duration
1848+
PRIVATE libfastfetch
1849+
)
1850+
18441851
enable_testing()
18451852
add_test(NAME test-strbuf COMMAND fastfetch-test-strbuf)
18461853
add_test(NAME test-list COMMAND fastfetch-test-list)
18471854
add_test(NAME test-format COMMAND fastfetch-test-format)
18481855
add_test(NAME test-color COMMAND fastfetch-test-color)
1856+
add_test(NAME test-duration COMMAND fastfetch-test-duration)
18491857
endif()
18501858

18511859
##################

src/common/parsing.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,19 +190,26 @@ void ffParseGTK(FFstrbuf* buffer, const FFstrbuf* gtk2, const FFstrbuf* gtk3, co
190190
}
191191
}
192192

193-
void ffParseDuration(uint32_t days, uint32_t hours, uint32_t minutes, uint32_t seconds, FFstrbuf* result)
193+
void ffParseDuration(uint64_t totalSeconds, FFstrbuf* result)
194194
{
195-
if(days == 0 && hours == 0 && minutes == 0)
195+
if(totalSeconds < 60)
196196
{
197-
ffStrbufAppendF(result, "%u seconds", seconds);
197+
ffStrbufAppendF(result, "%u second", (unsigned) totalSeconds);
198+
if (totalSeconds != 1)
199+
ffStrbufAppendC(result, 's');
198200
return;
199201
}
200202

201-
if(seconds >= 30)
202-
{
203-
minutes++;
204-
seconds = 0;
205-
}
203+
uint32_t seconds = totalSeconds % 60;
204+
totalSeconds /= 60;
205+
if (seconds >= 30)
206+
totalSeconds++;
207+
208+
uint32_t minutes = totalSeconds % 60;
209+
totalSeconds /= 60;
210+
uint32_t hours = totalSeconds % 24;
211+
totalSeconds /= 24;
212+
uint32_t days = (uint32_t) totalSeconds;
206213

207214
if(days > 0)
208215
{

src/common/parsing.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ int8_t ffVersionCompare(const FFVersion* version1, const FFVersion* version2);
2727

2828
void ffParseSize(uint64_t bytes, FFstrbuf* result);
2929
bool ffParseFrequency(uint32_t mhz, FFstrbuf* result);
30-
void ffParseDuration(uint32_t days, uint32_t hours, uint32_t minutes, uint32_t seconds, FFstrbuf* result);
30+
void ffParseDuration(uint64_t totalSeconds, FFstrbuf* result);

src/modules/battery/battery.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,6 @@ static void printBattery(FFBatteryOptions* options, FFBatteryResult* result, uin
2727
}));
2828
}
2929

30-
31-
uint32_t timeRemaining = result->timeRemaining < 0 ? 0 : (uint32_t) result->timeRemaining;
32-
33-
uint32_t seconds = timeRemaining % 60;
34-
timeRemaining /= 60;
35-
uint32_t minutes = timeRemaining % 60;
36-
timeRemaining /= 60;
37-
uint32_t hours = timeRemaining % 24;
38-
timeRemaining /= 24;
39-
uint32_t days = timeRemaining;
40-
4130
FFPercentageTypeFlags percentType = options->percent.type == 0 ? instance.config.display.percentType : options->percent.type;
4231

4332
if(options->moduleArgs.outputFormat.length == 0)
@@ -70,7 +59,7 @@ static void printBattery(FFBatteryOptions* options, FFBatteryResult* result, uin
7059
if(str.length > 0)
7160
ffStrbufAppendS(&str, " (");
7261

73-
ffParseDuration(days, hours, minutes, seconds, &str);
62+
ffParseDuration((uint32_t) result->timeRemaining, &str);
7463
ffStrbufAppendS(&str, " remaining)");
7564
}
7665
}
@@ -95,6 +84,15 @@ static void printBattery(FFBatteryOptions* options, FFBatteryResult* result, uin
9584
}
9685
else
9786
{
87+
uint32_t timeRemaining = result->timeRemaining < 0 ? 0 : (uint32_t) result->timeRemaining;
88+
uint32_t seconds = timeRemaining % 60;
89+
timeRemaining /= 60;
90+
uint32_t minutes = timeRemaining % 60;
91+
timeRemaining /= 60;
92+
uint32_t hours = timeRemaining % 24;
93+
timeRemaining /= 24;
94+
uint32_t days = timeRemaining;
95+
9896
FF_STRBUF_AUTO_DESTROY capacityNum = ffStrbufCreate();
9997
if(percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
10098
ffPercentAppendNum(&capacityNum, result->capacity, options->percent, false, &options->moduleArgs);

src/modules/uptime/uptime.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
void ffPrintUptime(FFUptimeOptions* options)
99
{
10-
FFUptimeResult result;
10+
FFUptimeResult result = {};
1111

1212
const char* error = ffDetectUptime(&result);
1313

@@ -19,26 +19,26 @@ void ffPrintUptime(FFUptimeOptions* options)
1919

2020
uint64_t uptime = result.uptime;
2121

22-
uint32_t milliseconds = (uint32_t) (uptime % 1000);
23-
uptime /= 1000;
24-
uint32_t seconds = (uint32_t) (uptime % 60);
25-
uptime /= 60;
26-
uint32_t minutes = (uint32_t) (uptime % 60);
27-
uptime /= 60;
28-
uint32_t hours = (uint32_t) (uptime % 24);
29-
uptime /= 24;
30-
uint32_t days = (uint32_t) uptime;
31-
3222
if(options->moduleArgs.outputFormat.length == 0)
3323
{
3424
ffPrintLogoAndKey(FF_UPTIME_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
3525
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
36-
ffParseDuration(days, hours, minutes, seconds, &buffer);
26+
ffParseDuration((uptime + 500) / 1000, &buffer);
3727

3828
ffStrbufPutTo(&buffer, stdout);
3929
}
4030
else
4131
{
32+
uint32_t milliseconds = (uint32_t) (uptime % 1000);
33+
uptime /= 1000;
34+
uint32_t seconds = (uint32_t) (uptime % 60);
35+
uptime /= 60;
36+
uint32_t minutes = (uint32_t) (uptime % 60);
37+
uptime /= 60;
38+
uint32_t hours = (uint32_t) (uptime % 24);
39+
uptime /= 24;
40+
uint32_t days = (uint32_t) uptime;
41+
4242
FF_PRINT_FORMAT_CHECKED(FF_UPTIME_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]){
4343
FF_FORMAT_ARG(days, "days"),
4444
FF_FORMAT_ARG(hours, "hours"),

tests/duration.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include "common/parsing.h"
2+
#include "util/textModifier.h"
3+
#include "fastfetch.h"
4+
5+
#include <stdlib.h>
6+
7+
static void verify(uint64_t totalSeconds, const char* expected, int lineNo)
8+
{
9+
FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate();
10+
ffParseDuration(totalSeconds, &result);
11+
if (!ffStrbufEqualS(&result, expected))
12+
{
13+
fprintf(stderr, FASTFETCH_TEXT_MODIFIER_ERROR "[%d] %llu: expected \"%s\", got \"%s\"\n" FASTFETCH_TEXT_MODIFIER_RESET, lineNo, (unsigned long long) totalSeconds, expected, result.chars);
14+
exit(1);
15+
}
16+
}
17+
18+
#define VERIFY(color, expected) verify((color), (expected), __LINE__)
19+
20+
int main(void)
21+
{
22+
// Test seconds less than 60
23+
VERIFY(0, "0 seconds");
24+
VERIFY(1, "1 second");
25+
VERIFY(2, "2 seconds");
26+
VERIFY(59, "59 seconds");
27+
28+
// Test minute rounding (when seconds >= 30)
29+
VERIFY(60, "1 min");
30+
VERIFY(60 + 29, "1 min");
31+
VERIFY(60 + 30, "2 mins");
32+
33+
// Test only minutes
34+
VERIFY(60 * 2 - 1, "2 mins");
35+
VERIFY(60 * 2, "2 mins");
36+
VERIFY(60 * 59 + 29, "59 mins");
37+
38+
// Test only hours (no minutes)
39+
VERIFY(60 * 59 + 30, "1 hour");
40+
VERIFY(60 * 60, "1 hour");
41+
VERIFY(2 * 60 * 60, "2 hours");
42+
VERIFY(23 * 60 * 60, "23 hours");
43+
44+
// Test combination of hours and minutes
45+
VERIFY(60 * 60 + 60, "1 hour, 1 min");
46+
VERIFY(60 * 60 + 60 * 2, "1 hour, 2 mins");
47+
VERIFY(60 * 60 * 2 + 60 + 29, "2 hours, 1 min");
48+
VERIFY(60 * 60 * 2 + 60 + 30, "2 hours, 2 mins");
49+
50+
// Test days
51+
VERIFY(60 * 60 * 24, "1 day");
52+
VERIFY(60 * 60 * 24 - 1, "1 day");
53+
VERIFY(60 * 60 * 24 * 2, "2 days");
54+
55+
// Test combination of days and hours
56+
VERIFY(60 * 60 * 24 + 60 * 60, "1 day, 1 hour");
57+
VERIFY(60 * 60 * 24 * 2 + 60 * 60, "2 days, 1 hour");
58+
VERIFY(60 * 60 * 24 * 2 + 60 * 60 * 2, "2 days, 2 hours");
59+
60+
// Test combination of days, hours, and minutes
61+
VERIFY(60 * 60 * 24 + 60 * 60 + 60, "1 day, 1 hour, 1 min");
62+
VERIFY(60 * 60 * 24 * 2 + 60 * 60 + 60 * 2, "2 days, 1 hour, 2 mins");
63+
VERIFY(60 * 60 * 24 * 2 + 60 * 2, "2 days, 2 mins");
64+
65+
// Test very large number of days
66+
VERIFY(60 * 60 * 24 * 100, "100 days(!)");
67+
VERIFY(60 * 60 * 24 * 200, "200 days(!)");
68+
69+
//Success
70+
puts("\033[32mAll tests passed!" FASTFETCH_TEXT_MODIFIER_RESET);
71+
}

0 commit comments

Comments
 (0)