Branch data Line data Source code
1 : : // Copyright 2019-2026 David Robillard <d@drobilla.net>
2 : : // SPDX-License-Identifier: ISC
3 : :
4 : : #include "date_utils.h"
5 : : #include "number_utils.h"
6 : : #include "read_utils.h"
7 : : #include "result.h"
8 : : #include "timezone.h"
9 : : #include "write_utils.h"
10 : :
11 : : #include <exess/exess.h>
12 : :
13 : : #include <string.h>
14 : :
15 : : EXESS_NONBLOCKING ExessResult
16 : 43 : exess_read_day(const char* const str, ExessDay* const out)
17 : : {
18 : 43 : memset(out, 0, sizeof(*out));
19 : :
20 : : // Skip leading absent-year delimiters
21 : 43 : size_t i = skip_whitespace(str);
22 [ + + ]: 43 : if (str[i] != '-') {
23 : 5 : return RESULT(EXESS_EXPECTED_DASH, i);
24 : : }
25 : :
26 : : // Skip absent-month delimitera
27 : 38 : ++i;
28 [ + + ]: 38 : if (str[i] != '-') {
29 : 1 : return RESULT(EXESS_EXPECTED_DASH, i);
30 : : }
31 : :
32 : : // Read (month-day delimiter and) day number
33 : 37 : ++i;
34 : 37 : ExessResult r = read_date_d(&out->day, str + i);
35 : :
36 : : // Read timezone if present
37 : 37 : r.count = i + r.count;
38 : 37 : return read_optional_timezone(&out->zone, str, r);
39 : : }
40 : :
41 : : EXESS_NONBLOCKING ExessResult
42 : 25 : exess_write_day(const ExessDay value, const size_t buf_size, char* const buf)
43 : : {
44 [ + + + + ]: 25 : if (value.day < 1 || value.day > 31) {
45 : 2 : return end_write(EXESS_BAD_VALUE, buf_size, buf, 0);
46 : : }
47 : :
48 : : // Write leading absent-year-month delimiters
49 : 23 : ExessResult r = {EXESS_SUCCESS, 0};
50 : 23 : r.count += write_char('-', buf_size, buf, r.count);
51 : 23 : r.count += write_char('-', buf_size, buf, r.count);
52 : :
53 : : // Write -DD number
54 : 23 : r.count += write_char('-', buf_size, buf, r.count);
55 : 23 : r.count += write_two_digit_number(value.day, buf_size, buf, r.count);
56 : :
57 : : // Write timezone suffix and end write
58 : 23 : return write_timezone(value.zone, buf_size, buf, r);
59 : : }
|