| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <aws/common/date_time.h> |
| 6 |
|
| 7 |
#include <aws/common/array_list.h> |
| 8 |
#include <aws/common/byte_buf.h> |
| 9 |
#include <aws/common/byte_order.h> |
| 10 |
#include <aws/common/clock.h> |
| 11 |
#include <aws/common/string.h> |
| 12 |
#include <aws/common/time.h> |
| 13 |
|
| 14 |
#include <ctype.h> |
| 15 |
#include <math.h> |
| 16 |
|
| 17 |
static const char *RFC822_DATE_FORMAT_STR_MINUS_Z = "%a, %d %b %Y %H:%M:%S GMT"; |
| 18 |
static const char *RFC822_DATE_FORMAT_STR_WITH_Z = "%a, %d %b %Y %H:%M:%S %Z"; |
| 19 |
static const char *RFC822_SHORT_DATE_FORMAT_STR = "%a, %d %b %Y"; |
| 20 |
static const char *ISO_8601_LONG_DATE_FORMAT_STR = "%Y-%m-%dT%H:%M:%SZ"; |
| 21 |
static const char *ISO_8601_SHORT_DATE_FORMAT_STR = "%Y-%m-%d"; |
| 22 |
static const char *ISO_8601_LONG_BASIC_DATE_FORMAT_STR = "%Y%m%dT%H%M%SZ"; |
| 23 |
static const char *ISO_8601_SHORT_BASIC_DATE_FORMAT_STR = "%Y%m%d"; |
| 24 |
|
| 25 |
#define STR_TRIPLET_TO_INDEX(str) \ |
| 26 |
(((uint32_t)tolower((uint8_t)((str)[0])) << 0) | ((uint32_t)tolower((uint8_t)((str)[1])) << 8) | \ |
| 27 |
((uint32_t)tolower((uint8_t)((str)[2])) << 16)) |
| 28 |
|
| 29 |
static uint32_t s_jan = 0; |
| 30 |
static uint32_t s_feb = 0; |
| 31 |
static uint32_t s_mar = 0; |
| 32 |
static uint32_t s_apr = 0; |
| 33 |
static uint32_t s_may = 0; |
| 34 |
static uint32_t s_jun = 0; |
| 35 |
static uint32_t s_jul = 0; |
| 36 |
static uint32_t s_aug = 0; |
| 37 |
static uint32_t s_sep = 0; |
| 38 |
static uint32_t s_oct = 0; |
| 39 |
static uint32_t s_nov = 0; |
| 40 |
static uint32_t s_dec = 0; |
| 41 |
|
| 42 |
static uint32_t s_utc = 0; |
| 43 |
static uint32_t s_gmt = 0; |
| 44 |
|
| 45 |
static void s_check_init_str_to_int(void) { |
| 46 |
if (!s_jan) { |
| 47 |
s_jan = STR_TRIPLET_TO_INDEX("jan"); |
| 48 |
s_feb = STR_TRIPLET_TO_INDEX("feb"); |
| 49 |
s_mar = STR_TRIPLET_TO_INDEX("mar"); |
| 50 |
s_apr = STR_TRIPLET_TO_INDEX("apr"); |
| 51 |
s_may = STR_TRIPLET_TO_INDEX("may"); |
| 52 |
s_jun = STR_TRIPLET_TO_INDEX("jun"); |
| 53 |
s_jul = STR_TRIPLET_TO_INDEX("jul"); |
| 54 |
s_aug = STR_TRIPLET_TO_INDEX("aug"); |
| 55 |
s_sep = STR_TRIPLET_TO_INDEX("sep"); |
| 56 |
s_oct = STR_TRIPLET_TO_INDEX("oct"); |
| 57 |
s_nov = STR_TRIPLET_TO_INDEX("nov"); |
| 58 |
s_dec = STR_TRIPLET_TO_INDEX("dec"); |
| 59 |
s_utc = STR_TRIPLET_TO_INDEX("utc"); |
| 60 |
s_gmt = STR_TRIPLET_TO_INDEX("gmt"); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
static int get_month_number_from_str(const char *time_string, size_t start_index, size_t stop_index) { |
| 66 |
s_check_init_str_to_int(); |
| 67 |
|
| 68 |
if (stop_index - start_index < 3) { |
| 69 |
return -1; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
uint32_t comp_val = STR_TRIPLET_TO_INDEX(time_string + start_index); |
| 74 |
|
| 75 |
|
| 76 |
if (s_jan == comp_val) { |
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
if (s_feb == comp_val) { |
| 81 |
return 1; |
| 82 |
} |
| 83 |
|
| 84 |
if (s_mar == comp_val) { |
| 85 |
return 2; |
| 86 |
} |
| 87 |
|
| 88 |
if (s_apr == comp_val) { |
| 89 |
return 3; |
| 90 |
} |
| 91 |
|
| 92 |
if (s_may == comp_val) { |
| 93 |
return 4; |
| 94 |
} |
| 95 |
|
| 96 |
if (s_jun == comp_val) { |
| 97 |
return 5; |
| 98 |
} |
| 99 |
|
| 100 |
if (s_jul == comp_val) { |
| 101 |
return 6; |
| 102 |
} |
| 103 |
|
| 104 |
if (s_aug == comp_val) { |
| 105 |
return 7; |
| 106 |
} |
| 107 |
|
| 108 |
if (s_sep == comp_val) { |
| 109 |
return 8; |
| 110 |
} |
| 111 |
|
| 112 |
if (s_oct == comp_val) { |
| 113 |
return 9; |
| 114 |
} |
| 115 |
|
| 116 |
if (s_nov == comp_val) { |
| 117 |
return 10; |
| 118 |
} |
| 119 |
|
| 120 |
if (s_dec == comp_val) { |
| 121 |
return 11; |
| 122 |
} |
| 123 |
|
| 124 |
return -1; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
static bool is_utc_time_zone(const char *str) { |
| 129 |
s_check_init_str_to_int(); |
| 130 |
|
| 131 |
size_t len = strlen(str); |
| 132 |
|
| 133 |
if (len > 0) { |
| 134 |
if (tolower((uint8_t)str[0]) == 'z') { |
| 135 |
return true; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
if (len == 5 && (str[0] == '+' || str[0] == '-')) { |
| 140 |
return true; |
| 141 |
} |
| 142 |
|
| 143 |
if (len == 2) { |
| 144 |
return tolower((uint8_t)str[0]) == 'u' && tolower((uint8_t)str[1]) == 't'; |
| 145 |
} |
| 146 |
|
| 147 |
if (len < 3) { |
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
uint32_t comp_val = STR_TRIPLET_TO_INDEX(str); |
| 152 |
|
| 153 |
if (comp_val == s_utc || comp_val == s_gmt) { |
| 154 |
return true; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
return false; |
| 159 |
} |
| 160 |
|
| 161 |
struct tm s_get_time_struct(struct aws_date_time *dt, bool local_time) { |
| 162 |
struct tm time; |
| 163 |
AWS_ZERO_STRUCT(time); |
| 164 |
if (local_time) { |
| 165 |
aws_localtime(dt->timestamp, &time); |
| 166 |
} else { |
| 167 |
aws_gmtime(dt->timestamp, &time); |
| 168 |
} |
| 169 |
|
| 170 |
return time; |
| 171 |
} |
| 172 |
|
| 173 |
void aws_date_time_init_now(struct aws_date_time *dt) { |
| 174 |
uint64_t current_time_ns = 0; |
| 175 |
aws_sys_clock_get_ticks(¤t_time_ns); |
| 176 |
aws_date_time_init_epoch_millis( |
| 177 |
dt, aws_timestamp_convert(current_time_ns, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_MILLIS, NULL)); |
| 178 |
} |
| 179 |
|
| 180 |
void aws_date_time_init_epoch_millis(struct aws_date_time *dt, uint64_t ms_since_epoch) { |
| 181 |
uint64_t milliseconds = 0; |
| 182 |
dt->timestamp = |
| 183 |
(time_t)aws_timestamp_convert(ms_since_epoch, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_SECS, &milliseconds); |
| 184 |
dt->milliseconds = (uint16_t)milliseconds; |
| 185 |
dt->gmt_time = s_get_time_struct(dt, false); |
| 186 |
dt->local_time = s_get_time_struct(dt, true); |
| 187 |
} |
| 188 |
|
| 189 |
void aws_date_time_init_epoch_secs(struct aws_date_time *dt, double sec_ms) { |
| 190 |
double integral = 0; |
| 191 |
dt->milliseconds = (uint16_t)(round(modf(sec_ms, &integral) * AWS_TIMESTAMP_MILLIS)); |
| 192 |
dt->timestamp = (time_t)integral; |
| 193 |
dt->gmt_time = s_get_time_struct(dt, false); |
| 194 |
dt->local_time = s_get_time_struct(dt, true); |
| 195 |
} |
| 196 |
|
| 197 |
enum parser_state { |
| 198 |
ON_WEEKDAY, |
| 199 |
ON_SPACE_DELIM, |
| 200 |
ON_YEAR, |
| 201 |
ON_MONTH, |
| 202 |
ON_MONTH_DAY, |
| 203 |
ON_HOUR, |
| 204 |
ON_MINUTE, |
| 205 |
ON_SECOND, |
| 206 |
ON_TZ, |
| 207 |
FINISHED, |
| 208 |
}; |
| 209 |
|
| 210 |
static bool s_parse_rfc_822( |
| 211 |
const struct aws_byte_cursor *date_str_cursor, |
| 212 |
struct tm *parsed_time, |
| 213 |
struct aws_date_time *dt) { |
| 214 |
size_t len = date_str_cursor->len; |
| 215 |
|
| 216 |
size_t index = 0; |
| 217 |
size_t state_start_index = 0; |
| 218 |
int state = ON_WEEKDAY; |
| 219 |
bool error = false; |
| 220 |
|
| 221 |
AWS_ZERO_STRUCT(*parsed_time); |
| 222 |
|
| 223 |
while (!error && index < len) { |
| 224 |
char c = (char)date_str_cursor->ptr[index]; |
| 225 |
|
| 226 |
switch (state) { |
| 227 |
|
| 228 |
case ON_WEEKDAY: |
| 229 |
if (c == ',') { |
| 230 |
state = ON_SPACE_DELIM; |
| 231 |
state_start_index = index + 1; |
| 232 |
} else if (aws_isdigit(c)) { |
| 233 |
state = ON_MONTH_DAY; |
| 234 |
} else if (!aws_isalpha(c)) { |
| 235 |
error = true; |
| 236 |
} |
| 237 |
break; |
| 238 |
case ON_SPACE_DELIM: |
| 239 |
if (aws_isspace(c)) { |
| 240 |
state = ON_MONTH_DAY; |
| 241 |
state_start_index = index + 1; |
| 242 |
} else { |
| 243 |
error = true; |
| 244 |
} |
| 245 |
break; |
| 246 |
case ON_MONTH_DAY: |
| 247 |
if (aws_isdigit(c)) { |
| 248 |
parsed_time->tm_mday = parsed_time->tm_mday * 10 + (c - '0'); |
| 249 |
} else if (aws_isspace(c)) { |
| 250 |
state = ON_MONTH; |
| 251 |
state_start_index = index + 1; |
| 252 |
} else { |
| 253 |
error = true; |
| 254 |
} |
| 255 |
break; |
| 256 |
case ON_MONTH: |
| 257 |
if (aws_isspace(c)) { |
| 258 |
int monthNumber = |
| 259 |
get_month_number_from_str((const char *)date_str_cursor->ptr, state_start_index, index + 1); |
| 260 |
|
| 261 |
if (monthNumber > -1) { |
| 262 |
state = ON_YEAR; |
| 263 |
state_start_index = index + 1; |
| 264 |
parsed_time->tm_mon = monthNumber; |
| 265 |
} else { |
| 266 |
error = true; |
| 267 |
} |
| 268 |
} else if (!aws_isalpha(c)) { |
| 269 |
error = true; |
| 270 |
} |
| 271 |
break; |
| 272 |
|
| 273 |
case ON_YEAR: |
| 274 |
if (aws_isspace(c) && index - state_start_index == 4) { |
| 275 |
state = ON_HOUR; |
| 276 |
state_start_index = index + 1; |
| 277 |
parsed_time->tm_year -= 1900; |
| 278 |
} else if (aws_isspace(c) && index - state_start_index == 2) { |
| 279 |
state = 5; |
| 280 |
state_start_index = index + 1; |
| 281 |
parsed_time->tm_year += 2000 - 1900; |
| 282 |
} else if (aws_isdigit(c)) { |
| 283 |
parsed_time->tm_year = parsed_time->tm_year * 10 + (c - '0'); |
| 284 |
} else { |
| 285 |
error = true; |
| 286 |
} |
| 287 |
break; |
| 288 |
case ON_HOUR: |
| 289 |
if (c == ':' && index - state_start_index == 2) { |
| 290 |
state = ON_MINUTE; |
| 291 |
state_start_index = index + 1; |
| 292 |
} else if (aws_isdigit(c)) { |
| 293 |
parsed_time->tm_hour = parsed_time->tm_hour * 10 + (c - '0'); |
| 294 |
} else { |
| 295 |
error = true; |
| 296 |
} |
| 297 |
break; |
| 298 |
case ON_MINUTE: |
| 299 |
if (c == ':' && index - state_start_index == 2) { |
| 300 |
state = ON_SECOND; |
| 301 |
state_start_index = index + 1; |
| 302 |
} else if (aws_isdigit(c)) { |
| 303 |
parsed_time->tm_min = parsed_time->tm_min * 10 + (c - '0'); |
| 304 |
} else { |
| 305 |
error = true; |
| 306 |
} |
| 307 |
break; |
| 308 |
case ON_SECOND: |
| 309 |
if (aws_isspace(c) && index - state_start_index == 2) { |
| 310 |
state = ON_TZ; |
| 311 |
state_start_index = index + 1; |
| 312 |
} else if (aws_isdigit(c)) { |
| 313 |
parsed_time->tm_sec = parsed_time->tm_sec * 10 + (c - '0'); |
| 314 |
} else { |
| 315 |
error = true; |
| 316 |
} |
| 317 |
break; |
| 318 |
case ON_TZ: |
| 319 |
if ((aws_isalnum(c) || c == '-' || c == '+') && (index - state_start_index) < 5) { |
| 320 |
dt->tz[index - state_start_index] = c; |
| 321 |
} else { |
| 322 |
error = true; |
| 323 |
} |
| 324 |
|
| 325 |
break; |
| 326 |
default: |
| 327 |
error = true; |
| 328 |
break; |
| 329 |
} |
| 330 |
|
| 331 |
index++; |
| 332 |
} |
| 333 |
|
| 334 |
if (dt->tz[0] != 0) { |
| 335 |
if (is_utc_time_zone(dt->tz)) { |
| 336 |
dt->utc_assumed = true; |
| 337 |
} else { |
| 338 |
error = true; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
return error || state != ON_TZ ? false : true; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
static bool s_read_n_digits(struct aws_byte_cursor *str, size_t n, int *out_val) { |
| 347 |
int val = 0; |
| 348 |
if (str->len < n) { |
| 349 |
return false; |
| 350 |
} |
| 351 |
|
| 352 |
for (size_t i = 0; i < n; ++i) { |
| 353 |
uint8_t c = str->ptr[i]; |
| 354 |
if (aws_isdigit(c)) { |
| 355 |
val = val * 10 + (c - '0'); |
| 356 |
} else { |
| 357 |
return false; |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
aws_byte_cursor_advance(str, n); |
| 362 |
*out_val = val; |
| 363 |
return true; |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
static bool s_read_1_char(struct aws_byte_cursor *str, uint8_t *out_c) { |
| 368 |
if (str->len == 0) { |
| 369 |
return false; |
| 370 |
} |
| 371 |
|
| 372 |
*out_c = str->ptr[0]; |
| 373 |
aws_byte_cursor_advance(str, 1); |
| 374 |
return true; |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
static bool s_advance_if_next_char_is(struct aws_byte_cursor *str, uint8_t c) { |
| 379 |
if (str->len == 0 || str->ptr[0] != c) { |
| 380 |
return false; |
| 381 |
} |
| 382 |
|
| 383 |
aws_byte_cursor_advance(str, 1); |
| 384 |
return true; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
static bool s_skip_optional_fractional_seconds(struct aws_byte_cursor *str) { |
| 390 |
if (str->len == 0) { |
| 391 |
return true; |
| 392 |
} |
| 393 |
|
| 394 |
uint8_t c = str->ptr[0]; |
| 395 |
if (c != '.' && c != ',') { |
| 396 |
return true; |
| 397 |
} |
| 398 |
|
| 399 |
size_t num_digits = 0; |
| 400 |
for (size_t i = 1; i < str->len; ++i) { |
| 401 |
if (aws_isdigit(str->ptr[i])) { |
| 402 |
++num_digits; |
| 403 |
} else { |
| 404 |
break; |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
if (num_digits == 0) { |
| 409 |
return false; |
| 410 |
} |
| 411 |
|
| 412 |
aws_byte_cursor_advance(str, 1 + num_digits); |
| 413 |
return true; |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
static bool s_parse_iso_8601(struct aws_byte_cursor str, struct tm *parsed_time, time_t *seconds_offset) { |
| 419 |
AWS_ZERO_STRUCT(*parsed_time); |
| 420 |
*seconds_offset = 0; |
| 421 |
uint8_t c = 0; |
| 422 |
|
| 423 |
|
| 424 |
if (!s_read_n_digits(&str, 4, &parsed_time->tm_year)) { |
| 425 |
return false; |
| 426 |
} |
| 427 |
parsed_time->tm_year -= 1900; |
| 428 |
|
| 429 |
|
| 430 |
bool has_date_separator = s_advance_if_next_char_is(&str, '-'); |
| 431 |
|
| 432 |
|
| 433 |
if (!s_read_n_digits(&str, 2, &parsed_time->tm_mon)) { |
| 434 |
return false; |
| 435 |
} |
| 436 |
parsed_time->tm_mon -= 1; |
| 437 |
|
| 438 |
if (has_date_separator) { |
| 439 |
if (!s_read_1_char(&str, &c) || c != '-') { |
| 440 |
return false; |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
if (!s_read_n_digits(&str, 2, &parsed_time->tm_mday)) { |
| 446 |
return false; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
if (str.len == 0) { |
| 451 |
return true; |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
if (!s_read_1_char(&str, &c) || (tolower(c) != 't' && c != ' ')) { |
| 456 |
return false; |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
if (!s_read_n_digits(&str, 2, &parsed_time->tm_hour)) { |
| 461 |
return false; |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
bool has_time_separator = s_advance_if_next_char_is(&str, ':'); |
| 466 |
|
| 467 |
|
| 468 |
if (!s_read_n_digits(&str, 2, &parsed_time->tm_min)) { |
| 469 |
return false; |
| 470 |
} |
| 471 |
|
| 472 |
if (has_time_separator) { |
| 473 |
if (!s_read_1_char(&str, &c) || c != ':') { |
| 474 |
return false; |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
if (!s_read_n_digits(&str, 2, &parsed_time->tm_sec)) { |
| 480 |
return false; |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
if (!s_skip_optional_fractional_seconds(&str)) { |
| 485 |
return false; |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
if (!s_read_1_char(&str, &c)) { |
| 490 |
return false; |
| 491 |
} |
| 492 |
|
| 493 |
if (tolower(c) == 'z') { |
| 494 |
|
| 495 |
return true; |
| 496 |
} |
| 497 |
|
| 498 |
if (c != '+' && c != '-') { |
| 499 |
return false; |
| 500 |
} |
| 501 |
|
| 502 |
bool negative_offset = c == '-'; |
| 503 |
|
| 504 |
|
| 505 |
int hours_offset = 0; |
| 506 |
if (!s_read_n_digits(&str, 2, &hours_offset)) { |
| 507 |
return false; |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
s_advance_if_next_char_is(&str, ':'); |
| 512 |
|
| 513 |
|
| 514 |
int minutes_offset = 0; |
| 515 |
if (!s_read_n_digits(&str, 2, &minutes_offset)) { |
| 516 |
return false; |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
*seconds_offset = (time_t)(hours_offset * 3600 + minutes_offset * 60) * (negative_offset ? -1 : 1); |
| 521 |
return true; |
| 522 |
} |
| 523 |
|
| 524 |
int aws_date_time_init_from_str_cursor( |
| 525 |
struct aws_date_time *dt, |
| 526 |
const struct aws_byte_cursor *date_str_cursor, |
| 527 |
enum aws_date_format fmt) { |
| 528 |
AWS_ERROR_PRECONDITION(date_str_cursor->len <= AWS_DATE_TIME_STR_MAX_LEN, AWS_ERROR_OVERFLOW_DETECTED); |
| 529 |
|
| 530 |
AWS_ZERO_STRUCT(*dt); |
| 531 |
|
| 532 |
struct tm parsed_time; |
| 533 |
bool successfully_parsed = false; |
| 534 |
|
| 535 |
time_t seconds_offset = 0; |
| 536 |
if (fmt == AWS_DATE_FORMAT_ISO_8601 || fmt == AWS_DATE_FORMAT_ISO_8601_BASIC || |
| 537 |
fmt == AWS_DATE_FORMAT_AUTO_DETECT) { |
| 538 |
if (s_parse_iso_8601(*date_str_cursor, &parsed_time, &seconds_offset)) { |
| 539 |
dt->utc_assumed = true; |
| 540 |
successfully_parsed = true; |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
if (fmt == AWS_DATE_FORMAT_RFC822 || (fmt == AWS_DATE_FORMAT_AUTO_DETECT && !successfully_parsed)) { |
| 545 |
if (s_parse_rfc_822(date_str_cursor, &parsed_time, dt)) { |
| 546 |
successfully_parsed = true; |
| 547 |
|
| 548 |
if (dt->utc_assumed) { |
| 549 |
if (dt->tz[0] == '+' || dt->tz[0] == '-') { |
| 550 |
|
| 551 |
|
| 552 |
char min_str[3] = {0}; |
| 553 |
char hour_str[3] = {0}; |
| 554 |
hour_str[0] = dt->tz[1]; |
| 555 |
hour_str[1] = dt->tz[2]; |
| 556 |
min_str[0] = dt->tz[3]; |
| 557 |
min_str[1] = dt->tz[4]; |
| 558 |
|
| 559 |
long hour = strtol(hour_str, NULL, 10); |
| 560 |
long min = strtol(min_str, NULL, 10); |
| 561 |
seconds_offset = (time_t)(hour * 3600 + min * 60); |
| 562 |
|
| 563 |
if (dt->tz[0] == '-') { |
| 564 |
seconds_offset = -seconds_offset; |
| 565 |
} |
| 566 |
} |
| 567 |
} |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
if (!successfully_parsed) { |
| 572 |
return aws_raise_error(AWS_ERROR_INVALID_DATE_STR); |
| 573 |
} |
| 574 |
|
| 575 |
if (dt->utc_assumed || seconds_offset) { |
| 576 |
dt->timestamp = aws_timegm(&parsed_time); |
| 577 |
} else { |
| 578 |
dt->timestamp = mktime(&parsed_time); |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
dt->timestamp -= seconds_offset; |
| 584 |
|
| 585 |
dt->milliseconds = 0U; |
| 586 |
dt->gmt_time = s_get_time_struct(dt, false); |
| 587 |
dt->local_time = s_get_time_struct(dt, true); |
| 588 |
|
| 589 |
return AWS_OP_SUCCESS; |
| 590 |
} |
| 591 |
|
| 592 |
int aws_date_time_init_from_str( |
| 593 |
struct aws_date_time *dt, |
| 594 |
const struct aws_byte_buf *date_str, |
| 595 |
enum aws_date_format fmt) { |
| 596 |
AWS_ERROR_PRECONDITION(date_str->len <= AWS_DATE_TIME_STR_MAX_LEN, AWS_ERROR_OVERFLOW_DETECTED); |
| 597 |
|
| 598 |
struct aws_byte_cursor date_cursor = aws_byte_cursor_from_buf(date_str); |
| 599 |
return aws_date_time_init_from_str_cursor(dt, &date_cursor, fmt); |
| 600 |
} |
| 601 |
|
| 602 |
static inline int s_date_to_str(const struct tm *tm, const char *format_str, struct aws_byte_buf *output_buf) { |
| 603 |
size_t remaining_space = output_buf->capacity - output_buf->len; |
| 604 |
size_t bytes_written = strftime((char *)output_buf->buffer + output_buf->len, remaining_space, format_str, tm); |
| 605 |
|
| 606 |
if (bytes_written == 0) { |
| 607 |
return aws_raise_error(AWS_ERROR_SHORT_BUFFER); |
| 608 |
} |
| 609 |
|
| 610 |
output_buf->len += bytes_written; |
| 611 |
|
| 612 |
return AWS_OP_SUCCESS; |
| 613 |
} |
| 614 |
|
| 615 |
int aws_date_time_to_local_time_str( |
| 616 |
const struct aws_date_time *dt, |
| 617 |
enum aws_date_format fmt, |
| 618 |
struct aws_byte_buf *output_buf) { |
| 619 |
AWS_ASSERT(fmt != AWS_DATE_FORMAT_AUTO_DETECT); |
| 620 |
|
| 621 |
switch (fmt) { |
| 622 |
case AWS_DATE_FORMAT_RFC822: |
| 623 |
return s_date_to_str(&dt->local_time, RFC822_DATE_FORMAT_STR_WITH_Z, output_buf); |
| 624 |
|
| 625 |
case AWS_DATE_FORMAT_ISO_8601: |
| 626 |
return s_date_to_str(&dt->local_time, ISO_8601_LONG_DATE_FORMAT_STR, output_buf); |
| 627 |
|
| 628 |
case AWS_DATE_FORMAT_ISO_8601_BASIC: |
| 629 |
return s_date_to_str(&dt->local_time, ISO_8601_LONG_BASIC_DATE_FORMAT_STR, output_buf); |
| 630 |
|
| 631 |
default: |
| 632 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
int aws_date_time_to_utc_time_str( |
| 637 |
const struct aws_date_time *dt, |
| 638 |
enum aws_date_format fmt, |
| 639 |
struct aws_byte_buf *output_buf) { |
| 640 |
AWS_ASSERT(fmt != AWS_DATE_FORMAT_AUTO_DETECT); |
| 641 |
|
| 642 |
switch (fmt) { |
| 643 |
case AWS_DATE_FORMAT_RFC822: |
| 644 |
return s_date_to_str(&dt->gmt_time, RFC822_DATE_FORMAT_STR_MINUS_Z, output_buf); |
| 645 |
|
| 646 |
case AWS_DATE_FORMAT_ISO_8601: |
| 647 |
return s_date_to_str(&dt->gmt_time, ISO_8601_LONG_DATE_FORMAT_STR, output_buf); |
| 648 |
|
| 649 |
case AWS_DATE_FORMAT_ISO_8601_BASIC: |
| 650 |
return s_date_to_str(&dt->gmt_time, ISO_8601_LONG_BASIC_DATE_FORMAT_STR, output_buf); |
| 651 |
|
| 652 |
default: |
| 653 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
int aws_date_time_to_local_time_short_str( |
| 658 |
const struct aws_date_time *dt, |
| 659 |
enum aws_date_format fmt, |
| 660 |
struct aws_byte_buf *output_buf) { |
| 661 |
AWS_ASSERT(fmt != AWS_DATE_FORMAT_AUTO_DETECT); |
| 662 |
|
| 663 |
switch (fmt) { |
| 664 |
case AWS_DATE_FORMAT_RFC822: |
| 665 |
return s_date_to_str(&dt->local_time, RFC822_SHORT_DATE_FORMAT_STR, output_buf); |
| 666 |
|
| 667 |
case AWS_DATE_FORMAT_ISO_8601: |
| 668 |
return s_date_to_str(&dt->local_time, ISO_8601_SHORT_DATE_FORMAT_STR, output_buf); |
| 669 |
|
| 670 |
case AWS_DATE_FORMAT_ISO_8601_BASIC: |
| 671 |
return s_date_to_str(&dt->local_time, ISO_8601_SHORT_BASIC_DATE_FORMAT_STR, output_buf); |
| 672 |
|
| 673 |
default: |
| 674 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
int aws_date_time_to_utc_time_short_str( |
| 679 |
const struct aws_date_time *dt, |
| 680 |
enum aws_date_format fmt, |
| 681 |
struct aws_byte_buf *output_buf) { |
| 682 |
AWS_ASSERT(fmt != AWS_DATE_FORMAT_AUTO_DETECT); |
| 683 |
|
| 684 |
switch (fmt) { |
| 685 |
case AWS_DATE_FORMAT_RFC822: |
| 686 |
return s_date_to_str(&dt->gmt_time, RFC822_SHORT_DATE_FORMAT_STR, output_buf); |
| 687 |
|
| 688 |
case AWS_DATE_FORMAT_ISO_8601: |
| 689 |
return s_date_to_str(&dt->gmt_time, ISO_8601_SHORT_DATE_FORMAT_STR, output_buf); |
| 690 |
|
| 691 |
case AWS_DATE_FORMAT_ISO_8601_BASIC: |
| 692 |
return s_date_to_str(&dt->gmt_time, ISO_8601_SHORT_BASIC_DATE_FORMAT_STR, output_buf); |
| 693 |
|
| 694 |
default: |
| 695 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
double aws_date_time_as_epoch_secs(const struct aws_date_time *dt) { |
| 700 |
return (double)dt->timestamp + (double)(dt->milliseconds / 1000.0); |
| 701 |
} |
| 702 |
|
| 703 |
uint64_t aws_date_time_as_nanos(const struct aws_date_time *dt) { |
| 704 |
return aws_timestamp_convert((uint64_t)dt->timestamp, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL) + |
| 705 |
aws_timestamp_convert((uint64_t)dt->milliseconds, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL); |
| 706 |
} |
| 707 |
|
| 708 |
uint64_t aws_date_time_as_millis(const struct aws_date_time *dt) { |
| 709 |
return aws_timestamp_convert((uint64_t)dt->timestamp, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_MILLIS, NULL) + |
| 710 |
(uint64_t)dt->milliseconds; |
| 711 |
} |
| 712 |
|
| 713 |
uint16_t aws_date_time_year(const struct aws_date_time *dt, bool local_time) { |
| 714 |
const struct tm *time = local_time ? &dt->local_time : &dt->gmt_time; |
| 715 |
|
| 716 |
return (uint16_t)(time->tm_year + 1900); |
| 717 |
} |
| 718 |
|
| 719 |
enum aws_date_month aws_date_time_month(const struct aws_date_time *dt, bool local_time) { |
| 720 |
const struct tm *time = local_time ? &dt->local_time : &dt->gmt_time; |
| 721 |
|
| 722 |
return time->tm_mon; |
| 723 |
} |
| 724 |
|
| 725 |
uint8_t aws_date_time_month_day(const struct aws_date_time *dt, bool local_time) { |
| 726 |
const struct tm *time = local_time ? &dt->local_time : &dt->gmt_time; |
| 727 |
|
| 728 |
return (uint8_t)time->tm_mday; |
| 729 |
} |
| 730 |
|
| 731 |
enum aws_date_day_of_week aws_date_time_day_of_week(const struct aws_date_time *dt, bool local_time) { |
| 732 |
const struct tm *time = local_time ? &dt->local_time : &dt->gmt_time; |
| 733 |
|
| 734 |
return time->tm_wday; |
| 735 |
} |
| 736 |
|
| 737 |
uint8_t aws_date_time_hour(const struct aws_date_time *dt, bool local_time) { |
| 738 |
const struct tm *time = local_time ? &dt->local_time : &dt->gmt_time; |
| 739 |
|
| 740 |
return (uint8_t)time->tm_hour; |
| 741 |
} |
| 742 |
|
| 743 |
uint8_t aws_date_time_minute(const struct aws_date_time *dt, bool local_time) { |
| 744 |
const struct tm *time = local_time ? &dt->local_time : &dt->gmt_time; |
| 745 |
|
| 746 |
return (uint8_t)time->tm_min; |
| 747 |
} |
| 748 |
|
| 749 |
uint8_t aws_date_time_second(const struct aws_date_time *dt, bool local_time) { |
| 750 |
const struct tm *time = local_time ? &dt->local_time : &dt->gmt_time; |
| 751 |
|
| 752 |
return (uint8_t)time->tm_sec; |
| 753 |
} |
| 754 |
|
| 755 |
bool aws_date_time_dst(const struct aws_date_time *dt, bool local_time) { |
| 756 |
const struct tm *time = local_time ? &dt->local_time : &dt->gmt_time; |
| 757 |
|
| 758 |
return (bool)time->tm_isdst; |
| 759 |
} |
| 760 |
|
| 761 |
time_t aws_date_time_diff(const struct aws_date_time *a, const struct aws_date_time *b) { |
| 762 |
return a->timestamp - b->timestamp; |
| 763 |
} |