oxedyne/fe2o3/fe2o3_datime/tests/clock.rs
6.8 KiB, 8 runs
created by r1870400018:6409, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 2 | //! Anthropic Claude |
| 3 | |
| 4 | use oxedyne_fe2o3_core::{ |
| 5 | prelude::*, |
| 6 | test::test_it, |
| 7 | }; |
| 8 | use oxedyne_fe2o3_datime::{ |
| 9 | clock::{ |
| 10 | ClockDuration, |
| 11 | ClockFields, |
| 12 | ClockHour, |
| 13 | ClockTime, |
| 14 | }, |
| 15 | core::Duration, |
| 16 | time::CalClockZone, |
| 17 | }; |
| 18 | |
| 19 | pub fn test_clock(filter: &str) -> Outcome<()> { |
| 20 | |
| 21 | res!(test_it(filter, &["clock_time_creation", "all", "clock", "time"], || { |
| 22 | let zone = res!(CalClockZone::new("UTC")); |
| 23 | |
| 24 | // Valid times |
| 25 | let time1 = res!(ClockTime::new(14, 30, 45, 123_456_789, zone.clone())); |
| 26 | assert_eq!(time1.hour().of(), 14); |
| 27 | assert_eq!(time1.minute().of(), 30); |
| 28 | assert_eq!(time1.second().of(), 45); |
| 29 | assert_eq!(time1.nanosecond().of(), 123_456_789); |
| 30 | |
| 31 | // Special times |
| 32 | let noon = res!(ClockTime::noon(zone.clone())); |
| 33 | assert_eq!(noon.hour().of(), 12); |
| 34 | assert_eq!(noon.minute().of(), 0); |
| 35 | |
| 36 | let midnight = res!(ClockTime::midnight(zone.clone())); |
| 37 | assert_eq!(midnight.hour().of(), 0); |
| 38 | assert_eq!(midnight.minute().of(), 0); |
| 39 | |
| 40 | // Invalid times should fail |
| 41 | assert!(ClockTime::new(24, 0, 0, 0, zone.clone()).is_err()); |
| 42 | assert!(ClockTime::new(23, 60, 0, 0, zone.clone()).is_err()); |
| 43 | assert!(ClockTime::new(23, 59, 60, 0, zone.clone()).is_err()); |
| 44 | assert!(ClockTime::new(23, 59, 59, 1_000_000_000, zone.clone()).is_err()); |
| 45 | Ok(()) |
| 46 | })); |
| 47 | |
| 48 | res!(test_it(filter, &["clock_duration", "all", "clock", "duration"], || { |
| 49 | // Test duration creation and arithmetic |
| 50 | let dur1 = ClockDuration::from_seconds(3661); // 1 hour, 1 minute, 1 second |
| 51 | assert_eq!(res!(dur1.to_seconds()), 3661); |
| 52 | |
| 53 | let dur2 = ClockDuration::from_millis(123_456); |
| 54 | assert_eq!(dur2.total_millis(), 123_456); |
| 55 | |
| 56 | let dur3 = ClockDuration::from_nanos(123_456_789); |
| 57 | assert_eq!(res!(dur3.to_nanos()), 123_456_789); |
| 58 | |
| 59 | // Test duration arithmetic |
| 60 | let sum = dur1.plus(&ClockDuration::from_seconds(60)); |
| 61 | assert_eq!(res!(sum.to_seconds()), 3721); |
| 62 | |
| 63 | let diff = dur1.minus(&ClockDuration::from_seconds(61)); |
| 64 | assert_eq!(res!(diff.to_seconds()), 3600); |
| 65 | Ok(()) |
| 66 | })); |
| 67 | |
| 68 | res!(test_it(filter, &["clock_fields", "all", "clock", "fields"], || { |
| 69 | // Test clock fields normalization |
| 70 | let mut fields = ClockFields::from_time(10, 30, 90, 0); // 90 seconds should normalize |
| 71 | fields.normalize(); |
| 72 | assert_eq!(fields.hour, 10); |
| 73 | assert_eq!(fields.minute, 31); |
| 74 | assert_eq!(fields.second, 30); |
| 75 | |
| 76 | // Test negative normalization - create with valid time then modify |
| 77 | let mut fields2 = ClockFields::from_time(10, 30, 0, 0); |
| 78 | fields2.second = -70; // -70 seconds |
| 79 | fields2.normalize(); |
| 80 | assert_eq!(fields2.hour, 10); |
| 81 | assert_eq!(fields2.minute, 28); |
| 82 | assert_eq!(fields2.second, 50); |
| 83 | |
| 84 | // Test arithmetic |
| 85 | let mut fields3 = ClockFields::from_time(23, 45, 30, 0); |
| 86 | let fields4 = ClockFields::from_time(0, 30, 45, 0); |
| 87 | fields3.add(&fields4); |
| 88 | fields3.normalize(); |
| 89 | assert_eq!(fields3.hour, 0); // Wrapped to next day |
| 90 | assert_eq!(fields3.minute, 16); |
| 91 | assert_eq!(fields3.second, 15); |
| 92 | Ok(()) |
| 93 | })); |
| 94 | |
| 95 | res!(test_it(filter, &["clock_hour_12", "all", "clock", "hour", "12hour"], || { |
| 96 | // Test 12-hour format |
| 97 | let hour0 = res!(ClockHour::new(0)); |
| 98 | assert_eq!(hour0.to_twelve_hour(), (12, false)); // 12 AM |
| 99 | |
| 100 | let hour12 = res!(ClockHour::new(12)); |
| 101 | assert_eq!(hour12.to_twelve_hour(), (12, true)); // 12 PM |
| 102 | |
| 103 | let hour13 = res!(ClockHour::new(13)); |
| 104 | assert_eq!(hour13.to_twelve_hour(), (1, true)); // 1 PM |
| 105 | |
| 106 | let hour23 = res!(ClockHour::new(23)); |
| 107 | assert_eq!(hour23.to_twelve_hour(), (11, true)); // 11 PM |
| 108 | |
| 109 | // Test from 12-hour |
| 110 | let hour_am = res!(ClockHour::from_12_hour(10, false)); |
| 111 | assert_eq!(hour_am.of(), 10); |
| 112 | |
| 113 | let hour_pm = res!(ClockHour::from_12_hour(10, true)); |
| 114 | assert_eq!(hour_pm.of(), 22); |
| 115 | |
| 116 | let hour_12am = res!(ClockHour::from_12_hour(12, false)); |
| 117 | assert_eq!(hour_12am.of(), 0); |
| 118 | |
| 119 | let hour_12pm = res!(ClockHour::from_12_hour(12, true)); |
| 120 | assert_eq!(hour_12pm.of(), 12); |
| 121 | Ok(()) |
| 122 | })); |
| 123 | |
| 124 | res!(test_it(filter, &["time_conversions", "all", "clock", "time", "conversion"], || { |
| 125 | let zone = res!(CalClockZone::new("UTC")); |
| 126 | let time = res!(ClockTime::new(14, 30, 45, 123_456_789, zone.clone())); |
| 127 | |
| 128 | // Test conversions |
| 129 | let nanos = time.to_nanos_of_day(); |
| 130 | assert_eq!(nanos, (14 * 3600 + 30 * 60 + 45) * 1_000_000_000 + 123_456_789); |
| 131 | |
| 132 | let millis = time.millis_of_day(); |
| 133 | assert_eq!(millis, (14 * 3600 + 30 * 60 + 45) * 1000 + 123); |
| 134 | |
| 135 | // Test from conversions |
| 136 | let time2 = res!(ClockTime::from_nanos_of_day(nanos, zone)); |
| 137 | assert_eq!(time2.hour().of(), 14); |
| 138 | assert_eq!(time2.minute().of(), 30); |
| 139 | assert_eq!(time2.second().of(), 45); |
| 140 | assert_eq!(time2.nanosecond().of(), 123_456_789); |
| 141 | Ok(()) |
| 142 | })); |
| 143 | |
| 144 | res!(test_it(filter, &["time_arithmetic", "all", "clock", "time", "arithmetic"], || { |
| 145 | let zone = res!(CalClockZone::new("UTC")); |
| 146 | let time1 = res!(ClockTime::new(10, 30, 0, 0, zone.clone())); |
| 147 | |
| 148 | // Add duration |
| 149 | let time2 = res!(time1.add_duration(&ClockDuration::from_seconds(3600))); // Add 1 hour |
| 150 | assert_eq!(time2.hour().of(), 11); |
| 151 | assert_eq!(time2.minute().of(), 30); |
| 152 | |
| 153 | // Add across midnight |
| 154 | let time3 = res!(ClockTime::new(23, 30, 0, 0, zone.clone())); |
| 155 | let time4 = res!(time3.add_duration(&ClockDuration::from_seconds(3600))); // Add 1 hour |
| 156 | assert_eq!(time4.hour().of(), 0); |
| 157 | assert_eq!(time4.minute().of(), 30); |
| 158 | |
| 159 | // Duration between times |
| 160 | let duration = time1.duration_until(&time2); |
| 161 | assert_eq!(res!(duration.to_seconds()), 3600); |
| 162 | Ok(()) |
| 163 | })); |
| 164 | |
| 165 | res!(test_it(filter, &["time_comparison", "all", "clock", "time", "comparison"], || { |
| 166 | let zone = res!(CalClockZone::new("UTC")); |
| 167 | |
| 168 | let time1 = res!(ClockTime::new(10, 30, 0, 0, zone.clone())); |
| 169 | let time2 = res!(ClockTime::new(10, 30, 0, 1, zone.clone())); |
| 170 | let time3 = res!(ClockTime::new(10, 30, 0, 0, zone.clone())); |
| 171 | |
| 172 | assert!(time1 < time2); |
| 173 | assert!(time2 > time1); |
| 174 | assert_eq!(time1, time3); |
| 175 | |
| 176 | assert!(time1.is_before(&time2)); |
| 177 | assert!(time2.is_after(&time1)); |
| 178 | assert!(!time1.is_before(&time3)); |
| 179 | Ok(()) |
| 180 | })); |
| 181 | |
| 182 | Ok(()) |
| 183 | } |