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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
use std::convert::{TryFrom, TryInto};

use smallvec::*;

use crate::convert::year::backend::{CHALAKIM_BETWEEN_MOLAD, FIRST_MOLAD};
use crate::convert::*;
use crate::holidays::get_chol_list;
use crate::holidays::get_shabbos_list;
use crate::holidays::get_special_parsha_list;
use crate::holidays::get_yt_list;
use chrono::{Duration, Utc};
use std::num::NonZeroI8;

pub(crate) mod backend;

use crate::convert::year::backend::{
    get_rosh_hashana, months_per_year, return_year_sched, CHALAKIM_PER_HOUR, EPOCH, FIRST_YEAR,
    YEAR_SCHED,
};
use crate::prelude::HebrewMonth::{Adar, Adar1, Adar2};
use crate::prelude::{ConversionError, HebrewMonth, Molad};

/// HebrewYear holds data on a given year. It's faster to get multiple HebrewDates from
/// an existing HebrewYear rather than generating each one on its own.
#[derive(Copy, Clone, Debug)]
pub struct HebrewYear {
    pub(crate) year: u64,
    pub(crate) day_of_rh: Day,
    pub(crate) day_of_next_rh: Day,
    pub(crate) months_per_year: u64,
    pub(crate) sched: [u8; 14],
    pub(crate) year_len: u64,
    pub(crate) days_since_epoch: u64,
    pub(crate) chalakim_since_epoch: u64,
}

impl HebrewYear {
    #[inline]
    pub fn new(year: u64) -> Result<HebrewYear, ConversionError> {
        //! Returns a new HebrewYear on success or a ConversionError on failure.
        //!
        //! # Arguments
        //!
        //! `year` - The Hebrew year
        //!
        if year < FIRST_YEAR + 1 {
            Err(ConversionError::YearTooSmall)
        } else {
            let cur_rh = get_rosh_hashana(year);
            let next_rh = get_rosh_hashana(year + 1);
            let days_since_epoch = cur_rh.0;
            let chalakim_since_epoch = cur_rh.2;
            let year_len = next_rh.0 - cur_rh.0;
            let months_per_year = months_per_year(year);
            let sched = &YEAR_SCHED[return_year_sched(year_len)];

            Ok(HebrewYear {
                day_of_rh: get_rosh_hashana(year).1,
                year,
                day_of_next_rh: get_rosh_hashana(year + 1).1,
                months_per_year,
                sched: sched.clone(),
                days_since_epoch,
                year_len,
                chalakim_since_epoch,
            })
        }
    }

    #[inline]
    /// Returns if this year is a leap year.
    ///
    /// ```
    /// use heca_lib::prelude::*;
    /// use heca_lib::HebrewYear;
    /// assert_eq!(HebrewYear::new(5779)?.is_leap_year(),true);
    /// # Ok::<(),ConversionError>(())
    /// ```
    pub fn is_leap_year(&self) -> bool {
        self.months_per_year == 13
    }

    #[inline]
    /// Returns the type of year.
    ///
    /// A Hebrew year can be one of 14 combinations of length and starting day.
    ///
    /// # Returns
    ///
    /// A [MonthSchedule](../heca_lib/prelude/enum.MonthSchedule.html)
    pub fn year_type(&self) -> MonthSchedule {
        if self.months_per_year == 12 {
            match self.day_of_rh {
                Day::Monday => {
                    if self.sched[1] == 30 && self.sched[2] == 30 {
                        MonthSchedule::BaShaH
                    } else if self.sched[1] == 29 && self.sched[2] == 29 {
                        MonthSchedule::BaChaG
                    } else {
                        panic!(format!(
                            "Year {} is 12 months, stars on Monday, yet has Cheshvan {} days and Kislev {} days",
                            self.year, self.sched[1], self.sched[2]
                        ))
                    }
                }
                Day::Tuesday => {
                    if self.sched[1] == 29 && self.sched[2] == 30 {
                        MonthSchedule::GaChaH
                    } else {
                        panic!(format!(
                            "Year {} is 12 months, starts on Tuesday, yet has Cheshvan {} days and Kislev {} days",
                            self.year, self.sched[1], self.sched[2]
                        ))
                    }
                }
                Day::Thursday => {
                    if self.sched[1] == 29 && self.sched[2] == 30 {
                        MonthSchedule::HaKaZ
                    } else if self.sched[1] == 30 && self.sched[2] == 30 {
                        MonthSchedule::HaShA
                    } else {
                        panic!(format!(
                            "Year {} is 12 months, starts on Thursday, yet has Cheshvan {} days and Kislev {} days",
                            self.year, self.sched[1], self.sched[2]
                        ))
                    }
                }
                Day::Shabbos => {
                    if self.sched[1] == 30 && self.sched[2] == 30 {
                        MonthSchedule::ZaShaG
                    } else if self.sched[1] == 29 && self.sched[2] == 29 {
                        MonthSchedule::ZaChA
                    } else {
                        panic!(format!(
                            "Year {} is 12 months, stars on Shabbos, yet has Cheshvan {} days and Kislev {} days",
                            self.year, self.sched[1], self.sched[2]
                        ))
                    }
                }
                x => panic!(format!("Rosh Hashana should never fall out on {:?}", x)),
            }
        } else {
            match self.day_of_rh {
                Day::Monday => {
                    if self.sched[1] == 30 && self.sched[2] == 30 {
                        MonthSchedule::BaShaZ
                    } else if self.sched[1] == 29 && self.sched[2] == 29 {
                        MonthSchedule::BaChaH
                    } else {
                        panic!(format!(
                            "Year {} is 13 months, stars on Monday, yet has Cheshvan {} days and Kislev {} days",
                            self.year, self.sched[1], self.sched[2]
                        ))
                    }
                }
                Day::Tuesday => {
                    if self.sched[1] == 29 && self.sched[2] == 30 {
                        MonthSchedule::GaKaZ
                    } else {
                        panic!(format!(
                            "Year {} is 13 months, starts on Tuesday, yet has Cheshvan {} days and Kislev {} days",
                            self.year, self.sched[1], self.sched[2]
                        ))
                    }
                }
                Day::Thursday => {
                    if self.sched[1] == 30 && self.sched[2] == 30 {
                        MonthSchedule::HaShaG
                    } else if self.sched[1] == 29 && self.sched[2] == 29 {
                        MonthSchedule::HaChA
                    } else {
                        panic!(format!(
                            "Year {} is 13 months, starts on Thursday, yet has Cheshvan {} days and Kislev {} days",
                            self.year, self.sched[1], self.sched[2]
                        ))
                    }
                }
                Day::Shabbos => {
                    if self.sched[1] == 30 && self.sched[2] == 30 {
                        MonthSchedule::ZaShaH
                    } else if self.sched[1] == 29 && self.sched[2] == 29 {
                        MonthSchedule::ZaChaG
                    } else {
                        panic!(format!(
                            "Year {} is 13 months, stars on Shabbos, yet has Cheshvan {} days and Kislev {} days",
                            self.year, self.sched[1], self.sched[2]
                        ))
                    }
                }
                x => panic!(format!("Rosh Hashana should never fall out on {:?}", x)),
            }
        }
    }

    /// Returns the year.
    ///
    /// # Examples:
    ///
    /// ```
    /// use std::num::NonZeroI8;
    /// use heca_lib::prelude::*;
    /// use heca_lib::{HebrewDate, HebrewYear};
    /// let year = HebrewYear::new(5779)?;
    /// assert_eq!(year.year(), 5779);
    /// # Ok::<(),ConversionError>(())
    /// ```
    #[inline]
    pub fn year(&self) -> u64 {
        self.year
    }
    /// Returns a HebrewDate from the current year and a supplied month and day.
    ///
    /// # Arguments:
    ///
    /// `month` - The Hebrew month.
    ///
    /// `day` - The day of the Hebrew month.
    ///
    /// # Examples:
    ///
    /// ```
    /// use std::num::NonZeroI8;
    /// use heca_lib::prelude::*;
    /// use heca_lib::{HebrewDate, HebrewYear};
    /// let year = HebrewYear::new(5779)?;
    /// assert_eq!(
    ///        year.get_hebrew_date(HebrewMonth::Tishrei, NonZeroI8::new(10).unwrap())?,
    ///        HebrewDate::from_ymd(5779, HebrewMonth::Tishrei, NonZeroI8::new(10).unwrap())?
    ///  );
    /// # Ok::<(),ConversionError>(())
    /// ```
    ///
    /// # Notes:
    ///
    /// Day must be above zero. If it's below zero, the function returns TooManyDaysInMonth. In a future release, day will be a NonZeroU8 so that it will be impossible to supply a negative number.
    #[inline]
    pub fn get_hebrew_date(
        self,
        month: HebrewMonth,
        day: NonZeroI8,
    ) -> Result<HebrewDate, ConversionError> {
        HebrewDate::from_ymd_internal(month, day, self)
    }

    pub(crate) fn get_hebrewdate_from_days_after_rh(self, amnt_days: u64) -> HebrewDate {
        let mut remainder = amnt_days - self.days_since_epoch;
        let mut month: u64 = 0;
        for days_in_month in self.sched.iter() {
            if remainder < u64::from(*days_in_month) {
                break;
            }
            month += 1;
            remainder -= u64::from(*days_in_month);
        }
        HebrewDate {
            year: self,
            month: HebrewMonth::from(month),
            day: NonZeroI8::new((remainder + 1) as i8).unwrap(),
        }
    }
    /// Returns all the days when the Torah is read.
    ///
    /// # Arguments
    ///
    /// `location` - Specify if you're looking for the calendar in Israel or in the Diaspora. Is
    /// relevent as there's only one day of Yom Tov in Israel while there are two day of Yom Tov outside.
    /// Since we don't read the Weekly Parsha on Yom Tov, in a year when the 8th day of Pesach is on a Shabbos,
    /// Israelis read the next Parsha while the Diaspora reads the Yom Tov Parsha, catching up in the summer.
    ///
    /// `yt_types` - An array containing `TorahReadingType`. This should be used as a flag to
    /// specify which types of Torah readings you want to list.
    ///
    /// # Returns
    ///
    /// Returns an array (or a vec) of days.
    ///
    /// # Note
    ///
    /// This may unsorted, and is returned under no defined order.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::num::NonZeroI8;
    /// use heca_lib::prelude::*;
    /// use heca_lib::{HebrewDate, HebrewYear};
    /// let year = HebrewYear::new(5779)?;
    /// let shabbosim = year.get_holidays(Location::Chul, &[TorahReadingType::Shabbos, TorahReadingType::SpecialParsha, TorahReadingType::Chol, TorahReadingType::YomTov]);
    /// let mut count = 0;
    /// for s in shabbosim.into_iter() {
    ///   if s.name() == TorahReading::Shabbos(Parsha::Bereishis) {
    ///     assert_eq!(s.day(), HebrewDate::from_ymd(5779,HebrewMonth::Tishrei, NonZeroI8::new(27).unwrap())?);
    ///     count += 1;
    ///   }
    ///   else if s.name() == TorahReading::SpecialParsha(SpecialParsha::Zachor) {
    ///     assert_eq!(s.day(), HebrewDate::from_ymd(5779,HebrewMonth::Adar2, NonZeroI8::new(9).unwrap())?);
    ///     count += 1;
    ///   }
    ///   else if s.name() == TorahReading::Chol(Chol::Chanukah1) {
    ///     assert_eq!(s.day(), HebrewDate::from_ymd(5779,HebrewMonth::Kislev, NonZeroI8::new(25).unwrap())?);
    ///     count += 1;
    ///   }
    ///   else if s.name() == TorahReading::YomTov(YomTov::Shavuos1) {
    ///     assert_eq!(s.day(), HebrewDate::from_ymd(5779,HebrewMonth::Sivan, NonZeroI8::new(6).unwrap())?);
    ///     count += 1;
    ///   }
    /// }
    /// assert_eq!(count,4);
    /// # Ok::<(),ConversionError>(())
    /// ```
    pub fn get_holidays(
        &self,
        location: Location,
        yt_types: &[TorahReadingType],
    ) -> SmallVec<[TorahReadingDay; 256]> {
        let mut return_vec: SmallVec<[TorahReadingDay; 256]> = SmallVec::new();
        if yt_types.contains(&TorahReadingType::YomTov) {
            return_vec.extend_from_slice(&get_yt_list(self.clone(), location));
        }
        if yt_types.contains(&TorahReadingType::Chol) {
            return_vec.extend_from_slice(&get_chol_list(self.clone()));
        }
        if yt_types.contains(&TorahReadingType::Shabbos) {
            return_vec.extend_from_slice(&get_shabbos_list(self.clone(), location));
        }
        if yt_types.contains(&TorahReadingType::SpecialParsha) {
            return_vec.extend_from_slice(&get_special_parsha_list(self.clone()));
        }
        return_vec
    }

    /// Returns the Molad of a given month, or a ConversionError if trying to get Molad of a month which is does not exist in that year.
    ///
    /// # Note:
    /// The Molad has no modern Halachic significance since Rosh Chodesh isn't derived from the Molad. However, it is useful to know as some say that one should know the Molad during the Birkas HaChodesh.
    pub fn get_molad(&self, month: HebrewMonth) -> Result<Molad, ConversionError> {
        let chalakim_since_epoch = if self.is_leap_year() {
            match month {
                HebrewMonth::Tishrei => 0,
                HebrewMonth::Cheshvan => 1,
                HebrewMonth::Kislev => 2,
                HebrewMonth::Teves => 3,
                HebrewMonth::Shvat => 4,
                Adar1 => 5,
                Adar2 => 6,
                HebrewMonth::Nissan => 7,
                HebrewMonth::Iyar => 8,
                HebrewMonth::Sivan => 9,
                HebrewMonth::Tammuz => 10,
                HebrewMonth::Av => 11,
                HebrewMonth::Elul => 12,
                Adar => {
                    return Err(ConversionError::IsLeapYear);
                }
            }
        } else {
            match month {
                HebrewMonth::Tishrei => 0,
                HebrewMonth::Cheshvan => 1,
                HebrewMonth::Kislev => 2,
                HebrewMonth::Teves => 3,
                HebrewMonth::Shvat => 4,
                HebrewMonth::Adar => 5,
                HebrewMonth::Nissan => 6,
                HebrewMonth::Iyar => 7,
                HebrewMonth::Sivan => 8,
                HebrewMonth::Tammuz => 9,
                HebrewMonth::Av => 10,
                HebrewMonth::Elul => 11,
                Adar1 => return Err(ConversionError::IsNotLeapYear),
                Adar2 => return Err(ConversionError::IsNotLeapYear),
            }
        } * CHALAKIM_BETWEEN_MOLAD
            + self.chalakim_since_epoch
            + FIRST_MOLAD;
        let minutes_since_epoch = (chalakim_since_epoch / (CHALAKIM_PER_HOUR / 60))
            .try_into()
            .unwrap();
        let remainder = (chalakim_since_epoch % (CHALAKIM_PER_HOUR / 60))
            .try_into()
            .unwrap();
        let day = EPOCH.clone() + Duration::minutes(minutes_since_epoch);
        Ok(Molad { day, remainder })
    }
}

#[test]
fn test_get_molad() {
    use chrono::prelude::*;
    let hebrew_year = HebrewYear::new(5780).unwrap();
    let p = hebrew_year.get_molad(HebrewMonth::Tishrei).unwrap();
    assert_eq!(
        p,
        Molad {
            day: Utc.ymd(2019, 9, 29).and_hms(5, 50, 0),
            remainder: 5
        }
    );
    let p = hebrew_year.get_molad(HebrewMonth::Cheshvan).unwrap();
    assert_eq!(
        p,
        Molad {
            day: Utc.ymd(2019, 10, 28).and_hms(18, 34, 0),
            remainder: 6
        }
    );

    let hebrew_year = HebrewYear::new(5781).unwrap();
    let p = hebrew_year.get_molad(HebrewMonth::Elul).unwrap();
    assert_eq!(
        p,
        Molad {
            day: Utc.ymd(2021, 8, 8).and_hms(10, 43, 0),
            remainder: 10
        }
    );

    //check error
    let hebrew_year = HebrewYear::new(5780).unwrap();
    assert_eq!(
        hebrew_year.get_molad(HebrewMonth::Adar1),
        Err(ConversionError::IsNotLeapYear)
    );

    let hebrew_year = HebrewYear::new(5779).unwrap();
    assert_eq!(
        hebrew_year.get_molad(HebrewMonth::Adar),
        Err(ConversionError::IsLeapYear)
    );
}

/// Returns a HebrewDate on success, or a ConversionError on failure.
///
/// # Arguments
/// * `date` - The Gregorian date.
///
/// # Note:
/// Hebrew days start at sundown, not midnight, so there isn't a full 1:1 mapping between
/// Gregorian days and Hebrew. So when you look up the date of Rosh Hashana 5779, most calendars will say that it's on Monday the 10th of September, 2018, while Rosh Hashana really started at sundown on the 9th of September.
///
/// I'm trying to be a _bit_ more precise, so I made the date cutoff at 6:00 PM. So for example:
///
/// ```
/// use std::num::NonZeroI8;
/// use std::convert::TryInto;
///
/// use chrono::Utc;
/// use chrono::offset::TimeZone;
/// use heca_lib::prelude::*;
/// use heca_lib::HebrewDate;
///
/// let hebrew_date: HebrewDate = Utc.ymd(2018,9,10).and_hms(17,59,59).try_into()?;
/// assert_eq!(hebrew_date,HebrewDate::from_ymd(5779,HebrewMonth::Tishrei,NonZeroI8::new(1).unwrap())?);
/// # Ok::<(),ConversionError>(())
/// ```
///
/// while
///
/// ```
/// use std::num::NonZeroI8;
/// use std::convert::TryInto;
///
/// use chrono::Utc;
/// use chrono::offset::TimeZone;
/// use heca_lib::prelude::*;
/// use heca_lib::HebrewDate;
///
///
/// let hebrew_date: HebrewDate = Utc.ymd(2018,9,10).and_hms(18,0,0).try_into()?;
/// assert_eq!(hebrew_date, HebrewDate::from_ymd(5779,HebrewMonth::Tishrei,NonZeroI8::new(2).unwrap())?);
/// # Ok::<(),ConversionError>(())
/// ```
/// # Error Values:
/// * YearTooSmall - This algorithm won't work if the year is before year 4.
///
impl TryFrom<DateTime<Utc>> for HebrewDate {
    type Error = ConversionError;
    fn try_from(original_day: DateTime<Utc>) -> Result<HebrewDate, ConversionError> {
        HebrewDate::from_gregorian(original_day)
    }
}

/// Gets the Gregorian date for the current Hebrew date.
///
/// # Notes
///
/// This function returns the DateTime of the given HebrewDate at nightfall.
///
/// For example, Yom Kippur 5779 started at sunset of September 18, 2018. So
/// ```
/// use std::num::NonZeroI8;
///
/// use chrono::prelude::*;
/// use heca_lib::prelude::*;
/// use heca_lib::HebrewDate;
///
/// let gregorian_date: DateTime<Utc> = HebrewDate::from_ymd(5779,HebrewMonth::Tishrei,NonZeroI8::new(10).unwrap())?.into();
/// assert_eq!(gregorian_date ,Utc.ymd(2018, 9,18).and_hms(18,00,00));
/// # Ok::<(),ConversionError>(())
/// ```
/// ## Algorithm:
/// The conversion is done (at the moment) according to the calculation of the Rambam (Maimonidies), as is documented in [Hilchos Kiddush Ha'chodesh](https://www.sefaria.org/Mishneh_Torah%2C_Sanctification_of_the_New_Month.6.1?lang=bi&with=all&lang2=en).
///
/// The algorithm is as follows:
///
/// 1. There are exactly 1080 Chalakim (parts) in an hour.
/// 2. There are exactly (well, not really. But it's close enough that we use that number as exact.) 29 days, 12 hours, and 793 Chalakim between new moons.
///
///  So that's the basic numbers. Regarding the calendar itself:
///
/// 3. All months are either 29 or 30 days long.
/// 4. There are either 12 or 13 months in the Hebrew calendar, depending if it's a leap year. When it's a leap year, Adar (which generally is in the late winter or early spring) is doubled into a "first Adar" (Adar1) and a "second Adar" (Adar2).
/// 5. There is a 19 year cycle of leap years. So the first two years of the cycle are regular years, the one after that's a leap year. Then another two are regular, then a leap year. Then it's regular, leap, regular, regular, leap, regular, regular, leap.
/// 6. Year 3763 was the first year of its 19 year cycle.
/// 7. Now you can calculate when's the New Moon before a given Rosh Hashana.
///
///  So how to calculate Rosh Hashana:
///
/// 8. If the New Moon is in the afternoon, Rosh Hashana is postponed to the next day.
/// 9. If Rosh Hashana's starting on a Sunday (Saturday night), Wednesday (Tuesday night), or Friday (Thursday night) - postpone it by a day.
///
///  If any of the above two conditions were fulfilled. Good. You just found Rosh Hashana. If not:
///
/// 10. If the New Moon is on a Tuesday after 3am+204 Chalakim and the coming year is not a leap year, Rosh Hashana is postponed to that upcoming Thursday instead.
/// 11. If the New Moon is on a Monday after 9am+589 Chalakim, and the previous year was a leap year, then Rosh Hashana is postponed to Tuesday.
///
///
///  Now you have all the Rosh Hashanas.
///
/// 12. In general, months alternate between “Full” (30 days long) and “Empty” (29 days long) months. So Tishrei is full, Teves is empty, Shvat is full, Adar is empty, Nissan is full.
/// 13. When the year is a leap year, Adar 1 is full and Adar 2 is empty. (So a full Shvat is followed by a full Adar1).
///
///  Knowing this, you can calculate any other date of the year.
///
///  But wait! We're playing with the date when Rosh Hashana will start, so not every year will be the same length! How do we make up these days?
///
///  So there's a last little bit:
///
/// 14. Cheshvan and Kislev are variable length months – some years both are full, some years both are empty, and some years Cheshvan is full and Kislev is empty - depending on the day Rosh Hashana starts (and the day _the next Rosh Hashana starts_) and how many days are in the year.
impl From<HebrewDate> for DateTime<Utc> {
    fn from(h: HebrewDate) -> Self {
        h.to_gregorian()
    }
}

mod test {
    #[test]
    fn make_new_year() {
        use super::*;

        for i in 4000..5000 {
            println!("{}", i);
            HebrewYear::new(i).unwrap();
        }
    }

    #[test]
    fn check_year_type() {
        use super::*;
        use chrono::prelude::*;

        for i in 3765..9999 {
            println!("{}", i);
            let y = HebrewYear::new(i).unwrap();
            let t = y.year_type();
            match t {
                MonthSchedule::GaChaH
                | MonthSchedule::BaShaH
                | MonthSchedule::BaChaH
                | MonthSchedule::ZaShaH => assert_eq!(
                    y.get_hebrew_date(HebrewMonth::Nissan, NonZeroI8::new(16).unwrap())
                        .unwrap()
                        .to_gregorian()
                        .weekday(),
                    Weekday::Thu
                ),

                MonthSchedule::HaShaG
                | MonthSchedule::ZaShaG
                | MonthSchedule::ZaChaG
                | MonthSchedule::BaChaG => assert_eq!(
                    y.get_hebrew_date(HebrewMonth::Nissan, NonZeroI8::new(16).unwrap())
                        .unwrap()
                        .to_gregorian()
                        .weekday(),
                    Weekday::Tue
                ),
                MonthSchedule::HaShA | MonthSchedule::ZaChA | MonthSchedule::HaChA => assert_eq!(
                    y.get_hebrew_date(HebrewMonth::Nissan, NonZeroI8::new(16).unwrap())
                        .unwrap()
                        .to_gregorian()
                        .weekday(),
                    Weekday::Sun
                ),
                MonthSchedule::HaKaZ | MonthSchedule::BaShaZ | MonthSchedule::GaKaZ => assert_eq!(
                    y.get_hebrew_date(HebrewMonth::Nissan, NonZeroI8::new(16).unwrap())
                        .unwrap()
                        .to_gregorian()
                        .weekday(),
                    Weekday::Sat
                ),
            }
        }
    }
}