Debian Codegraph @

crt/aws-c-common/include/aws/common/date_time.h

raw symbols SCIP
1 #ifndef AWS_COMMON_DATE_TIME_H
2 #define AWS_COMMON_DATE_TIME_H
3 /**
4 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: Apache-2.0.
6 */
7 #include <aws/common/common.h>
8
9 #include <time.h>
10
11 AWS_PUSH_SANE_WARNING_LEVEL
12
13 enum {
14 AWS_DATE_TIME_STR_MAX_LEN = 100,
15 AWS_DATE_TIME_STR_MAX_BASIC_LEN = 20,
16 };
17
18 struct aws_byte_buf;
19 struct aws_byte_cursor;
20
21 enum aws_date_format {
22 AWS_DATE_FORMAT_RFC822,
23 AWS_DATE_FORMAT_ISO_8601,
24 AWS_DATE_FORMAT_ISO_8601_BASIC,
25 AWS_DATE_FORMAT_AUTO_DETECT,
26 };
27
28 enum aws_date_month {
29 AWS_DATE_MONTH_JANUARY = 0,
30 AWS_DATE_MONTH_FEBRUARY,
31 AWS_DATE_MONTH_MARCH,
32 AWS_DATE_MONTH_APRIL,
33 AWS_DATE_MONTH_MAY,
34 AWS_DATE_MONTH_JUNE,
35 AWS_DATE_MONTH_JULY,
36 AWS_DATE_MONTH_AUGUST,
37 AWS_DATE_MONTH_SEPTEMBER,
38 AWS_DATE_MONTH_OCTOBER,
39 AWS_DATE_MONTH_NOVEMBER,
40 AWS_DATE_MONTH_DECEMBER,
41 };
42
43 enum aws_date_day_of_week {
44 AWS_DATE_DAY_OF_WEEK_SUNDAY = 0,
45 AWS_DATE_DAY_OF_WEEK_MONDAY,
46 AWS_DATE_DAY_OF_WEEK_TUESDAY,
47 AWS_DATE_DAY_OF_WEEK_WEDNESDAY,
48 AWS_DATE_DAY_OF_WEEK_THURSDAY,
49 AWS_DATE_DAY_OF_WEEK_FRIDAY,
50 AWS_DATE_DAY_OF_WEEK_SATURDAY,
51 };
52
53 struct aws_date_time {
54 time_t timestamp;
55 uint16_t milliseconds;
56 char tz[6];
57 struct tm gmt_time;
58 struct tm local_time;
59 bool utc_assumed;
60 };
61
62 AWS_EXTERN_C_BEGIN
63
64 /**
65 * Initializes dt to be the current system time.
66 */
67 AWS_COMMON_API void aws_date_time_init_now(struct aws_date_time *dt);
68
69 /**
70 * Initializes dt to be the time represented in milliseconds since unix epoch.
71 */
72 AWS_COMMON_API void aws_date_time_init_epoch_millis(struct aws_date_time *dt, uint64_t ms_since_epoch);
73
74 /**
75 * Initializes dt to be the time represented in seconds.millis since unix epoch.
76 */
77 AWS_COMMON_API void aws_date_time_init_epoch_secs(struct aws_date_time *dt, double sec_ms);
78
79 /**
80 * Initializes dt to be the time represented by date_str in format 'fmt'. Returns AWS_OP_SUCCESS if the
81 * string was successfully parsed, returns AWS_OP_ERR if parsing failed.
82 *
83 * The parser is lenient regarding AWS_DATE_FORMAT_ISO_8601 vs AWS_DATE_FORMAT_ISO_8601_BASIC.
84 * Regardless of which you pass in, both "2002-10-02T08:05:09Z" and "20021002T080509Z" would be accepted.
85 *
86 * Notes for AWS_DATE_FORMAT_RFC822:
87 * If no time zone information is provided, it is assumed to be local time (please don't do this).
88 *
89 * Only time zones indicating Universal Time (e.g. Z, UT, UTC, or GMT),
90 * or offsets from UTC (e.g. +0100, -0700), are accepted.
91 *
92 * Really, it's just better if you always use Universal Time.
93 */
94 AWS_COMMON_API int aws_date_time_init_from_str(
95 struct aws_date_time *dt,
96 const struct aws_byte_buf *date_str,
97 enum aws_date_format fmt);
98
99 /**
100 * aws_date_time_init variant that takes a byte_cursor rather than a byte_buf
101 */
102 AWS_COMMON_API int aws_date_time_init_from_str_cursor(
103 struct aws_date_time *dt,
104 const struct aws_byte_cursor *date_str_cursor,
105 enum aws_date_format fmt);
106
107 /**
108 * Copies the current time as a formatted date string in local time into output_buf. If buffer is too small, it will
109 * return AWS_OP_ERR. A good size suggestion is AWS_DATE_TIME_STR_MAX_LEN bytes. AWS_DATE_FORMAT_AUTO_DETECT is not
110 * allowed.
111 */
112 AWS_COMMON_API int aws_date_time_to_local_time_str(
113 const struct aws_date_time *dt,
114 enum aws_date_format fmt,
115 struct aws_byte_buf *output_buf);
116
117 /**
118 * Copies the current time as a formatted date string in utc time into output_buf. If buffer is too small, it will
119 * return AWS_OP_ERR. A good size suggestion is AWS_DATE_TIME_STR_MAX_LEN bytes. AWS_DATE_FORMAT_AUTO_DETECT is not
120 * allowed.
121 */
122 AWS_COMMON_API int aws_date_time_to_utc_time_str(
123 const struct aws_date_time *dt,
124 enum aws_date_format fmt,
125 struct aws_byte_buf *output_buf);
126
127 /**
128 * Copies the current time as a formatted short date string in local time into output_buf. If buffer is too small, it
129 * will return AWS_OP_ERR. A good size suggestion is AWS_DATE_TIME_STR_MAX_LEN bytes. AWS_DATE_FORMAT_AUTO_DETECT is not
130 * allowed.
131 */
132 AWS_COMMON_API int aws_date_time_to_local_time_short_str(
133 const struct aws_date_time *dt,
134 enum aws_date_format fmt,
135 struct aws_byte_buf *output_buf);
136
137 /**
138 * Copies the current time as a formatted short date string in utc time into output_buf. If buffer is too small, it will
139 * return AWS_OP_ERR. A good size suggestion is AWS_DATE_TIME_STR_MAX_LEN bytes. AWS_DATE_FORMAT_AUTO_DETECT is not
140 * allowed.
141 */
142 AWS_COMMON_API int aws_date_time_to_utc_time_short_str(
143 const struct aws_date_time *dt,
144 enum aws_date_format fmt,
145 struct aws_byte_buf *output_buf);
146
147 AWS_COMMON_API double aws_date_time_as_epoch_secs(const struct aws_date_time *dt);
148 AWS_COMMON_API uint64_t aws_date_time_as_nanos(const struct aws_date_time *dt);
149 AWS_COMMON_API uint64_t aws_date_time_as_millis(const struct aws_date_time *dt);
150 AWS_COMMON_API uint16_t aws_date_time_year(const struct aws_date_time *dt, bool local_time);
151 AWS_COMMON_API enum aws_date_month aws_date_time_month(const struct aws_date_time *dt, bool local_time);
152 AWS_COMMON_API uint8_t aws_date_time_month_day(const struct aws_date_time *dt, bool local_time);
153 AWS_COMMON_API enum aws_date_day_of_week aws_date_time_day_of_week(const struct aws_date_time *dt, bool local_time);
154 AWS_COMMON_API uint8_t aws_date_time_hour(const struct aws_date_time *dt, bool local_time);
155 AWS_COMMON_API uint8_t aws_date_time_minute(const struct aws_date_time *dt, bool local_time);
156 AWS_COMMON_API uint8_t aws_date_time_second(const struct aws_date_time *dt, bool local_time);
157 AWS_COMMON_API bool aws_date_time_dst(const struct aws_date_time *dt, bool local_time);
158
159 /**
160 * returns the difference of a and b (a - b) in seconds.
161 */
162 AWS_COMMON_API time_t aws_date_time_diff(const struct aws_date_time *a, const struct aws_date_time *b);
163
164 AWS_EXTERN_C_END
165 AWS_POP_SANE_WARNING_LEVEL
166
167 #endif /* AWS_COMMON_DATE_TIME_H */