|
| 1 | +use crate::calendar::Calendar; |
| 2 | +use crate::utilities::unpack_date; |
| 3 | +use time::{Date, Month}; |
| 4 | +use RustQuant_iso::*; |
| 5 | + |
| 6 | +/// Italy calendar. |
| 7 | +pub struct Italy; |
| 8 | + |
| 9 | +impl Calendar for Italy { |
| 10 | + fn new() -> Self { |
| 11 | + Self |
| 12 | + } |
| 13 | + fn name(&self) -> &'static str { |
| 14 | + "Italy" |
| 15 | + } |
| 16 | + |
| 17 | + fn country_code(&self) -> ISO_3166 { |
| 18 | + ITALY |
| 19 | + } |
| 20 | + |
| 21 | + fn market_identifier_code(&self) -> ISO_10383 { |
| 22 | + XMIL |
| 23 | + } |
| 24 | + |
| 25 | + fn is_holiday(&self, date: Date) -> bool { |
| 26 | + let (y, m, d, _, yd, em) = unpack_date(date, false); |
| 27 | + |
| 28 | + // New Year's Day |
| 29 | + if (d == 1 && m == Month::January) |
| 30 | + // Epiphany |
| 31 | + || (d == 6 && m == Month::January) |
| 32 | + // Easter Monday |
| 33 | + || (yd == em) |
| 34 | + // Liberation Day |
| 35 | + || (d == 25 && m == Month::April) |
| 36 | + // Labour Day |
| 37 | + || (d == 1 && m == Month::May) |
| 38 | + // Republic Day |
| 39 | + || (d == 2 && m == Month::June) |
| 40 | + // Assumption of Mary |
| 41 | + || (d == 15 && m == Month::August) |
| 42 | + // All Saints' Day |
| 43 | + || (d == 1 && m == Month::November) |
| 44 | + // Immaculate Conception |
| 45 | + || (d == 8 && m == Month::December) |
| 46 | + // Christmas Day |
| 47 | + || (d == 25 && m == Month::December) |
| 48 | + // St. Stephen's Day |
| 49 | + || (d == 26 && m == Month::December) |
| 50 | + { |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | + false |
| 55 | + } |
| 56 | +} |
| 57 | +#[cfg(test)] |
| 58 | +mod test_italy { |
| 59 | + use super::*; |
| 60 | + use time::macros::date; |
| 61 | + |
| 62 | + // Test to verify the name() method. |
| 63 | + #[test] |
| 64 | + fn test_name() { |
| 65 | + let calendar = Italy::new(); |
| 66 | + assert_eq!(calendar.name(), "Italy"); |
| 67 | + } |
| 68 | + |
| 69 | + // Test to verify if weekends are not considered business days. |
| 70 | + #[test] |
| 71 | + fn test_is_weekend() { |
| 72 | + let calendar = Italy::new(); |
| 73 | + let sat = date!(2024 - 08 - 24); |
| 74 | + let sun = date!(2024 - 08 - 25); |
| 75 | + |
| 76 | + assert!(!calendar.is_business_day(sat)); |
| 77 | + assert!(!calendar.is_business_day(sun)); |
| 78 | + } |
| 79 | + |
| 80 | + // Test to verify if the is_business_day() method properly accounts for public holidays in Italy. |
| 81 | + #[test] |
| 82 | + fn test_is_public_holiday() { |
| 83 | + let calendar = Italy::new(); |
| 84 | + let new_years_day = date!(2024 - 01 - 01); |
| 85 | + let epiphany = date!(2024 - 01 - 06); |
| 86 | + let liberation_day = date!(2024 - 04 - 25); |
| 87 | + let labour_day = date!(2024 - 05 - 01); |
| 88 | + let republic_day = date!(2024 - 06 - 02); |
| 89 | + let assumption = date!(2024 - 08 - 15); |
| 90 | + let all_saints_day = date!(2024 - 11 - 01); |
| 91 | + let immaculate_conception = date!(2024 - 12 - 08); |
| 92 | + let christmas = date!(2024 - 12 - 25); |
| 93 | + let st_stephen = date!(2024 - 12 - 26); |
| 94 | + |
| 95 | + assert!(!calendar.is_business_day(new_years_day)); |
| 96 | + assert!(!calendar.is_business_day(epiphany)); |
| 97 | + assert!(!calendar.is_business_day(liberation_day)); |
| 98 | + assert!(!calendar.is_business_day(labour_day)); |
| 99 | + assert!(!calendar.is_business_day(republic_day)); |
| 100 | + assert!(!calendar.is_business_day(assumption)); |
| 101 | + assert!(!calendar.is_business_day(all_saints_day)); |
| 102 | + assert!(!calendar.is_business_day(immaculate_conception)); |
| 103 | + assert!(!calendar.is_business_day(christmas)); |
| 104 | + assert!(!calendar.is_business_day(st_stephen)); |
| 105 | + } |
| 106 | + |
| 107 | + // Test to verify if the is_business_day() method properly accounts for regular business days in Italy. |
| 108 | + #[test] |
| 109 | + fn test_is_regular_business_day() { |
| 110 | + let calendar = Italy::new(); |
| 111 | + let regular_day1 = date!(2024 - 03 - 15); |
| 112 | + let regular_day2 = date!(2024 - 07 - 11); |
| 113 | + let regular_day3 = date!(2024 - 09 - 16); |
| 114 | + let day_before_new_year = date!(2025 - 12 - 31); // Not a holiday in Italy |
| 115 | + let day_after_epiphany = date!(2025 - 01 - 07); // Not a holiday in Italy |
| 116 | + |
| 117 | + assert!(calendar.is_business_day(regular_day1)); |
| 118 | + assert!(calendar.is_business_day(regular_day2)); |
| 119 | + assert!(calendar.is_business_day(regular_day3)); |
| 120 | + assert!(calendar.is_business_day(day_before_new_year)); |
| 121 | + assert!(calendar.is_business_day(day_after_epiphany)); |
| 122 | + } |
| 123 | +} |
0 commit comments