reltime 0.1.0

Relative time representations with language support
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
//! Relative time representations with language support.
//!
//! This library provides types for expressing dates and times in human-readable forms,
//! supporting both absolute and relative representations. All types serialise to and from
//! JSON in a natural format.
//!
//! # Features
//!
//! - `swedish` (default): Enables Swedish language variants for all time types.

use chrono::{DateTime, Months, NaiveTime, Utc};
use derive_more::Display;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::{
    exact::ExactDateTime,
    language::Language,
    month::{
        April, August, December, February, January, July, June, March, May, Month, November,
        October, September,
    },
    relative::{Relative, ThisMonth, ThisWeek, Today, Tomorrow},
    traits::FromLanguage,
    weekday::{Friday, Monday, Saturday, Sunday, Thursday, Tuesday, Wednesday, Weekday},
};

pub mod exact;
pub mod language;
pub mod month;
pub mod relative;
pub mod traits;
pub mod weekday;

/// A time representation supporting relative, named, exact, and absolute forms.
///
/// Serialises as an untagged enum, allowing natural JSON representations like
/// `"Today"`, `"Monday"`, `"2025-07-29T10:30:05Z"`, etc.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq, Display)]
#[serde(untagged)]
pub enum Time {
    Relative(Relative),
    Weekday(Weekday),
    Month(Month),
    Exact(ExactDateTime),
    DateTime(DateTime<Utc>),
}

impl Time {
    /// Converts to the earliest possible timestamp, relative to the current time.
    pub fn to_chrono_min_now(self) -> DateTime<Utc> {
        self.to_chrono_min(Utc::now())
    }

    /// Converts to the earliest possible timestamp, relative to the given time.
    pub fn to_chrono_min(self, relative_to: DateTime<Utc>) -> DateTime<Utc> {
        match self {
            Time::Relative(relative) => relative.to_chrono_min(relative_to),
            Time::Weekday(weekday) => weekday.to_chrono_min(relative_to, true),
            Time::Month(month) => month
                .to_chrono_max(relative_to, true)
                .checked_sub_months(Months::new(1))
                .unwrap(),
            Time::Exact(exact) => exact.to_chrono_min(relative_to),
            Time::DateTime(date_time) => date_time,
        }
    }

    /// Converts to the latest possible timestamp, relative to the current time.
    pub fn to_chrono_max_now(self) -> DateTime<Utc> {
        self.to_chrono_max(Utc::now())
    }

    /// Converts to the latest possible timestamp, relative to the given time.
    pub fn to_chrono_max(self, relative_to: DateTime<Utc>) -> DateTime<Utc> {
        match self {
            Time::Relative(relative) => relative.to_chrono_max(relative_to),
            Time::Weekday(weekday) => weekday.to_chrono_max(relative_to, true),
            Time::Month(month) => month.to_chrono_max(relative_to, true),
            Time::Exact(exact) => exact.to_chrono_max(relative_to),
            Time::DateTime(date_time) => date_time,
        }
    }

    /// Converts a chrono timestamp to the most natural time representation.
    ///
    /// When `relative_to` is provided, attempts to express the timestamp as a relative
    /// or named time (e.g., "Today", "Monday") in the specified language.
    pub fn from_max_chrono(
        date_time: DateTime<Utc>,
        relative_to: Option<DateTime<Utc>>,
        language: Language,
    ) -> Time {
        if let Some(now) = relative_to
            && date_time.time() == NaiveTime::MIN
        {
            let today = Relative::Today(Today::from_language(language));
            if date_time == today.clone().to_chrono_max(now) {
                return Time::Relative(today);
            };

            let tomorrow = Relative::Tomorrow(Tomorrow::from_language(language));
            if date_time == tomorrow.clone().to_chrono_max(now) {
                return Time::Relative(tomorrow);
            }

            let monday = Time::Weekday(Weekday::Monday(Monday::from_language(language)));
            if date_time == monday.clone().to_chrono_max(now) {
                return monday;
            }

            let tuesday = Time::Weekday(Weekday::Tuesday(Tuesday::from_language(language)));
            if date_time == tuesday.clone().to_chrono_max(now) {
                return tuesday;
            }

            let wednesday = Time::Weekday(Weekday::Wednesday(Wednesday::from_language(language)));
            if date_time == wednesday.clone().to_chrono_max(now) {
                return wednesday;
            }

            let thursday = Time::Weekday(Weekday::Thursday(Thursday::from_language(language)));
            if date_time == thursday.clone().to_chrono_max(now) {
                return thursday;
            }

            let friday = Time::Weekday(Weekday::Friday(Friday::from_language(language)));
            if date_time == friday.clone().to_chrono_max(now) {
                return friday;
            }

            let saturday = Time::Weekday(Weekday::Saturday(Saturday::from_language(language)));
            if date_time == saturday.clone().to_chrono_max(now) {
                return saturday;
            }

            let sunday = Time::Weekday(Weekday::Sunday(Sunday::from_language(language)));
            if date_time == sunday.clone().to_chrono_max(now) {
                return sunday;
            }

            let january = Time::Month(Month::January(January::from_language(language)));
            if date_time == january.clone().to_chrono_max(now) {
                return january;
            }

            let february = Time::Month(Month::February(February::from_language(language)));
            if date_time == february.clone().to_chrono_max(now) {
                return february;
            }

            let march = Time::Month(Month::March(March::from_language(language)));
            if date_time == march.clone().to_chrono_max(now) {
                return march;
            }

            let april = Time::Month(Month::April(April::from_language(language)));
            if date_time == april.clone().to_chrono_max(now) {
                return april;
            }

            let may = Time::Month(Month::May(May::from_language(language)));
            if date_time == may.clone().to_chrono_max(now) {
                return may;
            }

            let june = Time::Month(Month::June(June::from_language(language)));
            if date_time == june.clone().to_chrono_max(now) {
                return june;
            }

            let july = Time::Month(Month::July(July::from_language(language)));
            if date_time == july.clone().to_chrono_max(now) {
                return july;
            }

            let august = Time::Month(Month::August(August::from_language(language)));
            if date_time == august.clone().to_chrono_max(now) {
                return august;
            }

            let september = Time::Month(Month::September(September::from_language(language)));
            if date_time == september.clone().to_chrono_max(now) {
                return september;
            }

            let october = Time::Month(Month::October(October::from_language(language)));
            if date_time == october.clone().to_chrono_max(now) {
                return october;
            }

            let november = Time::Month(Month::November(November::from_language(language)));
            if date_time == november.clone().to_chrono_max(now) {
                return november;
            }

            let december = Time::Month(Month::December(December::from_language(language)));
            if date_time == december.clone().to_chrono_max(now) {
                return december;
            }

            let this_week = Relative::ThisWeek(ThisWeek::from_language(language));
            if date_time == this_week.clone().to_chrono_max(now) {
                return Time::Relative(this_week);
            }

            let this_month = Relative::ThisMonth(ThisMonth::from_language(language));
            if date_time == this_month.clone().to_chrono_max(now) {
                return Time::Relative(this_month);
            }
        }

        Time::DateTime(date_time)
    }
}

#[cfg(test)]
mod tests {
    use chrono::{Datelike, Days};

    use super::*;

    /// Creates a DateTime\<Utc> for Tuesday July 29th, 2025 at 10:30:05.
    fn base_time() -> DateTime<Utc> {
        DateTime::parse_from_rfc3339("2025-07-29T10:30:05-00:00")
            .unwrap()
            .to_utc()
    }

    #[test]
    fn weekday_next_midnight() {
        let tuesday = base_time();

        let weekday = Weekday::from_chrono(tuesday, false, Language::default());

        assert_eq!(weekday, Weekday::tuesday());

        let midnight_tomorrow = weekday.to_chrono_max(tuesday, false);

        assert_eq!(
            midnight_tomorrow,
            tuesday
                .checked_add_days(Days::new(1))
                .unwrap()
                .with_time(NaiveTime::MIN)
                .unwrap()
        );
    }

    #[test]
    fn weekday_next_midnight_skipping_self() {
        let tuesday = base_time();

        let tuesday_as_weekday = Weekday::from_chrono(tuesday, false, Language::default());

        assert_eq!(tuesday_as_weekday, Weekday::tuesday());

        let midnight_next_wednesday = tuesday_as_weekday.to_chrono_max(tuesday, true);

        assert_eq!(
            midnight_next_wednesday,
            tuesday
                .checked_add_days(Days::new(8))
                .unwrap()
                .with_time(NaiveTime::MIN)
                .unwrap()
        );
    }

    #[test]
    fn midnight_tomorrow_means_today() {
        let tuesday = base_time();

        let midnight_tomorrow = tuesday
            .checked_add_days(Days::new(1))
            .unwrap()
            .with_time(NaiveTime::MIN)
            .unwrap();

        let time = Time::from_max_chrono(midnight_tomorrow, Some(tuesday), Language::default());

        assert_eq!(time, Time::Relative(Relative::today()));
    }

    #[test]
    fn midnight_in_two_days_means_tomorrow() {
        let tuesday = base_time();

        let midnight_in_two_days = tuesday
            .checked_add_days(Days::new(2))
            .unwrap()
            .with_time(NaiveTime::MIN)
            .unwrap();

        let time = Time::from_max_chrono(midnight_in_two_days, Some(tuesday), Language::default());

        assert_eq!(time, Time::Relative(Relative::tomorrow()));
    }

    #[test]
    fn weekday_chrono_min() {
        let tuesday = base_time();

        let tuesday_as_weekday = Weekday::from_chrono(tuesday, false, Language::default());

        assert_eq!(tuesday_as_weekday, Weekday::tuesday());

        let tuesday_as_weekday_min = tuesday_as_weekday.to_chrono_min(tuesday, false);

        let tuesday_min = tuesday;

        assert_eq!(tuesday_min, tuesday_as_weekday_min);
    }

    #[test]
    fn weekday_chrono_min_skipping_self() {
        let tuesday = base_time();

        let tuesday_as_weekday = Weekday::from_chrono(tuesday, false, Language::default());

        assert_eq!(tuesday_as_weekday, Weekday::tuesday());

        let next_tuesday_as_weekday_min = tuesday_as_weekday.to_chrono_min(tuesday, true);

        let next_tuesday_min = tuesday
            .checked_add_days(Days::new(7))
            .unwrap()
            .with_time(NaiveTime::MIN)
            .unwrap();

        assert_eq!(next_tuesday_min, next_tuesday_as_weekday_min);
    }

    #[test]
    fn weekday_chrono_max() {
        let tuesday = base_time();

        let tuesday_as_weekday = Weekday::from_chrono(tuesday, false, Language::default());

        assert_eq!(tuesday_as_weekday, Weekday::tuesday());

        let tuesday_as_weekday_max = tuesday_as_weekday.to_chrono_max(tuesday, false);

        let tuesday_max = tuesday
            .checked_add_days(Days::new(1))
            .unwrap()
            .with_time(NaiveTime::MIN)
            .unwrap();

        assert_eq!(tuesday_max, tuesday_as_weekday_max);
    }

    #[test]
    fn weekday_chrono_max_skipping_self() {
        let tuesday = base_time();

        let tuesday_as_weekday = Weekday::from_chrono(tuesday, false, Language::default());

        assert_eq!(tuesday_as_weekday, Weekday::tuesday());

        let tuesday_as_weekday_max = tuesday_as_weekday.to_chrono_max(tuesday, true);

        let tuesday_max = tuesday
            .checked_add_days(Days::new(8))
            .unwrap()
            .with_time(NaiveTime::MIN)
            .unwrap();

        assert_eq!(tuesday_max, tuesday_as_weekday_max);
    }

    #[test]
    fn midnight_next_monday_means_sunday() {
        let tuesday = base_time();

        dbg!(&tuesday);
        let midnight_next_monday = tuesday
            .checked_add_days(Days::new(6))
            .unwrap()
            .with_time(NaiveTime::MIN)
            .unwrap();

        dbg!(&midnight_next_monday);

        assert_eq!(
            midnight_next_monday.weekday(),
            chrono::Weekday::Mon,
            "chrono monday check"
        );

        let time = Time::from_max_chrono(midnight_next_monday, Some(tuesday), Language::default());

        println!(
            "sunday chrono max {}",
            Time::Weekday(Weekday::sunday()).to_chrono_max(tuesday)
        );

        assert_eq!(time, Time::Weekday(Weekday::sunday()));
    }

    #[test]
    fn month_conversions() {
        let tuesday = base_time(); // July 29th, 2025

        let july = Month::from_chrono(tuesday, false, Language::default());
        assert_eq!(july, Month::july());

        // Next occurrence of July should be midnight on August 1st
        let next_july = july.to_chrono_max(tuesday, false);
        assert_eq!(next_july.month(), 8);
        assert_eq!(next_july.day(), 1);
    }

    #[test]
    fn month_skipping_self() {
        let tuesday = base_time(); // July 29th, 2025

        let july = Month::july();

        // Skipping self should give us next year's July
        let next_july = july.to_chrono_max(tuesday, true);
        assert_eq!(next_july.year(), 2026);
        assert_eq!(next_july.month(), 8);
        assert_eq!(next_july.day(), 1);
    }

    #[test]
    fn relative_today_conversion() {
        let tuesday = base_time();

        let today = Relative::today();

        // Min should be start of today
        let min = today.clone().to_chrono_min(tuesday);
        assert_eq!(min.date_naive(), tuesday.date_naive());
        assert_eq!(min.time(), NaiveTime::MIN);

        // Max should be start of tomorrow
        let max = today.to_chrono_max(tuesday);
        assert_eq!(max, tuesday.checked_add_days(Days::new(1)).unwrap().with_time(NaiveTime::MIN).unwrap());
    }

    #[test]
    fn relative_tomorrow_conversion() {
        let tuesday = base_time();

        let tomorrow = Relative::tomorrow();

        // Min should be start of tomorrow
        let min = tomorrow.clone().to_chrono_min(tuesday);
        assert_eq!(min, tuesday.checked_add_days(Days::new(1)).unwrap().with_time(NaiveTime::MIN).unwrap());

        // Max should be start of day after tomorrow
        let max = tomorrow.to_chrono_max(tuesday);
        assert_eq!(max, tuesday.checked_add_days(Days::new(2)).unwrap().with_time(NaiveTime::MIN).unwrap());
    }

    #[test]
    #[cfg(feature = "swedish")]
    fn language_switching_weekday() {
        use crate::traits::WithLanguage;
        use crate::language::{Language, Swedish};

        let monday_english = Monday::default();
        assert_eq!(format!("{}", monday_english), "Monday");

        let monday_swedish = monday_english.with_language(Language::Swedish(Swedish::default()));
        assert_eq!(format!("{}", monday_swedish), "MÃ¥ndag");
    }

    #[test]
    #[cfg(feature = "swedish")]
    fn language_switching_month() {
        use crate::traits::WithLanguage;
        use crate::language::{Language, Swedish};

        let january_english = January::default();
        assert_eq!(format!("{}", january_english), "January");

        let january_swedish = january_english.with_language(Language::Swedish(Swedish::default()));
        assert_eq!(format!("{}", january_swedish), "Januari");
    }

    #[test]
    #[cfg(feature = "swedish")]
    fn language_switching_relative() {
        use crate::traits::WithLanguage;
        use crate::language::{Language, Swedish};

        let today_english = Today::default();
        assert_eq!(format!("{}", today_english), "Today");

        let today_swedish = today_english.with_language(Language::Swedish(Swedish::default()));
        assert_eq!(format!("{}", today_swedish), "Idag");
    }

    #[test]
    fn this_week_conversion() {
        let tuesday = base_time(); // July 29th, 2025

        let this_week = Relative::this_week();

        // This week should end on Sunday midnight (which is the next Monday)
        let max = this_week.to_chrono_max(tuesday);

        // Should be the upcoming Sunday at midnight (which shows as Monday)
        assert_eq!(max.weekday(), chrono::Weekday::Mon);
    }

    #[test]
    fn next_week_conversion() {
        let tuesday = base_time();

        let next_week = Relative::next_week();

        // Next week should end a week after this week ends
        let max = next_week.to_chrono_max(tuesday);

        // Should be Monday, 7 days after the end of this week
        assert_eq!(max.weekday(), chrono::Weekday::Mon);

        let this_week_max = Relative::this_week().to_chrono_max(tuesday);
        assert_eq!(max, this_week_max.checked_add_days(Days::new(7)).unwrap());
    }

    #[test]
    fn this_month_conversion() {
        let tuesday = base_time(); // July 29th, 2025

        let this_month = Relative::this_month();

        // This month should end at midnight on August 1st
        let max = this_month.to_chrono_max(tuesday);
        assert_eq!(max.month(), 8);
        assert_eq!(max.day(), 1);
        assert_eq!(max.year(), 2025);
    }

    #[test]
    fn month_year_boundary() {
        let dec_15 = DateTime::parse_from_rfc3339("2025-12-15T10:00:00-00:00")
            .unwrap()
            .to_utc();

        // December should wrap to January next year
        let december = Month::december();
        let max = december.to_chrono_max(dec_15, false);

        assert_eq!(max.year(), 2026);
        assert_eq!(max.month(), 1);
        assert_eq!(max.day(), 1);
    }

    #[test]
    fn month_january_in_december() {
        let dec_15 = DateTime::parse_from_rfc3339("2025-12-15T10:00:00-00:00")
            .unwrap()
            .to_utc();

        // January should be next year when we're in December
        let january = Month::january();
        let max = january.to_chrono_max(dec_15, false);

        assert_eq!(max.year(), 2026);
        assert_eq!(max.month(), 2);
        assert_eq!(max.day(), 1);
    }

    #[test]
    fn weekday_year_boundary() {
        // Test on December 31, 2025 (Wednesday)
        let wed_dec_31 = DateTime::parse_from_rfc3339("2025-12-31T10:00:00-00:00")
            .unwrap()
            .to_utc();

        // Monday should be next year
        let monday = Weekday::monday();
        let max = monday.to_chrono_max(wed_dec_31, false);

        assert_eq!(max.year(), 2026);
        assert_eq!(max.month(), 1);
    }

    #[test]
    fn from_max_chrono_preserves_language() {
        let tuesday = base_time();
        let midnight_tomorrow = tuesday
            .checked_add_days(Days::new(1))
            .unwrap()
            .with_time(NaiveTime::MIN)
            .unwrap();

        #[cfg(feature = "swedish")]
        {
            use crate::language::{Language, Swedish};

            let time = Time::from_max_chrono(
                midnight_tomorrow,
                Some(tuesday),
                Language::Swedish(Swedish::default()),
            );

            if let Time::Relative(Relative::Today(today)) = time {
                assert_eq!(format!("{}", today), "Idag");
            } else {
                panic!("Expected Today variant");
            }
        }

        #[cfg(not(feature = "swedish"))]
        {
            let time = Time::from_max_chrono(midnight_tomorrow, Some(tuesday), Language::default());

            if let Time::Relative(Relative::Today(today)) = time {
                assert_eq!(format!("{}", today), "Today");
            } else {
                panic!("Expected Today variant");
            }
        }
    }
}