oxedyne/fe2o3/fe2o3_datime/src/tests/julian_day_test.rs
3.1 KiB, 4 runs
created by r1870400018:8502, 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 | //! Julian day number conversion tests. |
| 2 | //! |
| 3 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 4 | //! Anthropic Claude |
| 5 | |
| 6 | use crate::{ |
| 7 | calendar::CalendarDate, |
| 8 | time::CalClockZone, |
| 9 | core::Time, |
| 10 | }; |
| 11 | |
| 12 | #[test] |
| 13 | fn test_julian_day_roundtrip() { |
| 14 | let zone = CalClockZone::utc(); |
| 15 | |
| 16 | // Test some known dates |
| 17 | let test_dates = [ |
| 18 | (2024, 6, 15), // Modern date |
| 19 | (2000, 1, 1), // Y2K |
| 20 | (1970, 1, 1), // Unix epoch |
| 21 | (1582, 10, 15), // Gregorian calendar start |
| 22 | (1, 1, 1), // Year 1 |
| 23 | ]; |
| 24 | |
| 25 | for (year, month, day) in test_dates { |
| 26 | let original = CalendarDate::new(year, month, day, zone.clone()).unwrap(); |
| 27 | let julian_day = original.to_julian_day_number(); |
| 28 | let reconstructed = CalendarDate::from_julian_day_number(julian_day, zone.clone()).unwrap(); |
| 29 | |
| 30 | assert_eq!(original.year(), reconstructed.year()); |
| 31 | assert_eq!(original.month(), reconstructed.month()); |
| 32 | assert_eq!(original.day(), reconstructed.day()); |
| 33 | |
| 34 | println!("{}-{:02}-{:02} -> Julian Day {} -> {}-{:02}-{:02}", |
| 35 | year, month, day, julian_day, |
| 36 | reconstructed.year(), reconstructed.month(), reconstructed.day()); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | #[test] |
| 41 | fn test_add_days_with_julian_arithmetic() { |
| 42 | let zone = CalClockZone::utc(); |
| 43 | let start_date = CalendarDate::new(2024, 2, 28, zone.clone()).unwrap(); // Day before leap day |
| 44 | |
| 45 | // Test crossing leap day |
| 46 | let next_day = start_date.add_days(1).unwrap(); |
| 47 | assert_eq!(next_day.year(), 2024); |
| 48 | assert_eq!(next_day.month(), 2); |
| 49 | assert_eq!(next_day.day(), 29); // Leap day |
| 50 | |
| 51 | let day_after_leap = start_date.add_days(2).unwrap(); |
| 52 | assert_eq!(day_after_leap.year(), 2024); |
| 53 | assert_eq!(day_after_leap.month(), 3); |
| 54 | assert_eq!(day_after_leap.day(), 1); // March 1st |
| 55 | |
| 56 | // Test subtracting days |
| 57 | let earlier = start_date.add_days(-28).unwrap(); |
| 58 | assert_eq!(earlier.year(), 2024); |
| 59 | assert_eq!(earlier.month(), 1); |
| 60 | assert_eq!(earlier.day(), 31); // January 31st |
| 61 | |
| 62 | println!("Start: {}", start_date.format("yyyy-MM-dd")); |
| 63 | println!("Next day: {}", next_day.format("yyyy-MM-dd")); |
| 64 | println!("Day after leap: {}", day_after_leap.format("yyyy-MM-dd")); |
| 65 | println!("Earlier: {}", earlier.format("yyyy-MM-dd")); |
| 66 | } |
| 67 | |
| 68 | #[test] |
| 69 | fn test_unix_epoch_conversion() { |
| 70 | let zone = CalClockZone::utc(); |
| 71 | let unix_epoch = CalendarDate::new(1970, 1, 1, zone.clone()).unwrap(); |
| 72 | |
| 73 | // Unix epoch should be 0 days since epoch |
| 74 | assert_eq!(unix_epoch.days_since_epoch().unwrap(), 0); |
| 75 | |
| 76 | // Test creating from 0 days since epoch |
| 77 | let reconstructed = CalendarDate::from_days_since_epoch(0, zone.clone()).unwrap(); |
| 78 | assert_eq!(reconstructed.year(), 1970); |
| 79 | assert_eq!(reconstructed.month(), 1); |
| 80 | assert_eq!(reconstructed.day(), 1); |
| 81 | |
| 82 | // Test a few days after epoch |
| 83 | let few_days_later = CalendarDate::from_days_since_epoch(100, zone.clone()).unwrap(); |
| 84 | assert_eq!(few_days_later.days_since_epoch().unwrap(), 100); |
| 85 | |
| 86 | println!("Unix epoch: {}", unix_epoch.format("yyyy-MM-dd")); |
| 87 | println!("100 days later: {}", few_days_later.format("yyyy-MM-dd")); |
| 88 | } |