hoicko_lib 0.1.16

Hoicko library
Documentation
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
use std::fmt;

use chrono::{DateTime, Datelike, Local, NaiveDate, NaiveDateTime, TimeZone, Utc};
use chrono_tz::Tz;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DateTimeWithZone {
    pub datetime: DateTime<Utc>,
    pub timezone: Option<String>,
}
impl From<i32> for DateFormat {
    fn from(value: i32) -> Self {
        match value {
            0 => DateFormat::YearMonthDaySlash,
            1 => DateFormat::YearMonthDayDash,
            2 => DateFormat::DayMonthYear,
            3 => DateFormat::YearMonth,
            4 => DateFormat::MonthDay,
            5 => DateFormat::Year,
            6 => DateFormat::Month,
            7 => DateFormat::Day,
            _ => panic!("Invalid value for DateFormat"),
        }
    }
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(into = "i32", from = "i32")]
pub enum DateFormat {
    YearMonthDaySlash = 0,  
    YearMonthDayDash = 1,   
    DayMonthYear = 2,       
    YearMonth = 3,         
    MonthDay = 4,           
    Year = 5,               
    Month = 6,             
    Day = 7,               
}
impl DateFormat {
    pub fn format_with_timezone(&self, dt_with_zone: &DateTimeWithZone) -> Result<String, DateTimeError> {
        if let Some(tz_str) = &dt_with_zone.timezone {
            let tz: Tz = tz_str.parse()
                .map_err(|_| DateTimeError::InvalidTimezone(tz_str.clone()))?;
            let local_dt = dt_with_zone.datetime.with_timezone(&tz);
            Ok(self.format_datetime(&local_dt.with_timezone(&Utc)))
        } else {
            Ok(self.format_datetime(&dt_with_zone.datetime))
        }
    }
    pub fn parse_with_timezone(&self, date_str: &str, timezone: Option<&str>) -> Result<DateTimeWithZone, DateTimeError> {
        let utc_dt = self.parse_datetime(date_str)?;
        
        let timezone = match timezone {
            Some(tz_str) => {
                let _tz: Tz = tz_str.parse()
                    .map_err(|_| DateTimeError::InvalidTimezone(tz_str.to_string()))?;
                Some(tz_str.to_string())
            },
            None => None
        };

        Ok(DateTimeWithZone {
            datetime: utc_dt,
            timezone,
        })
    }
    pub fn format_timestamp(&self, timestamp: i64) -> Result<String, DateTimeError> {
        let datetime = Utc
            .timestamp_millis_opt(timestamp)
            .single()
            .ok_or_else(|| DateTimeError::InvalidTimestamp(timestamp))?;

        Ok(self.format_datetime(&datetime))
    }
    pub fn to_timestamp(&self, date_str: &str) -> Result<i64, DateTimeError> {
        let datetime = self.parse_datetime(date_str)?;
        Ok(datetime.timestamp_millis())
    }
    pub fn get_format_string(&self) -> &'static str {
        match self {
            DateFormat::YearMonthDaySlash => "%Y/%m/%d",
            DateFormat::YearMonthDayDash => "%Y-%m-%d",
            DateFormat::DayMonthYear => "%d/%m/%Y",
            DateFormat::YearMonth => "%Y-%m",
            DateFormat::MonthDay => "%m-%d",
            DateFormat::Year => "%Y",
            DateFormat::Month => "%m",
            DateFormat::Day => "%d",
        }
    }
    pub fn format_datetime(&self, date: &DateTime<Utc>) -> String {
        date.format(self.get_format_string()).to_string()
    }
    pub fn parse_datetime(&self, date_str: &str) -> Result<DateTime<Utc>, DateTimeError> {
        let current_date = Local::now();

        match self {
            // Full date formats
            DateFormat::YearMonthDaySlash
            | DateFormat::YearMonthDayDash
            | DateFormat::DayMonthYear => {
                let naive_date = NaiveDateTime::parse_from_str(
                    &format!("{} 00:00:00", date_str),
                    &format!("{} %H:%M:%S", self.get_format_string()),
                )
                .map_err(|_| DateTimeError::ParseError(date_str.to_string()))?;

                Ok(DateTime::from_naive_utc_and_offset(naive_date, Utc))
            }

            // Year-Month format (YYYY-MM)
            DateFormat::YearMonth => {
                let parts: Vec<&str> = date_str.split('-').collect();
                if parts.len() != 2 {
                    return Err(DateTimeError::ParseError(date_str.to_string()));
                }

                let year = parts[0].parse::<i32>().map_err(|_| {
                    DateTimeError::ParseError(format!("Invalid year: {}", parts[0]))
                })?;
                let month = parts[1].parse::<u32>().map_err(|_| {
                    DateTimeError::ParseError(format!("Invalid month: {}", parts[1]))
                })?;

                let naive_date = NaiveDate::from_ymd_opt(year, month, 1)
                    .ok_or_else(|| DateTimeError::InvalidDate(date_str.to_string()))?
                    .and_hms_opt(0, 0, 0)
                    .unwrap();

                Ok(DateTime::from_naive_utc_and_offset(naive_date, Utc))
            }

            // Month-Day format (MM-DD)
            DateFormat::MonthDay => {
                let parts: Vec<&str> = date_str.split('-').collect();
                if parts.len() != 2 {
                    return Err(DateTimeError::ParseError(date_str.to_string()));
                }

                let month = parts[0].parse::<u32>().map_err(|_| {
                    DateTimeError::ParseError(format!("Invalid month: {}", parts[0]))
                })?;
                let day = parts[1]
                    .parse::<u32>()
                    .map_err(|_| DateTimeError::ParseError(format!("Invalid day: {}", parts[1])))?;

                let naive_date = NaiveDate::from_ymd_opt(current_date.year(), month, day)
                    .ok_or_else(|| DateTimeError::InvalidDate(date_str.to_string()))?
                    .and_hms_opt(0, 0, 0)
                    .unwrap();

                Ok(DateTime::from_naive_utc_and_offset(naive_date, Utc))
            }

            // Single component formats
            DateFormat::Year => {
                let year = date_str.parse::<i32>().map_err(|_| {
                    DateTimeError::ParseError(format!("Invalid year: {}", date_str))
                })?;

                let naive_date = NaiveDate::from_ymd_opt(year, 1, 1)
                    .ok_or_else(|| DateTimeError::InvalidDate(date_str.to_string()))?
                    .and_hms_opt(0, 0, 0)
                    .unwrap();

                Ok(DateTime::from_naive_utc_and_offset(naive_date, Utc))
            }

            DateFormat::Month => {
                let month = date_str.parse::<u32>().map_err(|_| {
                    DateTimeError::ParseError(format!("Invalid month: {}", date_str))
                })?;

                let naive_date = NaiveDate::from_ymd_opt(current_date.year(), month, 1)
                    .ok_or_else(|| DateTimeError::InvalidDate(date_str.to_string()))?
                    .and_hms_opt(0, 0, 0)
                    .unwrap();

                Ok(DateTime::from_naive_utc_and_offset(naive_date, Utc))
            }

            DateFormat::Day => {
                let day = date_str
                    .parse::<u32>()
                    .map_err(|_| DateTimeError::ParseError(format!("Invalid day: {}", date_str)))?;

                let naive_date =
                    NaiveDate::from_ymd_opt(current_date.year(), current_date.month(), day)
                        .ok_or_else(|| DateTimeError::InvalidDate(date_str.to_string()))?
                        .and_hms_opt(0, 0, 0)
                        .unwrap();

                Ok(DateTime::from_naive_utc_and_offset(naive_date, Utc))
            }
        }
    }
}
impl From<DateFormat> for i32 {
    fn from(format: DateFormat) -> i32 {
        format as i32
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(into = "i32", from = "i32")]
pub enum TimeFormat {
    H24 = 0, // 23:59
    H12 = 1, // 11:59 PM
}

impl From<TimeFormat> for i32 {
    fn from(format: TimeFormat) -> i32 {
        format as i32
    }
}

impl From<i32> for TimeFormat {
    fn from(value: i32) -> Self {
        match value {
            1 => TimeFormat::H12,
            _ => TimeFormat::H24,
        }
    }
}

#[derive(Debug, thiserror::Error)]
pub enum DateTimeError {
    #[error("Failed to parse date string: {0}")]
    ParseError(String),
    #[error("Invalid date: {0}")]
    InvalidDate(String),
    #[error("Invalid timestamp: {0}")]
    InvalidTimestamp(i64),
    #[error("Invalid timezone: {0}")]
    InvalidTimezone(String),
}

impl fmt::Display for DateFormat {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let format_str = match self {
            DateFormat::YearMonthDaySlash => "YYYY/MM/DD",
            DateFormat::YearMonthDayDash => "YYYY-MM-DD",
            DateFormat::DayMonthYear => "DD/MM/YYYY",
            DateFormat::YearMonth => "YYYY-MM",
            DateFormat::MonthDay => "MM-DD",
            DateFormat::Year => "YYYY",
            DateFormat::Month => "MM",
            DateFormat::Day => "DD",
        };
        write!(f, "{}", format_str)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::TimeZone;
    #[test]
    fn test_timezone_conversion() {
        let format = DateFormat::YearMonthDayDash;
        let date_str = "2024-03-21";
        
        let result = format.parse_with_timezone(date_str, Some("America/New_York")).unwrap();
        let formatted = format.format_with_timezone(&result).unwrap();
        
        assert_eq!(result.timezone, Some("America/New_York".to_string()));
        assert!(formatted.starts_with("2024"));
    }

    #[test]
    fn test_invalid_timezone() {
        let format = DateFormat::YearMonthDayDash;
        let result = format.parse_with_timezone("2024-03-21", Some("Invalid/Timezone"));
        assert!(matches!(result.unwrap_err(), DateTimeError::InvalidTimezone(_)));
    }
    #[test]
    fn test_date_format_conversion() {
        let test_cases = vec![
            (1, DateFormat::YearMonthDaySlash),
            (2, DateFormat::YearMonthDayDash),
            (3, DateFormat::DayMonthYear),
            (4, DateFormat::YearMonth),
            (5, DateFormat::MonthDay),
            (6, DateFormat::Year),
            (7, DateFormat::Month),
            (8, DateFormat::Day),
        ];

        for (num, expected_format) in test_cases {
            let format: DateFormat = num.into();
            assert_eq!(format, expected_format);
            assert_eq!(i32::from(format), num);
        }
    }

    #[test]
    fn test_format_datetime() {
        let test_date = Utc.with_ymd_and_hms(2024, 3, 21, 0, 0, 0).unwrap();

        let test_cases = vec![
            (DateFormat::YearMonthDaySlash, "2024/03/21"),
            (DateFormat::YearMonthDayDash, "2024-03-21"),
            (DateFormat::DayMonthYear, "21/03/2024"),
            (DateFormat::YearMonth, "2024-03"),
            (DateFormat::MonthDay, "03-21"),
            (DateFormat::Year, "2024"),
            (DateFormat::Month, "03"),
            (DateFormat::Day, "21"),
        ];

        for (format, expected) in test_cases {
            let result = format.format_datetime(&test_date);
            assert_eq!(result, expected, "Failed formatting with {:?}", format);
        }
    }

    #[test]
    fn test_parse_year_month() {
        let format = DateFormat::YearMonth;
        let result = format.parse_datetime("2024-03");
        assert!(result.is_ok(), "Failed to parse year-month format");

        let datetime = result.unwrap();
        assert_eq!(datetime.year(), 2024);
        assert_eq!(datetime.month(), 3);
        assert_eq!(datetime.day(), 1); // Should default to first day of month
    }

    #[test]
    fn test_parse_month_day() {
        let format = DateFormat::MonthDay;
        let result = format.parse_datetime("03-21");
        assert!(result.is_ok(), "Failed to parse month-day format");

        let datetime = result.unwrap();
        let current_year = Local::now().year();
        assert_eq!(datetime.year(), current_year);
        assert_eq!(datetime.month(), 3);
        assert_eq!(datetime.day(), 21);
    }

    #[test]
    fn test_invalid_partial_dates() {
        let test_cases = vec![
            (DateFormat::YearMonth, "2024"),    // Missing month
            (DateFormat::YearMonth, "2024-13"), // Invalid month
            (DateFormat::MonthDay, "00-21"),    // Invalid month
            (DateFormat::MonthDay, "12-32"),    // Invalid day
        ];

        for (format, invalid_date) in test_cases {
            let result = format.parse_datetime(invalid_date);
            assert!(
                result.is_err(),
                "Expected error parsing {} with format {:?}",
                invalid_date,
                format
            );
        }
    }

    #[test]
    fn test_invalid_date_parsing() {
        let test_cases = vec![
            (DateFormat::YearMonthDaySlash, "invalid"),
            (DateFormat::YearMonthDayDash, "2024-13-45"), // Invalid month and day
            (DateFormat::DayMonthYear, "32/13/2024"),     // Invalid day and month
            (DateFormat::YearMonth, "2024-13"),           // Invalid month
            (DateFormat::MonthDay, "13-32"),              // Invalid month and day
            (DateFormat::Year, "invalid"),
            (DateFormat::Month, "13"), // Invalid month
            (DateFormat::Day, "32"),   // Invalid day
        ];

        for (format, invalid_date) in test_cases {
            let result = format.parse_datetime(invalid_date);
            assert!(
                result.is_err(),
                "Expected error parsing {} with format {:?}",
                invalid_date,
                format
            );
        }
    }

    #[test]
    fn test_timestamp_conversion() {
        let test_cases = vec![
            (DateFormat::YearMonthDayDash, "2024-03-21"),
            (DateFormat::YearMonthDaySlash, "2024/03/21"),
            (DateFormat::DayMonthYear, "21/03/2024"),
        ];

        for (format, date_str) in test_cases {
            // Convert string to timestamp
            let timestamp = format.to_timestamp(date_str).unwrap();

            // Convert timestamp back to string
            let formatted = format.format_timestamp(timestamp).unwrap();

            // For full date formats, the roundtrip should match exactly
            match format {
                DateFormat::YearMonthDayDash
                | DateFormat::YearMonthDaySlash
                | DateFormat::DayMonthYear => {
                    assert_eq!(formatted, date_str);
                }
                _ => {
                    // For partial formats, verify the relevant parts
                    let reparsed = format.parse_datetime(&formatted).unwrap();
                    match format {
                        DateFormat::YearMonth => {
                            assert_eq!(reparsed.format("%Y-%m").to_string(), date_str);
                        }
                        DateFormat::MonthDay => {
                            assert_eq!(reparsed.format("%m-%d").to_string(), date_str);
                        }
                        _ => {}
                    }
                }
            }
        }
    }

    #[test]
    fn test_display_format() {
        let test_cases = vec![
            (DateFormat::YearMonthDaySlash, "YYYY/MM/DD"),
            (DateFormat::YearMonthDayDash, "YYYY-MM-DD"),
            (DateFormat::DayMonthYear, "DD/MM/YYYY"),
            (DateFormat::YearMonth, "YYYY-MM"),
            (DateFormat::MonthDay, "MM-DD"),
            (DateFormat::Year, "YYYY"),
            (DateFormat::Month, "MM"),
            (DateFormat::Day, "DD"),
        ];

        for (format, expected) in test_cases {
            assert_eq!(format.to_string(), expected);
        }
    }

    #[test]
    fn test_edge_cases() {
        // Test leap year
        let result = DateFormat::YearMonthDayDash.parse_datetime("2024-02-29");
        assert!(result.is_ok());

        // Test non-leap year
        let result = DateFormat::YearMonthDayDash.parse_datetime("2023-02-29");
        assert!(result.is_err());

        // Test end of month dates
        let valid_dates = vec![
            "2024-01-31",
            "2024-03-31",
            "2024-04-30",
            "2024-05-31",
            "2024-06-30",
            "2024-07-31",
            "2024-08-31",
            "2024-09-30",
            "2024-10-31",
            "2024-11-30",
            "2024-12-31",
        ];

        for date in valid_dates {
            let result = DateFormat::YearMonthDayDash.parse_datetime(date);
            assert!(result.is_ok(), "Failed to parse valid date: {}", date);
        }
    }
}