Skip to content

Commit e955f76

Browse files
committed
format_timespan: work with nsec instead of usec, remove unused USEC_* symbols
Lennart: Hmm, let's stick to uint64_t as type in casync, and only deal with ns as unit. Patching through format_timespan() fixing up the factor sounds easy enough to me. Yes, this means systemd and casync will deviate from each other in this case, but I think that's fine, if we do it for a reason. In casync we mostly deal with ns, since we mostly deal with file system objects, and Linux exposes ns granularity for those, hence I think it is a good idea to standardize on ns time units. In systemd that's a bit different though... Signed-off-by: Arnaud Rebillout <[email protected]>
1 parent e418295 commit e955f76

File tree

2 files changed

+17
-26
lines changed

2 files changed

+17
-26
lines changed

src/time-util.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
66
char *format_timespan(char *buf, size_t l, uint64_t t, uint64_t accuracy) {
77
static const struct {
88
const char *suffix;
9-
uint64_t usec;
9+
uint64_t nsec;
1010
} table[] = {
11-
{ "y", USEC_PER_YEAR },
12-
{ "month", USEC_PER_MONTH },
13-
{ "w", USEC_PER_WEEK },
14-
{ "d", USEC_PER_DAY },
15-
{ "h", USEC_PER_HOUR },
16-
{ "min", USEC_PER_MINUTE },
17-
{ "s", USEC_PER_SEC },
18-
{ "ms", USEC_PER_MSEC },
19-
{ "us", 1 },
11+
{ "y", NSEC_PER_YEAR },
12+
{ "month", NSEC_PER_MONTH },
13+
{ "w", NSEC_PER_WEEK },
14+
{ "d", NSEC_PER_DAY },
15+
{ "h", NSEC_PER_HOUR },
16+
{ "min", NSEC_PER_MINUTE },
17+
{ "s", NSEC_PER_SEC },
18+
{ "ms", NSEC_PER_MSEC },
19+
{ "us", NSEC_PER_USEC },
20+
{ "ns", 1 },
2021
};
2122

2223
size_t i;
@@ -26,7 +27,7 @@ char *format_timespan(char *buf, size_t l, uint64_t t, uint64_t accuracy) {
2627
assert(buf);
2728
assert(l > 0);
2829

29-
if (t == USEC_INFINITY) {
30+
if (t == NSEC_INFINITY) {
3031
strncpy(p, "infinity", l-1);
3132
p[l-1] = 0;
3233
return p;
@@ -52,22 +53,22 @@ char *format_timespan(char *buf, size_t l, uint64_t t, uint64_t accuracy) {
5253
if (t < accuracy && something)
5354
break;
5455

55-
if (t < table[i].usec)
56+
if (t < table[i].nsec)
5657
continue;
5758

5859
if (l <= 1)
5960
break;
6061

61-
a = t / table[i].usec;
62-
b = t % table[i].usec;
62+
a = t / table[i].nsec;
63+
b = t % table[i].nsec;
6364

6465
/* Let's see if we should shows this in dot notation */
65-
if (t < USEC_PER_MINUTE && b > 0) {
66+
if (t < NSEC_PER_MINUTE && b > 0) {
6667
uint64_t cc;
6768
signed char j;
6869

6970
j = 0;
70-
for (cc = table[i].usec; cc > 1; cc /= 10)
71+
for (cc = table[i].nsec; cc > 1; cc /= 10)
7172
j++;
7273

7374
for (cc = accuracy; cc > 1; cc /= 10) {

src/time-util.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,17 @@
33
#ifndef footimeutilfoo
44
#define footimeutilfoo
55

6-
#define USEC_INFINITY ((uint64_t) -1)
76
#define NSEC_INFINITY ((uint64_t) -1)
87

9-
#define MSEC_PER_SEC 1000ULL
10-
#define USEC_PER_SEC ((uint64_t) 1000000ULL)
11-
#define USEC_PER_MSEC ((uint64_t) 1000ULL)
128
//#define NSEC_PER_SEC ((uint64_t) 1000000000ULL) already defined in util.h
139
#define NSEC_PER_MSEC ((uint64_t) 1000000ULL)
1410
#define NSEC_PER_USEC ((uint64_t) 1000ULL)
1511

16-
#define USEC_PER_MINUTE ((uint64_t) (60ULL*USEC_PER_SEC))
1712
#define NSEC_PER_MINUTE ((uint64_t) (60ULL*NSEC_PER_SEC))
18-
#define USEC_PER_HOUR ((uint64_t) (60ULL*USEC_PER_MINUTE))
1913
#define NSEC_PER_HOUR ((uint64_t) (60ULL*NSEC_PER_MINUTE))
20-
#define USEC_PER_DAY ((uint64_t) (24ULL*USEC_PER_HOUR))
2114
#define NSEC_PER_DAY ((uint64_t) (24ULL*NSEC_PER_HOUR))
22-
#define USEC_PER_WEEK ((uint64_t) (7ULL*USEC_PER_DAY))
2315
#define NSEC_PER_WEEK ((uint64_t) (7ULL*NSEC_PER_DAY))
24-
#define USEC_PER_MONTH ((uint64_t) (2629800ULL*USEC_PER_SEC))
2516
#define NSEC_PER_MONTH ((uint64_t) (2629800ULL*NSEC_PER_SEC))
26-
#define USEC_PER_YEAR ((uint64_t) (31557600ULL*USEC_PER_SEC))
2717
#define NSEC_PER_YEAR ((uint64_t) (31557600ULL*NSEC_PER_SEC))
2818

2919
char *format_timespan(char *buf, size_t l, uint64_t t, uint64_t accuracy);

0 commit comments

Comments
 (0)