GRU - Generic Reusable Utilities
Loading...
Searching...
No Matches
gru_time_utils.h
Go to the documentation of this file.
1/*
2 Copyright 2016 Otavio Rodolfo Piske
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16#ifndef GRU_TIME_UTILS_H
17#define GRU_TIME_UTILS_H
18
19#include <ctype.h>
20#include <inttypes.h>
21#include <math.h>
22#include <stdint.h>
23#include <stdlib.h>
24#include <time.h>
25
26#if !defined(_WIN32) && !defined(_WIN64)
27#include <sys/time.h>
28#include <unistd.h>
29#else
30#include <windows.h>
31
33#endif
34
35#include "common/gru_alloc.h"
36#include "common/gru_portable.h"
37#include "gru_localtime.h"
38
39#ifdef __cplusplus
40extern "C" {
41#endif
42
44#define GRU_TS_STRING_SEC_MAX 16
45
47#define GRU_TS_STRING_USEC_MAX 7
48
50#define GRU_TS_STRING_MAX GRU_TS_STRING_SEC_MAX + GRU_TS_STRING_USEC_MAX + 1
51
55typedef struct timeval gru_timestamp_t;
56
63
69gru_export void gru_time_add_seconds(gru_timestamp_t *t, uint64_t count);
70
76gru_export void gru_time_add_minutes(gru_timestamp_t *t, uint64_t count);
77
83gru_export void gru_time_add_hours(gru_timestamp_t *t, uint64_t count);
84
90gru_export void gru_time_add_days(gru_timestamp_t *t, uint64_t count);
91
101
108
109
119 const char *format,
120 gru_status_t *status);
121
127
133
141
148
155
164
165
174
175#ifdef __cplusplus
176}
177#endif
178
179#endif /* GRU_TIME_UTILS_H */
#define gru_export
Definition: gru_portable.h:19
gru_export int64_t gru_time_elapsed_secs(gru_timestamp_t start, gru_timestamp_t end)
Calculates the elapsed time between two timestamps.
Definition: gru_time_utils.c:154
gru_export gru_timestamp_t gru_time_from_milli_char(const char *ts)
Converts an unformatted string of milliseconds since epoch to a timestamp.
Definition: gru_time_utils.c:141
gru_export char * gru_time_write_str(const gru_timestamp_t *t)
Write a timeval structure to a string.
Definition: gru_time_utils.c:80
struct timeval gru_timestamp_t
An alias to struct timeval.
Definition: gru_time_utils.h:55
gru_export void gru_time_add_days(gru_timestamp_t *t, uint64_t count)
Add days to a timestamp object.
Definition: gru_time_utils.c:47
gru_export uint64_t gru_time_now_milli()
Gets the current moment in time in milliseconds.
Definition: gru_time_utils.c:122
gru_export int64_t gru_time_elapsed_milli(gru_timestamp_t start, gru_timestamp_t end)
Calculates the elapsed time between two timestamps.
Definition: gru_time_utils.c:162
gru_export void gru_time_add_hours(gru_timestamp_t *t, uint64_t count)
Add hours to a timestamp object.
Definition: gru_time_utils.c:43
gru_export char * gru_time_write_format(const gru_timestamp_t *t, const char *format, gru_status_t *status)
Writes the input timestamp value according to the given format.
Definition: gru_time_utils.c:95
gru_export gru_timestamp_t gru_time_now()
Gets the current moment in time.
Definition: gru_time_utils.c:115
gru_export gru_timestamp_t gru_time_read_str(const char *str)
Read a string in the format seconds.microseconds and return a struct timeval.
Definition: gru_time_utils.c:51
gru_export gru_timestamp_t gru_time_from_milli(int64_t timestamp)
Converts milliseconds since epoch to a timestamp.
Definition: gru_time_utils.c:128
gru_export int64_t gru_time_to_milli(const gru_timestamp_t *ts)
Converts a timestamp to milliseconds.
Definition: gru_time_utils.c:147
gru_export void gru_time_add_minutes(gru_timestamp_t *t, uint64_t count)
Add minutes to a timestamp object.
Definition: gru_time_utils.c:39
gru_export void gru_time_add_microseconds(gru_timestamp_t *t, uint64_t count)
Add microseconds to a timestamp object.
Definition: gru_time_utils.c:18
gru_export void gru_time_add_seconds(gru_timestamp_t *t, uint64_t count)
Add seconds to a timestamp object.
Definition: gru_time_utils.c:31
Status type.
Definition: gru_status.h:47