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