Skip to content

Commit 78ee7c9

Browse files
committed
Move time-related utils from util.h to time-util.h
Signed-off-by: Arnaud Rebillout <[email protected]>
1 parent cdbfbf9 commit 78ee7c9

File tree

2 files changed

+40
-40
lines changed

2 files changed

+40
-40
lines changed

src/time-util.h

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
#ifndef footimeutilfoo
44
#define footimeutilfoo
55

6+
#include <stdint.h>
7+
#include <time.h>
8+
69
#define NSEC_INFINITY ((uint64_t) -1)
710

8-
//#define NSEC_PER_SEC ((uint64_t) UINT64_C(1000000000)) already defined in util.h
11+
#define NSEC_PER_SEC ((uint64_t) UINT64_C(1000000000))
912
#define NSEC_PER_MSEC ((uint64_t) UINT64_C(1000000))
1013
#define NSEC_PER_USEC ((uint64_t) UINT64_C(1000))
1114

@@ -16,6 +19,42 @@
1619
#define NSEC_PER_MONTH ((uint64_t) (UINT64_C(2629800)*NSEC_PER_SEC))
1720
#define NSEC_PER_YEAR ((uint64_t) (UINT64_C(31557600)*NSEC_PER_SEC))
1821

22+
static inline uint64_t timespec_to_nsec(struct timespec t) {
23+
24+
if (t.tv_sec == (time_t) -1 &&
25+
t.tv_nsec == (long) -1)
26+
return UINT64_MAX;
27+
28+
return (uint64_t) t.tv_sec * UINT64_C(1000000000) + (uint64_t) t.tv_nsec;
29+
}
30+
31+
static inline struct timespec nsec_to_timespec(uint64_t u) {
32+
33+
if (u == UINT64_MAX)
34+
return (struct timespec) {
35+
.tv_sec = (time_t) -1,
36+
.tv_nsec = (long) -1,
37+
};
38+
39+
return (struct timespec) {
40+
.tv_sec = u / UINT64_C(1000000000),
41+
.tv_nsec = u % UINT64_C(1000000000)
42+
};
43+
}
44+
45+
#define NSEC_TO_TIMESPEC_INIT(u) \
46+
{ .tv_sec = u == UINT64_MAX ? (time_t) -1 : (time_t) (u / UINT64_C(1000000000)), \
47+
.tv_nsec = u == UINT64_MAX ? (long) -1 : (long) (u % UINT64_C(1000000000)) }
48+
49+
static inline uint64_t now(clockid_t id) {
50+
struct timespec ts;
51+
52+
if (clock_gettime(id, &ts) < 0)
53+
return UINT64_MAX;
54+
55+
return timespec_to_nsec(ts);
56+
}
57+
1958
char *format_timespan(char *buf, size_t l, uint64_t t, uint64_t accuracy);
2059

2160
#endif

src/util.h

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include <string.h>
1515
#include <sys/types.h>
1616
#include <sys/vfs.h>
17-
#include <time.h>
1817
#include <unistd.h>
1918

2019
#include <linux/btrfs.h>
@@ -66,42 +65,6 @@
6665

6766

6867

69-
static inline uint64_t timespec_to_nsec(struct timespec t) {
70-
71-
if (t.tv_sec == (time_t) -1 &&
72-
t.tv_nsec == (long) -1)
73-
return UINT64_MAX;
74-
75-
return (uint64_t) t.tv_sec * UINT64_C(1000000000) + (uint64_t) t.tv_nsec;
76-
}
77-
78-
static inline struct timespec nsec_to_timespec(uint64_t u) {
79-
80-
if (u == UINT64_MAX)
81-
return (struct timespec) {
82-
.tv_sec = (time_t) -1,
83-
.tv_nsec = (long) -1,
84-
};
85-
86-
return (struct timespec) {
87-
.tv_sec = u / UINT64_C(1000000000),
88-
.tv_nsec = u % UINT64_C(1000000000)
89-
};
90-
}
91-
92-
#define NSEC_TO_TIMESPEC_INIT(u) \
93-
{ .tv_sec = u == UINT64_MAX ? (time_t) -1 : (time_t) (u / UINT64_C(1000000000)), \
94-
.tv_nsec = u == UINT64_MAX ? (long) -1 : (long) (u % UINT64_C(1000000000)) }
95-
96-
static inline uint64_t now(clockid_t id) {
97-
struct timespec ts;
98-
99-
if (clock_gettime(id, &ts) < 0)
100-
return UINT64_MAX;
101-
102-
return timespec_to_nsec(ts);
103-
}
104-
10568
int loop_write(int fd, const void *p, size_t l);
10669
int loop_write_block(int fd, const void *p, size_t l);
10770
ssize_t loop_read(int fd, void *p, size_t l);
@@ -769,8 +732,6 @@ struct fsxattr {
769732
#define FS_IOC_FSSETXATTR _IOW ('X', 32, struct fsxattr)
770733
#endif
771734

772-
#define NSEC_PER_SEC (UINT64_C(1000000000))
773-
774735
/* Cleanup functions */
775736

776737
#define _cleanup_(x) __attribute__((cleanup(x)))

0 commit comments

Comments
 (0)