9
9
10
10
#include <sys/time.h>
11
11
12
+ #ifdef __wasilibc_use_wasip2
13
+ #include <wasi/wasip2.h>
14
+ #else
12
15
#include <wasi/api.h>
16
+ #endif
13
17
#include <stdbool.h>
14
18
#include <time.h>
15
19
16
20
#define NSEC_PER_SEC 1000000000
17
21
18
22
static inline bool timespec_to_timestamp_exact (
19
- const struct timespec * timespec , __wasi_timestamp_t * timestamp ) {
23
+ #ifdef __wasilibc_use_wasip2
24
+ const struct timespec * timespec , wall_clock_datetime_t * timestamp ) {
25
+ #else
26
+ const struct timespec * timespec , __wasi_timestamp_t * timestamp ) {
27
+ #endif
20
28
// Invalid nanoseconds field.
21
29
if (timespec -> tv_nsec < 0 || timespec -> tv_nsec >= NSEC_PER_SEC )
22
30
return false;
@@ -25,39 +33,86 @@ static inline bool timespec_to_timestamp_exact(
25
33
if (timespec -> tv_sec < 0 )
26
34
return false;
27
35
36
+ #ifdef __wasilibc_use_wasip2
37
+ timestamp -> seconds = timespec -> tv_sec ;
38
+ timestamp -> nanoseconds = timespec -> tv_nsec ;
39
+ return true;
40
+ #else
28
41
// Make sure our timestamp does not overflow.
29
42
return !__builtin_mul_overflow (timespec -> tv_sec , NSEC_PER_SEC , timestamp ) &&
30
43
!__builtin_add_overflow (* timestamp , timespec -> tv_nsec , timestamp );
44
+ #endif
31
45
}
32
46
33
47
static inline bool timespec_to_timestamp_clamp (
34
- const struct timespec * timespec , __wasi_timestamp_t * timestamp ) {
48
+ #ifdef __wasilibc_use_wasip2
49
+ const struct timespec * timespec , wall_clock_datetime_t * timestamp ) {
50
+ #else
51
+ const struct timespec * timespec , __wasi_timestamp_t * timestamp ) {
52
+ #endif
35
53
// Invalid nanoseconds field.
36
54
if (timespec -> tv_nsec < 0 || timespec -> tv_nsec >= NSEC_PER_SEC )
37
55
return false;
38
56
39
57
if (timespec -> tv_sec < 0 ) {
40
58
// Timestamps before the Epoch are not supported.
41
- * timestamp = 0 ;
59
+ #if __wasilibc_use_wasip2
60
+ timestamp -> seconds = 0 ;
61
+ timestamp -> nanoseconds = 0 ;
62
+ } else {
63
+ timestamp -> seconds = timespec -> tv_sec ;
64
+ timestamp -> nanoseconds = timespec -> tv_nsec ;
65
+ #else
66
+ * timestamp = 0 ;
42
67
} else if (__builtin_mul_overflow (timespec -> tv_sec , NSEC_PER_SEC , timestamp ) ||
43
68
__builtin_add_overflow (* timestamp , timespec -> tv_nsec , timestamp )) {
44
69
// Make sure our timestamp does not overflow.
45
70
* timestamp = NUMERIC_MAX (__wasi_timestamp_t );
71
+ #endif
46
72
}
47
73
return true;
48
74
}
49
75
76
+ #ifdef __wasilibc_use_wasip2
77
+ static inline struct timespec timestamp_to_timespec (
78
+ wall_clock_datetime_t * timestamp ) {
79
+ return (struct timespec ){.tv_sec = timestamp -> seconds ,
80
+ .tv_nsec = timestamp -> nanoseconds };
81
+ }
82
+ #else
50
83
static inline struct timespec timestamp_to_timespec (
51
- __wasi_timestamp_t timestamp ) {
84
+ __wasi_timestamp_t timestamp ) {
52
85
// Decompose timestamp into seconds and nanoseconds.
53
86
return (struct timespec ){.tv_sec = timestamp / NSEC_PER_SEC ,
54
87
.tv_nsec = timestamp % NSEC_PER_SEC };
55
88
}
89
+ #endif
90
+
91
+ #ifdef __wasilibc_use_wasip2
92
+ static inline struct timespec instant_to_timespec (
93
+ monotonic_clock_instant_t ns ) {
94
+ // Decompose instant into seconds and nanoseconds
95
+ return (struct timespec ){.tv_sec = ns / NSEC_PER_SEC ,
96
+ .tv_nsec = ns % NSEC_PER_SEC };
97
+ }
98
+
99
+ static inline struct timeval instant_to_timeval (
100
+ monotonic_clock_instant_t ns ) {
101
+ // Decompose instant into seconds and microoseconds
102
+ return (struct timeval ){.tv_sec = ns / 1000 ,
103
+ .tv_usec = ns % 1000 };
104
+ }
56
105
57
106
static inline struct timeval timestamp_to_timeval (
58
- __wasi_timestamp_t timestamp ) {
107
+ wall_clock_datetime_t * timestamp ) {
108
+ return (struct timeval ){.tv_sec = timestamp -> seconds ,
109
+ .tv_usec = timestamp -> nanoseconds / 1000 };
110
+ }
111
+ #else
112
+ static inline struct timeval timestamp_to_timeval (
113
+ __wasi_timestamp_t timestamp ) {
59
114
struct timespec ts = timestamp_to_timespec (timestamp );
60
115
return (struct timeval ){.tv_sec = ts .tv_sec , ts .tv_nsec / 1000 };
61
116
}
62
-
117
+ #endif
63
118
#endif
0 commit comments