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
// SPDX-License-Identifier: MIT
// Copyright 2023 IROX Contributors

//!
//! Implementations of [`Format`] and [`FormatParser`] based on the ISO8601 specification
//!

use std::str::FromStr;

use irox_tools::iterators::Itertools;
use irox_units::units::duration::Duration;

use crate::datetime::UTCDateTime;
use crate::format::{Format, FormatError, FormatParser};
use crate::gregorian::Date;
use crate::Time;

///
/// IS0 8601-1:2019 Basic Date and Time of Day Format, section 5.4.2
///
/// Equivalent to `YYYYMMddTHHmmssZ`
pub struct BasicDateTimeOfDay;
///
/// IS0 8601-1:2019 Basic Date and Time of Day Format, section 5.4.2
///
/// Equivalent to `YYYYMMddTHHmmssZ`
pub const BASIC_DATE_TIME_OF_DAY: BasicDateTimeOfDay = BasicDateTimeOfDay {};
impl Format for BasicDateTimeOfDay {
    type Item = UTCDateTime;

    fn format(&self, date: &UTCDateTime) -> String {
        format!(
            "{}{}",
            BasicCalendarDate::format(&date.get_date()),
            BasicTimeOfDay::format(&date.get_time())
        )
    }
}
impl FormatParser for BasicDateTimeOfDay {
    type Item = UTCDateTime;

    fn try_from(&self, data: &str) -> Result<UTCDateTime, FormatError> {
        let date = Date::parse_from(&BASIC_CALENDAR_DATE, data)?;
        let time = Time::parse_from(&BASIC_TIME_OF_DAY, data.split_at(8).1)?;
        Ok(UTCDateTime { date, time })
    }
}

///
/// IS0 8601-1:2019 Basic Calendar Date Format, of section 5.2.2
///
/// Equivalent to `YYYYMMdd`
#[derive(Default, Debug, Copy, Clone)]
pub struct BasicCalendarDate;
///
/// IS0 8601-1:2019 Basic Calendar Date Format, of section 5.2.2
///
/// Equivalent to `YYYYMMdd`
pub const BASIC_CALENDAR_DATE: BasicCalendarDate = BasicCalendarDate {};
impl BasicCalendarDate {
    pub fn format(date: &Date) -> String {
        BasicCalendarDate.format(date)
    }
}
impl Format for BasicCalendarDate {
    type Item = Date;

    fn format(&self, date: &Self::Item) -> String {
        format!(
            "{:04}{:02}{:02}",
            date.year(),
            date.month_of_year() as u8,
            date.day_of_month() + 1,
        )
    }
}
impl FormatParser for BasicCalendarDate {
    type Item = Date;

    fn try_from(&self, data: &str) -> Result<Self::Item, FormatError> {
        // expecting a string of length 8
        let mut iter = data.chars();
        let year_str = iter.collect_next_chunk(4);
        let month_str = iter.collect_next_chunk(2);
        let day_str = iter.collect_next_chunk(2);

        if year_str.len() != 4 {
            return FormatError::err(format!(
                "Expecting year to be length 4, but was {}",
                year_str.len()
            ));
        }
        if month_str.len() != 2 {
            return FormatError::err(format!(
                "Expecting month to be length 2, but was {}",
                month_str.len()
            ));
        }
        if day_str.len() != 2 {
            return FormatError::err(format!(
                "Expecting day to be length 2, but was {}",
                day_str.len()
            ));
        }
        let year_str = String::from_iter(year_str);
        let year = i32::from_str(&year_str)?;
        let month_str = String::from_iter(month_str);
        let month = u8::from_str(&month_str)?;
        let day_str = String::from_iter(day_str);
        let day = u8::from_str(&day_str)?;

        Ok(Date::try_from_values(year, month, day)?)
    }
}

///
/// IS0 8601-1:2019 Basic Time Of Day Format, of section 5.3.3
///
/// Equivalent to `THHmmssZ`
#[derive(Default, Debug, Copy, Clone)]
pub struct BasicTimeOfDay;
///
/// IS0 8601-1:2019 Basic Time Of Day Format, of section 5.3.3
///
/// Equivalent to `THHmmssZ`
pub const BASIC_TIME_OF_DAY: BasicTimeOfDay = BasicTimeOfDay {};

impl Format for BasicTimeOfDay {
    type Item = Time;

    fn format(&self, date: &Self::Item) -> String {
        let (h, m, s) = date.as_hms();
        format!("T{h:02}{m:02}{s:02}Z")
    }
}
impl FormatParser for BasicTimeOfDay {
    type Item = Time;

    fn try_from(&self, data: &str) -> Result<Self::Item, FormatError> {
        let mut iter = data.chars();
        let tee = iter.collect_next_chunk(1);
        let hour_string = iter.collect_next_chunk(2);
        let minute_string = iter.collect_next_chunk(2);
        let second_string = iter.collect_next_chunk(2);
        let zee = iter.collect_next_chunk(1);

        if tee.first() != Some(&'T') {
            return FormatError::err_str("Expecting the string to start with 'T'");
        }
        if zee.first() != Some(&'Z') {
            return FormatError::err_str("Expecting the string to end with 'Z'");
        }

        if hour_string.len() != 2 {
            return FormatError::err(format!(
                "Expecting hours to be length 2, but was {}",
                hour_string.len()
            ));
        }
        if minute_string.len() != 2 {
            return FormatError::err(format!(
                "Expecting minutes to be length 2, but was {}",
                minute_string.len()
            ));
        }
        if second_string.len() != 2 {
            return FormatError::err(format!(
                "Expecting seconds to be length 2, but was {}",
                second_string.len()
            ));
        }

        let hours = u32::from_str(String::from_iter(hour_string).as_str())?;
        let minutes = u32::from_str(String::from_iter(minute_string).as_str())?;
        let seconds = u32::from_str(String::from_iter(second_string).as_str())?;

        let second_of_day = hours * 3600 + minutes * 60 + seconds;

        Ok(Time {
            second_of_day,
            nanoseconds: 0,
        })
    }
}
impl BasicTimeOfDay {
    pub fn format(time: &Time) -> String {
        BasicTimeOfDay.format(time)
    }
}

pub struct ISO8601Duration;
pub const DURATION: ISO8601Duration = ISO8601Duration;

impl Format for ISO8601Duration {
    type Item = Duration;

    fn format(&self, date: &Self::Item) -> String {
        let (days, hours, minutes, seconds) = date.as_dhms();
        if days > 0 {
            return format!("P{days}DT{hours:02}H{minutes:02}M{seconds:02}S");
        }
        if hours > 0 {
            return format!("PT{hours}H{minutes:02}M{seconds:02}S");
        }
        if minutes > 0 {
            return format!("PT{minutes}M{seconds:02}S");
        }
        format!("PT{seconds}S")
    }
}

#[cfg(test)]
mod tests {
    use crate::epoch::{
        COMMON_ERA_EPOCH, GPS_EPOCH, GREGORIAN_EPOCH, NTP_EPOCH, PRIME_EPOCH, UNIX_EPOCH,
        WINDOWS_NT_EPOCH,
    };
    use crate::format::iso8601::{BASIC_CALENDAR_DATE, BASIC_TIME_OF_DAY};
    use crate::format::FormatError;
    use crate::gregorian::Date;
    use crate::Time;

    #[test]
    pub fn test_basic_date() -> Result<(), FormatError> {
        let tests = vec![
            ("19700101", UNIX_EPOCH),
            ("19800106", GPS_EPOCH),
            ("19000101", NTP_EPOCH),
            ("19000101", PRIME_EPOCH),
            ("15821015", GREGORIAN_EPOCH),
            ("00010101", COMMON_ERA_EPOCH),
            ("16010101", WINDOWS_NT_EPOCH),
        ];
        for (string, format) in tests {
            assert_eq!(
                string,
                format
                    .get_gregorian_date()
                    .format(&BASIC_CALENDAR_DATE)
                    .as_str()
            );
            assert_eq!(
                format.get_gregorian_date(),
                Date::parse_from(&BASIC_CALENDAR_DATE, string)?
            );
        }
        Ok(())
    }

    #[test]
    pub fn test_basic_time() -> Result<(), FormatError> {
        for hour in 0..24 {
            for minute in 0..60 {
                for second in 0..60 {
                    let time_sec = hour * 3600 + minute * 60 + second;
                    let time = Time::new(time_sec, 0)?;

                    assert_eq!(
                        format!("T{hour:02}{minute:02}{second:02}Z"),
                        time.format(&BASIC_TIME_OF_DAY)
                    );
                }
            }
        }
        Ok(())
    }

    #[test]
    pub fn test_basic_datetime() -> Result<(), FormatError> {
        Ok(())
    }
}