ixdtf/parsers/
datetime.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

//! Parsing for `Date`, `DateTime`, and `MonthDay`.

use crate::{
    assert_syntax,
    parsers::{
        annotations,
        grammar::{
            is_annotation_open, is_date_time_separator, is_hyphen, is_sign, is_utc_designator,
        },
        records::{DateRecord, TimeRecord},
        time::parse_time_record,
        timezone, Cursor, IxdtfParseRecord,
    },
    ParseError, ParserResult,
};

use super::records::{Annotation, UtcOffsetRecordOrZ};

#[derive(Debug, Default, Clone)]
/// A `DateTime` Parse Node that contains the date, time, and offset info.
pub(crate) struct DateTimeRecord {
    /// Date
    pub(crate) date: Option<DateRecord>,
    /// Time
    pub(crate) time: Option<TimeRecord>,
    /// Tz Offset
    pub(crate) time_zone: Option<UtcOffsetRecordOrZ>,
}

/// This function handles parsing for [`AnnotatedDateTime`][datetime],
/// [`AnnotatedDateTimeTimeRequred`][time], and
/// [`TemporalInstantString.`][instant] according to the requirements
/// provided via Spec.
///
/// [datetime]: https://tc39.es/proposal-temporal/#prod-AnnotatedDateTime
/// [time]: https://tc39.es/proposal-temporal/#prod-AnnotatedDateTimeTimeRequired
/// [instant]: https://tc39.es/proposal-temporal/#prod-TemporalInstantString
pub(crate) fn parse_annotated_date_time<'a>(
    cursor: &mut Cursor<'a>,
    handler: impl FnMut(Annotation<'a>) -> Option<Annotation<'a>>,
) -> ParserResult<IxdtfParseRecord<'a>> {
    let date_time = parse_date_time(cursor)?;

    // Peek Annotation presence
    // Throw error if annotation does not exist and zoned is true, else return.
    if !cursor.check_or(false, is_annotation_open) {
        cursor.close()?;

        return Ok(IxdtfParseRecord {
            date: date_time.date,
            time: date_time.time,
            offset: date_time.time_zone,
            tz: None,
            calendar: None,
        });
    }

    let annotation_set = annotations::parse_annotation_set(cursor, handler)?;

    cursor.close()?;

    Ok(IxdtfParseRecord {
        date: date_time.date,
        time: date_time.time,
        offset: date_time.time_zone,
        tz: annotation_set.tz,
        calendar: annotation_set.calendar,
    })
}

/// Parses an AnnotatedMonthDay.
pub(crate) fn parse_annotated_month_day<'a>(
    cursor: &mut Cursor<'a>,
    handler: impl FnMut(Annotation<'a>) -> Option<Annotation<'a>>,
) -> ParserResult<IxdtfParseRecord<'a>> {
    let date = parse_month_day(cursor)?;

    if !cursor.check_or(false, is_annotation_open) {
        cursor.close()?;

        return Ok(IxdtfParseRecord {
            date: Some(date),
            time: None,
            offset: None,
            tz: None,
            calendar: None,
        });
    }

    let annotation_set = annotations::parse_annotation_set(cursor, handler)?;

    Ok(IxdtfParseRecord {
        date: Some(date),
        time: None,
        offset: None,
        tz: annotation_set.tz,
        calendar: annotation_set.calendar,
    })
}

/// Parse an annotated YearMonth
pub(crate) fn parse_annotated_year_month<'a>(
    cursor: &mut Cursor<'a>,
    handler: impl FnMut(Annotation<'a>) -> Option<Annotation<'a>>,
) -> ParserResult<IxdtfParseRecord<'a>> {
    let year = parse_date_year(cursor)?;
    cursor.advance_if(cursor.check_or(false, is_hyphen));
    let month = parse_date_month(cursor)?;

    let date = DateRecord {
        year,
        month,
        day: 1,
    };

    if !cursor.check_or(false, is_annotation_open) {
        cursor.close()?;

        return Ok(IxdtfParseRecord {
            date: Some(date),
            time: None,
            offset: None,
            tz: None,
            calendar: None,
        });
    }

    let annotation_set = annotations::parse_annotation_set(cursor, handler)?;

    Ok(IxdtfParseRecord {
        date: Some(date),
        time: None,
        offset: None,
        tz: annotation_set.tz,
        calendar: annotation_set.calendar,
    })
}

/// Parses a `DateTime` record.
fn parse_date_time(cursor: &mut Cursor) -> ParserResult<DateTimeRecord> {
    let date = parse_date(cursor)?;

    // If there is no `DateTimeSeparator`, return date early.
    if !cursor.check_or(false, is_date_time_separator) {
        return Ok(DateTimeRecord {
            date: Some(date),
            time: None,
            time_zone: None,
        });
    }

    cursor.advance();

    let time = parse_time_record(cursor)?;

    let time_zone = if cursor.check_or(false, |ch| is_sign(ch) || is_utc_designator(ch)) {
        Some(timezone::parse_date_time_utc(cursor)?)
    } else {
        None
    };

    Ok(DateTimeRecord {
        date: Some(date),
        time: Some(time),
        time_zone,
    })
}

/// Parses `Date` record.
fn parse_date(cursor: &mut Cursor) -> ParserResult<DateRecord> {
    let year = parse_date_year(cursor)?;
    let hyphenated = cursor
        .check(is_hyphen)
        .ok_or(ParseError::abrupt_end("Date"))?;

    cursor.advance_if(hyphenated);

    let month = parse_date_month(cursor)?;

    let second_hyphen = cursor.check_or(false, is_hyphen);
    assert_syntax!(hyphenated == second_hyphen, DateSeparator);
    cursor.advance_if(second_hyphen);

    let day = parse_date_day(cursor)?;

    check_date_validity(year, month, day)?;

    Ok(DateRecord { year, month, day })
}

// ==== `MonthDay` parsing functions ====

/// Parses a `DateSpecMonthDay`
pub(crate) fn parse_month_day(cursor: &mut Cursor) -> ParserResult<DateRecord> {
    let hyphenated = cursor
        .check(is_hyphen)
        .ok_or(ParseError::abrupt_end("MonthDay"))?;
    cursor.advance_if(hyphenated);
    let balanced_hyphens = hyphenated
        && cursor
            .check(is_hyphen)
            .ok_or(ParseError::abrupt_end("MonthDay"))?;
    cursor.advance_if(balanced_hyphens);

    if hyphenated && !balanced_hyphens {
        return Err(ParseError::MonthDayHyphen);
    }

    let month = parse_date_month(cursor)?;

    cursor.advance_if(cursor.check_or(false, is_hyphen));

    let day = parse_date_day(cursor)?;

    assert_syntax!(cursor.check_or(true, is_annotation_open), InvalidEnd);

    Ok(DateRecord {
        year: 0,
        month,
        day,
    })
}

// ==== Unit Parsers ====

#[inline]
fn parse_date_year(cursor: &mut Cursor) -> ParserResult<i32> {
    if cursor.check_or(false, is_sign) {
        let sign = if cursor.next_or(ParseError::ImplAssert)? == '+' {
            1
        } else {
            -1
        };

        let first = cursor.next_digit()?.ok_or(ParseError::DateExtendedYear)? as i32 * 100_000;
        let second = cursor.next_digit()?.ok_or(ParseError::DateExtendedYear)? as i32 * 10_000;
        let third = cursor.next_digit()?.ok_or(ParseError::DateExtendedYear)? as i32 * 1000;
        let fourth = cursor.next_digit()?.ok_or(ParseError::DateExtendedYear)? as i32 * 100;
        let fifth = cursor.next_digit()?.ok_or(ParseError::DateExtendedYear)? as i32 * 10;

        let year_value = first
            + second
            + third
            + fourth
            + fifth
            + cursor.next_digit()?.ok_or(ParseError::DateExtendedYear)? as i32;

        // 13.30.1 Static Semantics: Early Errors
        //
        // It is a Syntax Error if DateYear is "-000000" or "−000000" (U+2212 MINUS SIGN followed by 000000).
        if sign == -1 && year_value == 0 {
            return Err(ParseError::DateExtendedYear);
        }

        let year = sign * year_value;

        return Ok(year);
    }

    let first = cursor.next_digit()?.ok_or(ParseError::DateYear)? as i32 * 1000;
    let second = cursor.next_digit()?.ok_or(ParseError::DateYear)? as i32 * 100;
    let third = cursor.next_digit()?.ok_or(ParseError::DateYear)? as i32 * 10;
    let year_value =
        first + second + third + cursor.next_digit()?.ok_or(ParseError::DateYear)? as i32;

    Ok(year_value)
}

#[inline]
fn parse_date_month(cursor: &mut Cursor) -> ParserResult<u8> {
    let first = cursor.next_digit()?.ok_or(ParseError::DateMonth)?;
    let month_value = first * 10 + cursor.next_digit()?.ok_or(ParseError::DateMonth)?;
    if !(1..=12).contains(&month_value) {
        return Err(ParseError::InvalidMonthRange);
    }
    Ok(month_value)
}

#[inline]
fn parse_date_day(cursor: &mut Cursor) -> ParserResult<u8> {
    let first = cursor.next_digit()?.ok_or(ParseError::DateDay)?;
    let day_value = first * 10 + cursor.next_digit()?.ok_or(ParseError::DateDay)?;
    Ok(day_value)
}

#[inline]
fn check_date_validity(year: i32, month: u8, day: u8) -> ParserResult<()> {
    let Some(days_in_month) = days_in_month(year, month) else {
        // NOTE: This should never through due to check in `parse_date_month`
        return Err(ParseError::InvalidMonthRange);
    };
    if !(1..=days_in_month).contains(&day) {
        return Err(ParseError::InvalidDayRange);
    }
    Ok(())
}

/// Utilty to return the days in month, returns None if month is invalid
#[inline]
fn days_in_month(year: i32, month: u8) -> Option<u8> {
    match month {
        1 | 3 | 5 | 7 | 8 | 10 | 12 => Some(31),
        4 | 6 | 9 | 11 => Some(30),
        2 => Some(28 + u8::from(in_leap_year(year))),
        _ => None,
    }
}

/// Utility that returns whether a year is a leap year.
#[inline]
fn in_leap_year(year: i32) -> bool {
    if year % 4 != 0 {
        false
    } else if year % 4 == 0 && year % 100 != 0 {
        true
    } else if year % 100 == 0 && year % 400 != 0 {
        false
    } else {
        assert_eq!(year % 400, 0);
        true
    }
}