-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: hexian000 <[email protected]>
- Loading branch information
Showing
5 changed files
with
170 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* csnippets (c) 2019-2023 He Xian <[email protected]> | ||
* This code is licensed under MIT license (see LICENSE for details) */ | ||
|
||
#include "formats.h" | ||
#include "utils/arraysize.h" | ||
#include "utils/minmax.h" | ||
|
||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <math.h> | ||
|
||
struct duration make_duration(double value) | ||
{ | ||
if (!isfinite(value)) { | ||
return (struct duration){ 0 }; | ||
} | ||
struct duration d; | ||
if (value < 0.0) { | ||
d.sign = -1; | ||
value = -value; | ||
} else { | ||
d.sign = 1; | ||
} | ||
d.nanos = (unsigned int)floor(fmod(value * 1e+9, 1000.0)); | ||
d.micros = (unsigned int)floor(fmod(value * 1e+6, 1000.0)); | ||
d.millis = (unsigned int)floor(fmod(value * 1e+3, 1000.0)); | ||
d.seconds = (unsigned int)floor(fmod(value, 60.0)); | ||
value /= 60.0; | ||
d.minutes = (unsigned int)floor(fmod(value, 60.0)); | ||
value /= 60.0; | ||
d.hours = (unsigned int)floor(fmod(value, 24.0)); | ||
value /= 24.0; | ||
d.days = (unsigned int)floor(value); | ||
return d; | ||
} | ||
|
||
struct duration make_duration_nanos(int64_t value) | ||
{ | ||
struct duration d; | ||
if (value < INT64_C(0)) { | ||
d.sign = -1; | ||
value = -value; | ||
} else { | ||
d.sign = 1; | ||
} | ||
d.nanos = (unsigned int)(value % 1000); | ||
value /= 1000; | ||
d.micros = (unsigned int)(value % 1000); | ||
value /= 1000; | ||
d.millis = (unsigned int)(value % 1000); | ||
value /= 1000; | ||
d.seconds = (unsigned int)(value % 60); | ||
value /= 60; | ||
d.minutes = (unsigned int)(value % 60); | ||
value /= 60; | ||
d.hours = (unsigned int)(value % 24); | ||
value /= 24; | ||
d.days = (unsigned int)value; | ||
return d; | ||
} | ||
|
||
static const char *iec_units[] = { | ||
"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB", | ||
}; | ||
|
||
int format_iec_bytes(char *buf, const size_t bufsize, const double value) | ||
{ | ||
if (!isfinite(value) || (value < 8192.0)) { | ||
return snprintf(buf, bufsize, "%.0lf %s", value, iec_units[0]); | ||
} | ||
const int x = ((int)log2(value) - 3) / 10; | ||
const int n = (int)ARRAY_SIZE(iec_units) - 1; | ||
const int i = CLAMP(x, 0, n); | ||
const double v = ldexp(value, i * -10); | ||
if (v < 10.0) { | ||
return snprintf(buf, bufsize, "%.02lf %s", v, iec_units[i]); | ||
} | ||
if (v < 100.0) { | ||
return snprintf(buf, bufsize, "%.01lf %s", v, iec_units[i]); | ||
} | ||
return snprintf(buf, bufsize, "%.0lf %s", v, iec_units[i]); | ||
} | ||
|
||
int format_duration_seconds(char *b, const size_t size, const struct duration d) | ||
{ | ||
if (d.days) { | ||
return snprintf( | ||
b, size, "%dd%02u:%02u:%02u", d.sign * (int)d.days, | ||
d.hours, d.minutes, d.seconds); | ||
} else if (d.hours) { | ||
return snprintf( | ||
b, size, "%d:%02u:%02u", d.sign * (int)d.hours, | ||
d.minutes, d.seconds); | ||
} else if (d.minutes) { | ||
return snprintf( | ||
b, size, "%d:%02u", d.sign * (int)d.minutes, d.seconds); | ||
} | ||
if (d.sign < 0) { | ||
return snprintf(b, size, "-%u", d.seconds); | ||
} | ||
return snprintf(b, size, "%u", d.seconds); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* csnippets (c) 2019-2023 He Xian <[email protected]> | ||
* This code is licensed under MIT license (see LICENSE for details) */ | ||
|
||
#ifndef FORMATS_H | ||
#define FORMATS_H | ||
|
||
#include <stddef.h> | ||
#include <stdint.h> | ||
|
||
int format_iec_bytes(char *buf, size_t bufsize, double value); | ||
|
||
struct duration { | ||
signed int sign; /* +1 or -1, 0 is null, otherwise undefined */ | ||
unsigned int days; | ||
unsigned int hours; | ||
unsigned int minutes; | ||
unsigned int seconds; | ||
unsigned int millis; | ||
unsigned int micros; | ||
unsigned int nanos; | ||
}; | ||
|
||
struct duration make_duration(double seconds); | ||
struct duration make_duration_nanos(int64_t nanos); | ||
|
||
int format_duration_seconds(char *b, size_t size, struct duration d); | ||
|
||
#endif /* FORMATS_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters