Branch data Line data Source code
1 : : // Copyright 2019-2026 David Robillard <d@drobilla.net>
2 : : // SPDX-License-Identifier: ISC
3 : :
4 : : #include "date.h"
5 : : #include "date_time.h"
6 : : #include "date_utils.h"
7 : : #include "number_utils.h"
8 : : #include "read_utils.h"
9 : : #include "timezone.h"
10 : : #include "write_utils.h"
11 : : #include "year.h"
12 : :
13 : : #include <exess/exess.h>
14 : :
15 : : #include <string.h>
16 : :
17 : : EXESS_NONBLOCKING ExessOrder
18 : 158 : exess_compare_date(const ExessDate lhs, const ExessDate rhs)
19 : : {
20 : 158 : const ExessDateTime lhs_datetime = {
21 : 158 : lhs.year, lhs.month, lhs.day, lhs.zone, 0, 0, 0, 0};
22 : :
23 : 158 : const ExessDateTime rhs_datetime = {
24 : 158 : rhs.year, rhs.month, rhs.day, rhs.zone, 0, 0, 0, 0};
25 : :
26 : 158 : return exess_compare_date_time(lhs_datetime, rhs_datetime);
27 : : }
28 : :
29 : : EXESS_NONBLOCKING ExessResult
30 : 127 : exess_read_date(const char* const str, ExessDate* const out)
31 : : {
32 : 127 : memset(out, 0, sizeof(*out));
33 : :
34 : : // Read YYYY-MM-DD numbers
35 : 127 : size_t i = skip_whitespace(str);
36 : 127 : ExessResult r = read_date_numbers(out, str + i);
37 : :
38 : : // Read timezone if present
39 : 127 : r.count = i + r.count;
40 : 127 : return read_optional_timezone(&out->zone, str, r);
41 : : }
42 : :
43 : : EXESS_NONBLOCKING ExessResult
44 : 151 : exess_write_date(const ExessDate value, const size_t buf_size, char* const buf)
45 : : {
46 [ + + + + : 151 : if (value.month < 1 || value.month > 12 || value.day < 1 || value.day > 31) {
+ + + + ]
47 : 4 : return end_write(EXESS_BAD_VALUE, buf_size, buf, 0);
48 : : }
49 : :
50 : : // Write YYYY-MM-DD numbers
51 : 147 : ExessResult r = write_year_number(value.year, buf_size, buf);
52 : 147 : r.count += write_char('-', buf_size, buf, r.count);
53 : 147 : r.count += write_two_digit_number(value.month, buf_size, buf, r.count);
54 : 147 : r.count += write_char('-', buf_size, buf, r.count);
55 : 147 : r.count += write_two_digit_number(value.day, buf_size, buf, r.count);
56 : :
57 : : // Write timezone suffix and end write
58 : 147 : return write_timezone(value.zone, buf_size, buf, r);
59 : : }
|