Skip to content

Commit ea86273

Browse files
committed
Common: refactors time formatting to use a shared buffer and utility macro
1 parent 4f88e1f commit ea86273

File tree

5 files changed

+34
-23
lines changed

5 files changed

+34
-23
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ set(LIBFASTFETCH_SRC
370370
src/common/settings.c
371371
src/common/size.c
372372
src/common/temps.c
373+
src/common/time.c
373374
src/detection/bluetoothradio/bluetoothradio.c
374375
src/detection/bootmgr/bootmgr.c
375376
src/detection/chassis/chassis.c

src/common/time.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include <time.h>
2+
3+
char ffTimeInternalBuffer[64]; // Reduce memory usage and prevent redundant allocations

src/common/time.h

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <OS.h>
1212
#endif
1313

14+
#include "util/arrayUtils.h"
15+
1416
static inline double ffTimeGetTick(void) //In msec
1517
{
1618
#ifdef _WIN32
@@ -64,11 +66,12 @@ static inline const char* ffTimeToFullStr(uint64_t msec)
6466
time_t tsec = (time_t) (msec / 1000);
6567
const struct tm* tm = localtime(&tsec);
6668

67-
static char buf[32];
68-
strftime(buf, __builtin_strlen("0000-00-00T00:00:00") + 1, "%FT%T", tm);
69-
snprintf(buf + __builtin_strlen("0000-00-00T00:00:00"), __builtin_strlen(".000") + 1, ".%03u", (unsigned) (msec % 1000));
70-
strftime(buf + __builtin_strlen("0000-00-00T00:00:00.000"), __builtin_strlen("+0000") + 1, "%z", tm);
71-
return buf;
69+
extern char ffTimeInternalBuffer[64];
70+
uint32_t len = 0;
71+
len += (uint32_t) strftime(ffTimeInternalBuffer, ARRAY_SIZE(ffTimeInternalBuffer) - len, "%FT%T", tm);
72+
len += (uint32_t) snprintf(ffTimeInternalBuffer + len, ARRAY_SIZE(ffTimeInternalBuffer) - len, ".%03u", (unsigned) (msec % 1000));
73+
len += (uint32_t) strftime(ffTimeInternalBuffer + len, ARRAY_SIZE(ffTimeInternalBuffer) - len, "%z", tm);
74+
return ffTimeInternalBuffer;
7275
}
7376

7477
// Not thread-safe
@@ -77,9 +80,9 @@ static inline const char* ffTimeToShortStr(uint64_t msec)
7780
if (msec == 0) return "";
7881
time_t tsec = (time_t) (msec / 1000);
7982

80-
static char buf[32];
81-
strftime(buf, sizeof(buf), "%F %T", localtime(&tsec));
82-
return buf;
83+
extern char ffTimeInternalBuffer[64];
84+
strftime(ffTimeInternalBuffer, ARRAY_SIZE(ffTimeInternalBuffer), "%F %T", localtime(&tsec));
85+
return ffTimeInternalBuffer;
8386
}
8487

8588
// Not thread-safe
@@ -88,10 +91,10 @@ static inline const char* ffTimeToTimeStr(uint64_t msec)
8891
if (msec == 0) return "";
8992
time_t tsec = (time_t) (msec / 1000);
9093

91-
static char buf[32];
92-
strftime(buf, sizeof(buf), "%T", localtime(&tsec));
93-
sprintf(buf + __builtin_strlen("00:00:00"), ".%03u", (unsigned) (msec % 1000));
94-
return buf;
94+
extern char ffTimeInternalBuffer[64];
95+
strftime(ffTimeInternalBuffer, ARRAY_SIZE(ffTimeInternalBuffer), "%T", localtime(&tsec));
96+
sprintf(ffTimeInternalBuffer + __builtin_strlen("00:00:00"), ".%03u", (unsigned) (msec % 1000));
97+
return ffTimeInternalBuffer;
9598
}
9699

97100
#ifdef _WIN32

src/fastfetch.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define __attribute__(x)
1616
#endif
1717

18+
#include "util/arrayUtils.h"
1819
#include "util/FFstrbuf.h"
1920
#include "util/FFlist.h"
2021
#include "util/platform/FFPlatform.h"
@@ -24,17 +25,6 @@
2425
#include "options/display.h"
2526
#include "options/general.h"
2627

27-
#ifdef __has_builtin
28-
#if __has_builtin(__is_array)
29-
#define ARRAY_SIZE(x) ({ static_assert(__is_array(__typeof__(x)), "Must be an array"); (uint32_t) (sizeof(x) / sizeof(*(x))); })
30-
#elif __has_builtin(__builtin_types_compatible_p)
31-
#define ARRAY_SIZE(x) ({ static_assert(!__builtin_types_compatible_p(__typeof__(x), __typeof__(&*(x))), "Must not be a pointer"); (uint32_t) (sizeof(x) / sizeof(*(x))); })
32-
#endif
33-
#endif
34-
#ifndef ARRAY_SIZE
35-
#define ARRAY_SIZE(x) ((uint32_t) (sizeof(x) / sizeof(*(x))))
36-
#endif
37-
3828

3929
typedef struct FFconfig
4030
{

src/util/arrayUtils.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
#include <assert.h>
4+
5+
#ifdef __has_builtin
6+
#if __has_builtin(__is_array)
7+
#define ARRAY_SIZE(x) ({ static_assert(__is_array(__typeof__(x)), "Must be an array"); (uint32_t) (sizeof(x) / sizeof(*(x))); })
8+
#elif __has_builtin(__builtin_types_compatible_p)
9+
#define ARRAY_SIZE(x) ({ static_assert(!__builtin_types_compatible_p(__typeof__(x), __typeof__(&*(x))), "Must not be a pointer"); (uint32_t) (sizeof(x) / sizeof(*(x))); })
10+
#endif
11+
#endif
12+
#ifndef ARRAY_SIZE
13+
#define ARRAY_SIZE(x) ((uint32_t) (sizeof(x) / sizeof(*(x))))
14+
#endif

0 commit comments

Comments
 (0)