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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use crate::{DtErr, DtErrKind, Offset, Scale, TimeParts, an_err};
impl TimeParts {
/// Generalized ISO / CCSDS ASCII Time Code parser (A or B variant).
/// - Parses e.g. **`+2000-01-01T17:00:00 -0500 [America/New_York] TAI`**.
/// - Only supports ASCII characters.
/// - If a time is included then some kind of date-time separator e.g. `T` is
/// required.
/// - Supports both calendar (`%Y-%m-%d`) and day-of-year (`%Y-%j`) formats.
/// - Treats years digits literally as shown, for example `99-01-01` would be
/// the year 99 AD not 1999.
/// - Supported **optional** components:
/// - Time components after a date e.g. `T12:00:00`.
/// - Offset after time components or directly after the date e.g. `+0200` or
/// `2023-01-01+05:00`.
/// - Timezone name, **requires square brackets** and requires `jiff-tz` feature,
/// after time or offset e.g. `T12:00:00 [America/New_York]`.
/// - Library time scale right on the end of the input, e.g. `TAI`.
/// - This function is considerably faster than all other string parsing methods if
/// your date-time string is in the supported formats.
pub fn from_str_iso(input: &str) -> Result<Self, DtErr> {
let bytes = input.as_bytes();
let len_ = bytes.len();
let mut start = 0usize;
while start < len_ {
let b = bytes[start];
if b.is_ascii_digit()
|| (matches!(b, b'+' | b'-')
&& start + 1 < len_
&& bytes[start + 1].is_ascii_digit())
{
break;
}
start += 1;
}
if start == len_ {
return Err(an_err!(
DtErrKind::ExpectedYear,
"year start (digit or +/- and digit)"
));
}
let input = &input[start..];
let bytes = input.as_bytes();
let len_ = bytes.len();
let mut pos: usize = 0;
let mut tp = TimeParts::new_utc();
// Year (manual accumulation, optional sign)
let mut year: i64 = 0;
let negative_year = if pos < len_ && matches!(bytes[pos], b'+' | b'-') {
pos += 1;
bytes[pos] == b'-'
} else {
false
};
if bytes[pos].is_ascii_digit() {
while pos < len_ && bytes[pos].is_ascii_digit() {
year = year * 10 + (bytes[pos] - b'0') as i64;
pos += 1;
}
} else {
return Err(an_err!(
DtErrKind::ExpectedYear,
"year (digits after optional sign)"
));
}
if negative_year {
year = -year;
}
tp.yr = Some(year);
// required separator after year
if pos < len_ {
pos += 1;
}
// DOY vs calendar detection, uses required datetime separator to detect
let is_doy = pos + 3 == len_ || (pos + 3 < len_ && !bytes[pos + 3].is_ascii_digit());
if is_doy {
// 3-digit day of year
let mut doy: u16 = 0;
// digit 1
if bytes[pos].is_ascii_digit() {
doy = doy * 10 + (bytes[pos] - b'0') as u16;
pos += 1;
} else {
return Err(an_err!(DtErrKind::ExpectedDayOfYear, "0/3 digits"));
}
// digit 2
if bytes[pos].is_ascii_digit() {
doy = doy * 10 + (bytes[pos] - b'0') as u16;
pos += 1;
} else {
return Err(an_err!(DtErrKind::ExpectedDayOfYear, "1/3 digits"));
}
// digit 3
if bytes[pos].is_ascii_digit() {
doy = doy * 10 + (bytes[pos] - b'0') as u16;
pos += 1;
} else {
return Err(an_err!(DtErrKind::ExpectedDayOfYear, "2/3 digits"));
}
tp.day_of_yr = Some(doy);
} else {
// 2-digit month
let mut mo: u8 = 0;
// digit 1
if bytes[pos].is_ascii_digit() {
mo = mo * 10 + (bytes[pos] - b'0');
pos += 1;
} else {
return Err(an_err!(DtErrKind::ExpectedMonth, "0/2 digits"));
}
// digit 2
if bytes[pos].is_ascii_digit() {
mo = mo * 10 + (bytes[pos] - b'0');
pos += 1;
} else {
return Err(an_err!(DtErrKind::ExpectedMonth, "1/2 digits"));
}
tp.mo = Some(mo);
// Optional separator after month
if pos < len_ && !bytes[pos].is_ascii_digit() {
pos += 1;
}
// 2-digit day
let mut day: u8 = 0;
// digit 1
if bytes[pos].is_ascii_digit() {
day = day * 10 + (bytes[pos] - b'0');
pos += 1;
} else {
return Err(an_err!(DtErrKind::ExpectedDay, "0/2 digits"));
}
// digit 2
if bytes[pos].is_ascii_digit() {
day = day * 10 + (bytes[pos] - b'0');
pos += 1;
} else {
return Err(an_err!(DtErrKind::ExpectedDay, "1/2 digits"));
}
tp.day = Some(day);
}
// required date-time separator
while pos < len_ && bytes[pos].is_ascii_whitespace() {
pos += 1;
}
if pos < len_ {
let c = bytes[pos];
// push past a T
if !c.is_ascii_digit() {
if pos + 1 < len_ && !matches!(c, b'+' | b'-') {
if bytes[pos + 1].is_ascii_digit() {
pos += 1;
} else if bytes[pos + 1].is_ascii_whitespace() {
pos += 1;
while pos < len_ && bytes[pos].is_ascii_whitespace() {
pos += 1;
}
}
}
}
}
// Optional time components
if pos < len_ && bytes[pos].is_ascii_digit() {
// Hour (2 digits)
let mut hr: u8 = 0;
// digit 1
if bytes[pos].is_ascii_digit() {
hr = hr * 10 + (bytes[pos] - b'0');
pos += 1;
} else {
return Err(an_err!(DtErrKind::ExpectedHour, "0/2 digits"));
}
// digit 2
if bytes[pos].is_ascii_digit() {
hr = hr * 10 + (bytes[pos] - b'0');
pos += 1;
} else {
return Err(an_err!(DtErrKind::ExpectedHour, "1/2 digits"));
}
tp.hr = hr;
'time: {
// only continue if it's not a + or - and not an alpha
if pos >= len_
|| bytes[pos].is_ascii_digit()
|| matches!(bytes[pos], b'+' | b'-')
|| bytes[pos].is_ascii_alphabetic()
{
break 'time;
}
pos += 1;
// Minute (2 digits)
if pos + 2 > len_ {
break 'time;
}
let mut min: u8 = 0;
// digit 1
if bytes[pos].is_ascii_digit() {
min = min * 10 + (bytes[pos] - b'0');
pos += 1;
} else {
return Err(an_err!(DtErrKind::ExpectedMinute, "0/2 digits"));
}
// digit 2
if bytes[pos].is_ascii_digit() {
min = min * 10 + (bytes[pos] - b'0');
pos += 1;
} else {
return Err(an_err!(DtErrKind::ExpectedMinute, "0/2 digits"));
}
tp.min = min;
// only continue if it's not a + or - and not an alpha
if pos >= len_
|| bytes[pos].is_ascii_digit()
|| matches!(bytes[pos], b'+' | b'-')
|| bytes[pos].is_ascii_alphabetic()
{
break 'time;
}
pos += 1;
// Second (2 digits, if present)
if pos + 2 > len_ {
break 'time;
}
let mut sec: u8 = 0;
// digit 1
if bytes[pos].is_ascii_digit() {
sec = sec * 10 + (bytes[pos] - b'0');
pos += 1;
} else {
return Err(an_err!(DtErrKind::ExpectedSecond, "0/2 digits"));
}
// digit 2
if bytes[pos].is_ascii_digit() {
sec = sec * 10 + (bytes[pos] - b'0');
pos += 1;
} else {
return Err(an_err!(DtErrKind::ExpectedSecond, "1/2 digits"));
}
tp.sec = sec;
// only continue if it's not a + or - and not an alpha
if pos >= len_
|| bytes[pos].is_ascii_digit()
|| matches!(bytes[pos], b'+' | b'-')
|| bytes[pos].is_ascii_alphabetic()
{
break 'time;
}
pos += 1;
// Fractional seconds (with or without leading dot)
if pos < len_ {
if bytes[pos] == b'.' {
pos += 1;
}
if pos < len_ && bytes[pos].is_ascii_digit() {
let mut attos: u64 = 0;
let mut digits_seen: usize = 0;
while pos < len_ && bytes[pos].is_ascii_digit() {
if digits_seen < 18 {
attos = attos * 10 + (bytes[pos] - b'0') as u64;
digits_seen += 1;
}
// Ignore any digits beyond the first 18
pos += 1;
}
if digits_seen > 0 {
tp.attos = attos * 10u64.pow(18u32.saturating_sub(digits_seen as u32));
}
}
}
}
// Optional trailing Z/z
if pos < len_ && matches!(bytes[pos], b'Z' | b'z') {
pos += 1;
}
}
// Skip any whitespace
while pos < len_ && bytes[pos].is_ascii_whitespace() {
pos += 1;
}
// Optional offset
if pos < len_ && matches!(bytes[pos], b'+' | b'-') {
let sign: i64 = if bytes[pos] == b'+' { 1 } else { -1 };
pos += 1;
// Parse hours (up to 2 digits). "+05:30"/"+0530"
let mut hours: i64 = 0;
let mut h_digits = 0usize;
while pos < len_ && bytes[pos].is_ascii_digit() && h_digits < 2 {
hours = hours * 10 + (bytes[pos] - b'0') as i64;
pos += 1;
h_digits += 1;
}
if h_digits > 0 {
// Optional ':' separator before minutes
if pos < len_ && bytes[pos] == b':' {
pos += 1;
}
// Parse minutes (up to 2 digits; optional)
let mut minutes: i64 = 0;
let mut m_digits = 0usize;
while pos < len_ && bytes[pos].is_ascii_digit() && m_digits < 2 {
minutes = minutes * 10 + (bytes[pos] - b'0') as i64;
pos += 1;
m_digits += 1;
}
let total_sec_i64 = sign * (hours * 3600 + minutes * 60);
let total_seconds: i32 =
total_sec_i64.clamp(i32::MIN as i64, i32::MAX as i64) as i32;
tp.offset = Some(Offset::Fixed(total_seconds));
}
}
// Skip any whitespace before IANA name or scale
while pos < len_ && bytes[pos].is_ascii_whitespace() {
pos += 1;
}
// Optional IANA timezone name in square brackets, e.g. [America/New_York]
// Must be explicitly wrapped in [] so we don't mistake a scale for a zone.
if pos < len_ && bytes[pos] == b'[' {
pos += 1; // skip '['
let name_start = pos;
while pos < len_ && bytes[pos] != b']' {
pos += 1;
}
if pos >= len_ {
return Err(an_err!(
DtErrKind::InvalidTimezoneOffset,
"unclosed IANA tz name (missing ']')"
));
}
// pos is now at ']'
let iana_bytes = &bytes[name_start..pos];
let iana = core::str::from_utf8(iana_bytes).map_err(|_| {
an_err!(
DtErrKind::InvalidBytes,
"IANA tz name contains invalid UTF-8"
)
})?;
tp.set_iana_name(Some(iana));
pos += 1; // consume ']'
}
// Optional trailing scale (e.g. TAI, UTC)
if pos < len_ {
while pos < len_ && !bytes[pos].is_ascii_alphabetic() {
pos += 1;
}
if pos < len_ {
let end = {
let mut i = pos;
while i < len_ && bytes[i].is_ascii_alphabetic() {
i += 1;
if i - pos > 8 {
break;
}
}
i
};
if let Some(sc) = Scale::from_abbrev(&input[pos..end]) {
tp.scale = sc;
// pos += end - pos;
}
}
}
Ok(tp)
}
}