1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
/*
* Copyright (c) 2017 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file parc_Time.h
* @ingroup inputoutput
* @brief Time Manipulation
*
* Different platforms have different ways to express time-of-day, elapsed-time, clock-time.
* In some cases multiple ways to express the same semantic value have evolved over time,
* for example `struct timeval` and `struct timespec`.
*
*/
#ifndef libparc_parc_Time_h
#define libparc_parc_Time_h
#include <sys/time.h>
#include <stdint.h>
/**
* Create a nul-terminated C string containing the formatted representation of a `struct timeval`.
*
* The minimum length of the result is 8 characters consisting of the `struct timeval`
* formatted as a decimal string consisting of the number of seconds since midnight (0 hour), January 1, 1970.
*
* @param [in] timeval The instance of the struct `timeval` to convert to a C string.
* @return An allocated, null-terminated C string that must be freed via {@link parcMemory_Deallocate()}.
*
* Example:
* @code
* {
* struct timeval timeval;
* gettimeofday(&timeval, NULL);
*
* char *string = parcTime_TimevalAsString(timeval);
*
* parcMemory_Deallocate(&string);
* }
* @endcode
*/
char *parcTime_TimevalAsString(struct timeval timeval);
/**
* Format an ISO8601 date from the given struct timeval into the given character array, @p result.
*
* @param [in] utcTime A pointer to a valid struct timeval instance with the time in UTC.
* @param [out] result A pointer to the 64 element nul-terminated character array, @p result.
*
* @return The same value as @p result.
*
* Example:
* @code
* {
* struct timeval theTime;
* gettimeofday(&theTime, NULL);
*
* char result[64];
* parcTime_TimevalAsISO8601(&theTime, result);
*
* }
* @endcode
*/
char *parcTime_TimevalAsISO8601(const struct timeval *utcTime, char *result);
/**
* Format an RFC3339 compliant date from the given struct timeval into the given character array.
*
* @param [in] utcTime A pointer to a valid struct timeval instance with the time in UTC.
* @param [out] result A pointer to the 64 element nul-terminated character array, @p result.
*
* @return The same value as @p result.
*
* Example:
* @code
* {
* struct timeval theTime;
* gettimeofday(&theTime, NULL);
*
* char result[64];
* parcTime_TimevalAsRFC3339(&theTime, result);
*
* }
* @endcode
*/
char *parcTime_TimevalAsRFC3339(const struct timeval *utcTime, char *result);
/**
* Format an ISO8601 date from the given `time_t` value into the given character array, @p result.
*
* @param [in] utcTime `time_t` value representing the time in UTC.
* @param [out] result A pointer to the 64 element nul-terminated character array, @p result.
*
* @return The same value as @p result.
*
* Example:
* @code
* {
* time_t theTime = time(0);
*
* char result[64];
* parcTime_TimeAsISO8601(theTime, result);
*
* }
* @endcode
*/
char *parcTime_TimeAsISO8601(const time_t utcTime, char *result);
/**
* Format the current time as an ISO8601 date into the given character array, @p result.
*
* @param [out] result A pointer to the 64 element nul-terminated character array, @p result.
*
* @return The same value as @p result.
*
* Example:
* @code
* {
* char result[64];
* parcTime_NowAsISO8601(theTime, result);
*
* }
* @endcode
*/
char *parcTime_NowAsISO8601(char *result);
/**
* Format an RFC3339 compliant date from the given `time_t` value into the given character array.
*
* @param [in] utcTime `time_t` value of the time in UTC.
* @param [out] result A pointer to the 64 element nul-terminated character array, @p result.
*
* @return The same value as @p result.
*
* Example:
* @code
* {
* struct timeval theTime;
* time_t theTime = time(0);
*
* char result[64];
* parcTime_TimeAsRFC3339(&theTime, result);
*
* }
* @endcode
*/
char *parcTime_TimeAsRFC3339(const time_t utcTime, char *result);
/**
* Format the current time as an RFC3339 compliant date into the given character array.
*
* @param [out] result A pointer to the 64 element nul-terminated character array, @p result.
*
* @return The same value as @p result.
*
* Example:
* @code
* {
* char result[64];
* parcTime_NowAsRFC3339(&theTime, result);
*
* }
* @endcode
*/
char *parcTime_NowAsRFC3339(char *result);
/**
* Add two `struct timeval` values together.
*
* @param [in] addend1 The first value.
* @param [in] addend2 The second value.
*
* @return The sum of the first and second values.
*
* Example:
* @code
* {
* struct timeval now;
* gettimeofday(&now, NULL);
*
* struct timeval theEnd = parcTime_TimevalAdd(&now, timeout);
* }
* @endcode
*/
struct timeval parcTime_TimevalAdd(const struct timeval *addend1, const struct timeval *addend2);
/**
* Subtract two `struct timeval` values.
*
* @param [in] minuend The number from which the subtrahend is to be subtracted.
* @param [in] subtrahend The subtrahend.
*
* @return The difference between the first and second values.
*
* Example:
* @code
* {
* struct timeval now;
* gettimeofday(&now, NULL);
*
* struct timeval result = parcTime_TimevalSubtract(&now, timeout);
* }
* @endcode
*/
struct timeval parcTime_TimevalSubtract(const struct timeval *minuend, const struct timeval *subtrahend);
/**
* The current time as a `struct timeval`.
*
* @return The current time as a `struct timeval`.
*
* Example:
* @code
* {
*
* struct timeval now = parcTime_NowTimeval();
* }
* @endcode
*/
struct timeval parcTime_NowTimeval(void);
/**
* The current time in microseconds since midnight (0 hour), January 1, 1970 as a `uint64_t`.
*
* @return The current time in microseconds since midnight (0 hour), January 1, 1970 as a `uint64_t`.
*
* Example:
* @code
* {
*
* uint64_t now = parcTime_NowMicroseconds();
* }
* @endcode
*/
uint64_t parcTime_NowMicroseconds(void);
/**
* The current time in nanoseconds since midnight (0 hour), January 1, 1970 as a `uint64_t`.
*
* @return The current time in nanoseconds since midnight (0 hour), January 1, 1970 as a `uint64_t`.
*
* Example:
* @code
* {
*
* uint64_t now = parcTime_NowNanoseconds();
* }
* @endcode
*/
uint64_t parcTime_NowNanoseconds(void);
#endif // libparc_parc_Time_h
|