minimo 0.5.42

terminal ui library combining alot of things from here and there and making it slightly easier to play with
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
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
use std::fmt;
use std::cmp::Ordering;
use std::ops::{Add, Sub, Mul, Div, Neg};
use std::time::SystemTime;

use crate::Location;

const SECONDS_PER_DAY: i64 = 86400;
const SECONDS_PER_HOUR: i64 = 3600;
const SECONDS_PER_MINUTE: i64 = 60;
const NANOS_PER_SECOND: i64 = 1_000_000_000;

pub trait DateLike {
    fn year(&self) -> i32;
    fn month(&self) -> u32;
    fn date(&self) -> u32;
    fn hour(&self) -> u32;
    fn minute(&self) -> u32;
    fn second(&self) -> u32;
    fn week(&self) -> u32;
    fn add_duration(&self, duration: Duration) -> Self;
    fn sub_duration(&self, duration: Duration) -> Self;
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Date {
    year: i32,
    month: u32,
    day: u32,
}

impl Date {
    pub fn new(year: i32, month: u32, day: u32) -> Option<Self> {
        if is_valid_date(year, month, day) {
            Some(Self { year, month, day })
        } else {
            None
        }
    }

    pub fn next_day(&self) -> Self {
        let (y, m, d) = civil_from_days(days_from_civil(self.year, self.month, self.day) + 1);
        Self::new(y, m as u32, d as u32).unwrap()
    }

    pub fn previous_day(&self) -> Self {
        let (y, m, d) = civil_from_days(days_from_civil(self.year, self.month, self.day) - 1);
        Self::new(y, m as u32, d as u32).unwrap()
    }

    pub fn add_years(&self, years: i32) -> Self {
        let new_year = self.year + years;
        let new_day = std::cmp::min(self.day, days_in_month(new_year, self.month));
        Self::new(new_year, self.month, new_day).unwrap()
    }

    pub fn add_months(&self, months: i32) -> Self {
        //get the month and year from the date
        let mut months = months;
        let mut year = self.year;
        let mut month = self.month;
        let day = self.day;
        //add the months to the date and if new month is greater than 12, add the years to the date
        while months > 0 {
            if month == 12 {
                year += 1;
                month = 1;
            } else {
                month += 1;
            }
            months -= 1;
        }

        Self::new(year, month, day).unwrap()
    }

    pub fn add_days(&self, days: i32) -> Self {
        let mut days = days;
        let mut year = self.year;
        let mut month = self.month;
        let mut day = self.day;
        while days > 0 {
            day += 1;
            if day > days_in_month(year, month) {
                month += 1;
                day = 1;
            }
            if month > 12 {
                year += 1;
                month = 1;
            }
            days -= 1;
        }
        Self::new(year, month, day).unwrap()    
    }

    pub fn sub_years(&self, years: i32) -> Self {
        let new_year = self.year - years;
        let new_day = std::cmp::min(self.day, days_in_month(new_year, self.month));
        Self::new(new_year, self.month, new_day).unwrap()
    }

    pub fn sub_months(&self, months: i32) -> Self {
        let mut months = months;
        let mut year = self.year;
        let mut month = self.month;
        let day = self.day;
        while months < 0 {
            if month == 1 {
                year -= 1;
                month = 12;
            } else {
                month -= 1;
            }
            months -= 1;
        }
        Self::new(year, month, day).unwrap()
    }

    pub fn sub_days(&self, days: i32) -> Self {
        let mut days = days;
        let mut year = self.year;
        let mut month = self.month;
        let mut day = self.day;
        while days < 0 {
            day -= 1;
            if day == 0 {
                month -= 1;
                if month == 0 {
                    year -= 1;
                    month = 12;
                }
                day = days_in_month(year, month);
            }
            days -= 1;
        }   
        Self::new(year, month, day).unwrap()
    }

    pub fn with_year(self, year: i32) -> Self {
        let new_day = std::cmp::min(self.day, days_in_month(year, self.month));
        Self::new(year, self.month, new_day).unwrap()
    }

    pub fn with_month(self, month: u32) -> Self {
        let new_day = std::cmp::min(self.day, days_in_month(self.year, month));
        Self::new(self.year, month, new_day).unwrap()
    }

    pub fn with_day(self, day: u32) -> Self {
        let new_day = std::cmp::min(day, days_in_month(self.year, self.month));
        Self::new(self.year, self.month, new_day).unwrap()
    }
    
     pub fn now() -> Self {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("Time went backwards");
        let days_since_epoch = now.as_secs() / 86400;
        let (year, month, day) = civil_from_days(days_since_epoch as i32);
        Self::new(year, month as u32, day as u32).unwrap()
     }
}

impl DateLike for Date {
    fn year(&self) -> i32 { self.year }
    fn month(&self) -> u32 { self.month }
    fn date(&self) -> u32 { self.day }
    fn hour(&self) -> u32 { 0 }
    fn minute(&self) -> u32 { 0 }
    fn second(&self) -> u32 { 0 }

    fn week(&self) -> u32 {
        let days = days_from_civil(self.year, self.month, self.day) + 4;
        let weeks = days / 7;
        let remainder = days % 7;
        let week = (weeks % 52 + 52) % 52 + 1;
        week as u32
    }

    fn add_duration(&self, duration: Duration) -> Self {
        self.add_days(duration.total_seconds() as i32 / SECONDS_PER_DAY as i32)
    }

    fn sub_duration(&self, duration: Duration) -> Self {
        self.add_duration(-duration)
    }
}

impl fmt::Display for Date {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:04}-{:02}-{:02}", self.year, self.month, self.day)
    }
}

impl Default for Date {
    fn default() -> Self {
        Date::new(1970, 1, 1).unwrap()
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Time {
    hour: u32,
    minute: u32,
    second: u32,
}

impl Time {
    pub fn new(hour: u32, minute: u32, second: u32) -> Self {
        let total_seconds = hour * 3600 + minute * 60 + second;
        let normalized_hour = (total_seconds / 3600) % 24;
        let normalized_minute = (total_seconds % 3600) / 60;
        let normalized_second = total_seconds % 60;
        Self {
            hour: normalized_hour,
            minute: normalized_minute,
            second: normalized_second,
        }
    }

    pub fn from_hours(hours: f64) ->  Self {
       
            let total_seconds = (hours * 3600.0).round() as u32;
            let hour = total_seconds / 3600;
            let minute = (total_seconds % 3600) / 60;
            let second = total_seconds % 60;
            Self::new(hour, minute, second)
      
    }

    pub fn hour(&self) -> u32 { self.hour }
    pub fn minute(&self) -> u32 { self.minute }
    pub fn second(&self) -> u32 { self.second }

    pub fn hour_fraction(&self) -> f64 {
        self.hour as f64 + self.minute as f64 / 60.0 + self.second as f64 / 3600.0
    }
}

impl fmt::Display for Time {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:02}:{:02}:{:02}", self.hour, self.minute, self.second)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TimeZone {
    offset: i32,
}

impl TimeZone {
    pub fn new(hours: f32) -> Self {
        let total_minutes = (hours * 60.0).round() as i32;
        Self { offset: total_minutes }
    }

    pub fn utc() -> Self {
        Self { offset: 0 }
    }

    pub fn offset_seconds(&self) -> i32 {
        self.offset * 60
    }
}

impl Default for TimeZone {
    fn default() -> Self {
        Self::utc()
    }
}

impl From<f32> for TimeZone {
    fn from(offset: f32) -> Self {
        Self::new(offset)
    }
}

impl From<Location> for TimeZone {
    fn from(location: Location) -> Self {
        Self::new(location.timezone_offset() as f32 / 60.0)
    }
}

impl fmt::Display for TimeZone {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let hours = self.offset / 60;
        let minutes = self.offset.abs() % 60;
        write!(f, "{:+03}:{:02}", hours, minutes)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DateTime {
    date: Date,
    time: Time,
    timezone: TimeZone,
}

impl DateTime {
    pub fn new(date: Date, time: Time, timezone: TimeZone) -> Self {
        Self { date, time, timezone }
    }

    pub fn from_timestamp(timestamp: i64, timezone: TimeZone) -> Self {
        let days = timestamp / SECONDS_PER_DAY;
        let seconds = timestamp % SECONDS_PER_DAY;
        let (y, m, d) = civil_from_days(days as i32);
        let date = Date::new(y, m as u32, d as u32).unwrap();
        let hour = (seconds / SECONDS_PER_HOUR) as u32;
        let minute = ((seconds % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE) as u32;
        let second = (seconds % SECONDS_PER_MINUTE) as u32;
        let time = Time::new(hour, minute, second);
        Self { date, time, timezone }
    }

    pub fn to_utc(&self) -> Self {
        let timestamp = self.timestamp();
        Self::from_timestamp(timestamp, TimeZone::utc())
    }

    pub fn with_timezone(&self, timezone: TimeZone) -> Self {
        let timestamp = self.timestamp();
        Self::from_timestamp(timestamp, timezone)
    }

    pub fn timestamp(&self) -> i64 {
        let days = days_from_civil(self.date.year, self.date.month, self.date.day);
        let seconds = self.time.hour as i64 * SECONDS_PER_HOUR
            + self.time.minute as i64 * SECONDS_PER_MINUTE
            + self.time.second as i64;
        days as i64 * SECONDS_PER_DAY + seconds - self.timezone.offset_seconds() as i64
    }

    pub fn date(&self) -> Date { self.date }
    pub fn time(&self) -> Time { self.time }
    pub fn timezone(&self) -> TimeZone { self.timezone }

    pub fn year(&self) -> i32 { self.date.year }
    pub fn month(&self) -> u32 { self.date.month }
    pub fn day(&self) -> u32 { self.date.day }
    pub fn hour(&self) -> u32 { self.time.hour }
    pub fn minute(&self) -> u32 { self.time.minute }
    pub fn second(&self) -> u32 { self.time.second }

    pub fn hour_fraction(&self) -> f64 {
        self.time.hour_fraction()
    }

    pub fn add_years(&self, years: i32) -> Self {
        let new_date = self.date.add_years(years);
        Self::new(new_date, self.time, self.timezone)
    }

    pub fn add_months(&self, months: i32) -> Self {
        let new_date = self.date.add_months(months);
        Self::new(new_date, self.time, self.timezone)
    }

    pub fn add_days(&self, days: i32) -> Self {
        let new_date = self.date.add_days(days);
        Self::new(new_date, self.time, self.timezone)
    }

    pub fn add_hours(&self, hours: i32) -> Self {
        self.add_duration(Duration::from_hours(hours as i64))
    }

    pub fn add_minutes(&self, minutes: i32) -> Self {
        self.add_duration(Duration::from_minutes(minutes as i64))
    }

    pub fn add_seconds(&self, seconds: i32) -> Self {
        self.add_duration(Duration::from_seconds(seconds as i64))
    }

    pub fn sub_years(&self, years: i32) -> Self { self.add_years(-years) }
    pub fn sub_months(&self, months: i32) -> Self { self.add_months(-months) }
    pub fn sub_days(&self, days: i32) -> Self { self.add_days(-days) }
    pub fn sub_hours(&self, hours: i32) -> Self { self.add_hours(-hours) }
    pub fn sub_minutes(&self, minutes: i32) -> Self { self.add_minutes(-minutes) }
    pub fn sub_seconds(&self, seconds: i32) -> Self { self.add_seconds(-seconds) }

    pub fn create_utc(year: i32, month: u32, day: u32, hour: f64) -> Self {
        let date = Date::new(year, month, day).unwrap();
        let time = Time::from_hours(hour);
        Self::new(date, time, TimeZone::utc())
    }

    pub fn create_local(year: i32, month: u32, day: u32, hour: f64, timezone_offset: f32) -> Self {
        let date = Date::new(year, month, day).unwrap();
        let time = Time::from_hours(hour);
        let timezone = TimeZone::new(timezone_offset);
        Self::new(date, time, timezone)
    }

    pub fn at_location(location: impl Into<Location>, datetime:  impl Into<DateTime>) -> Self {
        let datetime = datetime.into();
        let location = location.into();
        let timezone = TimeZone::from(location);
        datetime.with_timezone(timezone)
    }

    pub fn time_distance(&self, other: Self) -> Duration {
        Duration::from_seconds(self.timestamp() - other.timestamp())
    }
    
    pub fn from_date_time(date: impl Into<Date>, time: impl Into<Time>, timezone: impl Into<TimeZone>) -> Self {
        let date = date.into();
        let time = time.into();
        let timezone = timezone.into();
        Self::new(date, time, timezone)
    }
    
   pub fn now() -> Self {
       let date = Date::new(1970, 1, 1).unwrap();
       let time = Time::new(0, 0, 0);
       let timezone = TimeZone::utc();
       Self::new(date, time, timezone)
   }
}

impl DateLike for DateTime {
    fn year(&self) -> i32 { self.date.year }
    fn month(&self) -> u32 { self.date.month }
    fn date(&self) -> u32 { self.date.day }
    fn hour(&self) -> u32 { self.time.hour }
    fn minute(&self) -> u32 { self.time.minute }
    fn second(&self) -> u32 { self.time.second }
    fn week(&self) -> u32 { self.date.week() }

    fn add_duration(&self, duration: Duration) -> Self {
        let new_timestamp = self.timestamp() + duration.total_seconds();
        Self::from_timestamp(new_timestamp, self.timezone)
    }

    fn sub_duration(&self, duration: Duration) -> Self {
        self.add_duration(-duration)
    }
}

impl PartialOrd for DateTime {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.timestamp().partial_cmp(&other.timestamp())
    }
}

impl Ord for DateTime {
    fn cmp(&self, other: &Self) -> Ordering {
        self.timestamp().cmp(&other.timestamp())
    }
}

impl Add<Duration> for DateTime {
    type Output = Self;
    fn add(self, duration: Duration) -> Self::Output {
        self.add_duration(duration)
    }
}

 

impl Sub<Duration> for DateTime {
    type Output = Self;

    fn sub(self, duration: Duration) -> Self::Output {
        self.sub_duration(duration)
    }
}

impl Sub<DateTime> for DateTime {
    type Output = Duration;

    fn sub(self, other: DateTime) -> Duration {
        Duration::from_seconds(self.timestamp() - other.timestamp())
    }
}

impl fmt::Display for DateTime {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}{}", 
               self.year(), self.month(), self.date(), self.hour(), self.minute(), self.second(), self.timezone)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Duration {
    seconds: i64,
    nanos: i32,
}

impl Into<Duration> for (i32, u32, u32, f64) {
    fn into(self) -> Duration {
        let (year, month, day, hour) = self;
            let datetime = DateTime::create_utc(year, month, day, hour);
        datetime.time_distance(DateTime::create_utc(year, month, day, 0.0))
    }
}

impl Duration {
    pub fn new<S: Into<i64>, N: Into<i32>>(seconds: S, nanos: N) -> Self {
        let mut seconds = seconds.into();
        let mut nanos = nanos.into();

        if nanos >= NANOS_PER_SECOND as i32 || nanos <= -(NANOS_PER_SECOND as i32) {
            seconds += nanos as i64 / NANOS_PER_SECOND;
            nanos %= NANOS_PER_SECOND as i32;
        }

        if seconds > 0 && nanos < 0 {
            seconds -= 1;
            nanos += NANOS_PER_SECOND as i32;
        } else if seconds < 0 && nanos > 0 {
            seconds += 1;
            nanos -= NANOS_PER_SECOND as i32;
        }

        Self { seconds, nanos }
    }

    pub fn from_seconds<S: Into<i64>>(seconds: S) -> Self {
        Self::new(seconds, 0)
    }

    pub fn from_days<D: Into<i64>>(days: D) -> Self {
        Self::from_seconds(days.into() * SECONDS_PER_DAY)
    }

    pub fn from_years<Y: Into<i64>>(years: Y) -> Self {
        Self::from_seconds(years.into() * SECONDS_PER_DAY * 365)
    }

    pub fn from_hours<H: Into<i64>>(hours: H) -> Self {
        Self::from_seconds(hours.into() * SECONDS_PER_HOUR)
    }

    pub fn from_minutes<M: Into<i64>>(minutes: M) -> Self {
        Self::from_seconds(minutes.into() * SECONDS_PER_MINUTE)
    }

    pub fn total_seconds(&self) -> i64 {
        self.seconds
    }

    pub fn abs(&self) -> Self {
        if self.seconds < 0 || (self.seconds == 0 && self.nanos < 0) {
            self.neg()
        } else {
            *self
        }
    }
}

impl Add for Duration {
    type Output = Self;

    fn add(self, other: Self) -> Self::Output {
        Self::new(
            self.seconds + other.seconds,
            self.nanos + other.nanos,
        )
    }
}

impl Sub for Duration {
    type Output = Self;

    fn sub(self, other: Self) -> Self::Output {
        Self::new(
            self.seconds - other.seconds,
            self.nanos - other.nanos,
        )
    }
}

impl Mul<i64> for Duration {
    type Output = Self;

    fn mul(self, rhs: i64) -> Self::Output {
        Self::new(self.seconds * rhs, self.nanos * rhs as i32)
    }
}

impl Div<i64> for Duration {
    type Output = Self;

    fn div(self, rhs: i64) -> Self::Output {
        let total_nanos = self.seconds * NANOS_PER_SECOND + self.nanos as i64;
        let result_nanos = total_nanos / rhs;
        Self::new(result_nanos / NANOS_PER_SECOND, (result_nanos % NANOS_PER_SECOND) as i32)
    }
}

impl Neg for Duration {
    type Output = Self;

    fn neg(self) -> Self::Output {
        Self::new(-self.seconds, -self.nanos)
    }
}

impl fmt::Display for Duration {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut first = true;
        let abs = self.abs();

        if self.seconds < 0 || self.nanos < 0 {
            write!(f, "-")?;
        }

        let components = [
            (abs.seconds / SECONDS_PER_DAY, "d"),
            ((abs.seconds % SECONDS_PER_DAY) / SECONDS_PER_HOUR, "h"),
            ((abs.seconds % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE, "m"),
            (abs.seconds % SECONDS_PER_MINUTE, "s"),
        ];

        for (value, unit) in components.iter() {
            if *value > 0 || (first && *unit == "s") {
                if !first {
                    write!(f, " ")?;
                }
                write!(f, "{}{}", value, unit)?;
                first = false;
            }
        }

        if abs.nanos > 0 {
            write!(f, ".{:09}s", abs.nanos)?;
        }

        Ok(())
    }
}

// Utility functions

pub fn is_valid_date(year: i32, month: u32, day: u32) -> bool {
    if year < 1 || month < 1 || month > 12 || day < 1 {
        return false;
    }

    let days_in_month = match month {
        4 | 6 | 9 | 11 => 30,
        2 => {
            if is_leap_year(year) {
                29
            } else {
                28
            }
        }
        _ => 31,
    };

    day <= days_in_month
}

pub fn is_leap_year(year: i32) -> bool {
    (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
}

pub fn days_before_year(year: i32) -> i32 {
    365 * (year - 1) + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400
}

pub fn civil_from_days(days: i32) -> (i32, i32, i32) {
    let z = days + 719468;
    let era = (if z >= 0 { z } else { z - 146096 }) / 146097;
    let doe = z - era * 146097;
    let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
    let y = yoe + era * 400;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let d = doy - (153 * mp + 2) / 5 + 1;
    let m = mp + (if mp < 10 { 3 } else { -9 });
    (y + (if m <= 2 { 1 } else { 0 }), m, d)
}

pub fn days_from_civil(y: i32, m: u32, d: u32) -> i32 {
    let y = y - (m <= 2) as i32;
    let era = (if y >= 0 { y } else { y - 399 }) / 400;
    let yoe = y - era * 400;
    let doy = (153 * (m + (if m > 10 { 11 } else { 0 }) - 1) + 2) / 5 + d - 1;
    let doe = ( era * 146097 +  (doy as i32) + 719468
    ) as i32;
    doe
}

pub fn days_in_month(year: i32, month: u32) -> u32 {
    match month {
        4 | 6 | 9 | 11 => 30,
        2 => if is_leap_year(year) { 29 } else { 28 },
        _ => 31,
    }
}

// From implementations

impl From<(i32, u32, u32)> for Date {
    fn from(ymd: (i32, u32, u32)) -> Self {
        Self::new(ymd.0, ymd.1, ymd.2).unwrap()
    }
}

impl From<(u32, u32, u32)> for Time {
    fn from(hms: (u32, u32, u32)) -> Self {
        Self::new(hms.0, hms.1, hms.2)
    }
}

impl From<(i32, i32)> for TimeZone {
    fn from(hm: (i32, i32)) -> Self {
        Self::new(hm.0 as f32 + hm.1 as f32 / 60.0)
    }
}


 impl From<std::time::SystemTime> for DateTime {
    fn from(system_time: std::time::SystemTime) -> Self {
        let duration = system_time.duration_since(std::time::UNIX_EPOCH)
            .expect("SystemTime before UNIX EPOCH!");
        let timestamp = duration.as_secs() as i64;
        DateTime::from_timestamp(timestamp, TimeZone::utc())
    }
 }

impl From<DateTime> for std::time::SystemTime {
    fn from(dt: DateTime) -> Self {
        let timestamp = dt.timestamp();
        std::time::UNIX_EPOCH + std::time::Duration::from_secs(timestamp as u64)
    }
}

impl From<(i32, u32, u32, f64)> for DateTime {
    fn from(ymdh: (i32, u32, u32, f64)) -> Self {
        let (year, month, day, hour) = ymdh;
        DateTime::create_utc(year, month, day, hour)
    }
}


impl Into<DateTime> for (i32, i32, i32, f64) {
    fn into(self) -> DateTime {
        let (year, month, day, hour) = self;
        DateTime::create_utc(year, month as u32, day as u32, hour)
    }
}