date_time 2.0.0

Date_Time is a high-level rust library for use in situations where precision beyond seconds is not necessary.
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
use date_utils;
use regex::Regex;
use std::cmp::Ordering;
use std::convert::From;
use std::fmt;
use std::ops::{Add, AddAssign, Sub, SubAssign};
use std::str::FromStr;

pub type Time = TimeTuple;
pub type TimeOfDay = TimeTuple;

/// A wrapper for a particular time of day.
///
/// Precise to second-level.
///
/// **NOTE:** This cannot be 24 hours or greater - see `TimeTuple::new()` for more details.
///
/// The wrapping described in `TimeTuple::new()` also applies when adding and subtracting times.
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
pub struct TimeTuple {
    h: u8,
    m: u8,
    s: u8,
}

impl TimeTuple {
    /// Produces a new TimeTuple.
    ///
    /// Times of 24 hours or greater and negative times
    /// will wrap around 24 hours to always produce a positive time.
    ///
    /// The value is calculated from total number of seconds so a time
    /// with a minute value of 90 would add an hour to the resulting tuple
    /// and set the minutes to 30, for example.
    pub fn new(h: i32, m: i32, s: i32) -> TimeTuple {
        let mut total_seconds = s + 60 * m + 3600 * h;
        while total_seconds < 0 {
            total_seconds += 86400;
        }
        TimeTuple::from_seconds(total_seconds as u64)
    }

    /// Same as `TimeTuple::new()` but takes the total number of seconds
    /// as its argument and calculates the hours, minutes, and seconds
    /// from that.
    pub fn from_seconds(mut total_seconds: u64) -> TimeTuple {
        while total_seconds >= 86400 {
            total_seconds -= 86400;
        }
        let h = total_seconds / 3600;
        total_seconds -= h * 3600;
        let m = total_seconds / 60;
        total_seconds -= m * 60;
        TimeTuple {
            h: h as u8,
            m: m as u8,
            s: total_seconds as u8,
        }
    }

    /// Returns a `TimeTuple` of the current time as `std::time::SystemTime` provides it.
    pub fn now() -> TimeTuple {
        date_utils::now_as_timetuple()
    }

    pub fn get_hours(&self) -> u8 {
        self.h
    }

    pub fn get_minutes(&self) -> u8 {
        self.m
    }

    pub fn get_seconds(&self) -> u8 {
        self.s
    }

    /// Produces a string such as 08:30 for 8 hours and 30 minutes.
    ///
    /// Ignores seconds.
    pub fn to_hhmm_string(&self) -> String {
        format!("{:02}:{:02}", self.h, self.m)
    }

    /// Gets the total number of seconds in the tuple.
    pub fn to_seconds(&self) -> u32 {
        3600 * self.h as u32 + 60 * self.m as u32 + self.s as u32
    }

    /// Gets the total number of minutes in the tuple, ignoring seconds.
    pub fn to_minutes(&self) -> u32 {
        60 * self.h as u32 + self.m as u32
    }

    /// Adds a number of seconds to the TimeTuple,
    /// wrapping the same way `TimeTuple::new()` does.
    pub fn add_seconds(&mut self, seconds: i32) {
        let new_seconds = self.s as i32 + seconds;
        *self = TimeTuple::new(self.h as i32, self.m as i32, new_seconds);
    }

    /// Subtracts a number of seconds from the TimeTuple,
    /// wrapping the same way `TimeTuple::new()` does.
    pub fn subtract_seconds(&mut self, seconds: i32) {
        let new_seconds = self.s as i32 - seconds;
        *self = TimeTuple::new(self.h as i32, self.m as i32, new_seconds);
    }

    /// Adds a number of minutes to the TimeTuple,
    /// wrapping the same way `TimeTuple::new()` does.
    pub fn add_minutes(&mut self, minutes: i32) {
        let new_minutes = self.m as i32 + minutes;
        *self = TimeTuple::new(self.h as i32, new_minutes, self.s as i32);
    }

    /// Subtracts a number of minutes from the TimeTuple,
    /// wrapping the same way `TimeTuple::new()` does.
    pub fn subtract_minutes(&mut self, minutes: i32) {
        let new_minutes = self.m as i32 - minutes;
        *self = TimeTuple::new(self.h as i32, new_minutes, self.s as i32);
    }

    /// Adds a number of hours to the TimeTuple,
    /// wrapping the same way `TimeTuple::new()` does.
    pub fn add_hours(&mut self, hours: i32) {
        let new_hours = self.h as i32 + hours;
        *self = TimeTuple::new(new_hours, self.m as i32, self.s as i32);
    }

    /// Subtracts a number of hours from the TimeTuple,
    /// wrapping the same way `TimeTuple::new()` does.
    pub fn subtract_hours(&mut self, hours: i32) {
        let new_hours = self.h as i32 - hours;
        *self = TimeTuple::new(new_hours, self.m as i32, self.s as i32);
    }
}

impl fmt::Display for TimeTuple {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:02}:{:02}:{:02}", self.h, self.m, self.s)
    }
}

impl FromStr for TimeTuple {
    type Err = String;

    fn from_str(s: &str) -> Result<TimeTuple, Self::Err> {
        let valid_format = Regex::new(r"^\d{2}:\d{2}:\d{2}$").unwrap();
        if !valid_format.is_match(s) {
            Err(format!(
                "Invalid str formatting of TimeTuple: {}\nExpects a string formatted like 08:30:05",
                s
            ))
        } else {
            let mut parts = s.split(':');
            Ok(TimeTuple::new(
                i32::from_str(parts.next().unwrap()).unwrap(),
                i32::from_str(parts.next().unwrap()).unwrap(),
                i32::from_str(parts.next().unwrap()).unwrap(),
            ))
        }
    }
}

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

impl Ord for TimeTuple {
    fn cmp(&self, other: &TimeTuple) -> Ordering {
        self.to_seconds().cmp(&other.to_seconds())
    }
}

impl Add for TimeTuple {
    type Output = TimeTuple;
    fn add(self, other: TimeTuple) -> TimeTuple {
        TimeTuple::new(
            (self.h + other.h) as i32,
            (self.m + other.m) as i32,
            (self.s + other.s) as i32,
        )
    }
}

impl AddAssign for TimeTuple {
    fn add_assign(&mut self, other: TimeTuple) {
        *self = TimeTuple::new(
            (self.h + other.h) as i32,
            (self.m + other.m) as i32,
            (self.s + other.s) as i32,
        );
    }
}

impl Sub for TimeTuple {
    type Output = TimeTuple;
    fn sub(self, other: TimeTuple) -> TimeTuple {
        TimeTuple::new(
            (self.h - other.h) as i32,
            (self.m - other.m) as i32,
            (self.s - other.s) as i32,
        )
    }
}

impl SubAssign for TimeTuple {
    fn sub_assign(&mut self, other: TimeTuple) {
        *self = TimeTuple::new(
            (self.h - other.h) as i32,
            (self.m - other.m) as i32,
            (self.s - other.s) as i32,
        );
    }
}

/// A wrapper for a duration.
///
/// Does not count in days, but can have hours >24 (up to `u32::MAX`)
///
/// Precise to second-level.
///
/// Cannot be negative.
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
pub struct Duration {
    h: u32,
    m: u8,
    s: u8,
}

impl Duration {
    /// Produces a new Duration.
    ///
    /// The value is calculated from total number of seconds so a duration
    /// with a minute value of 90 would add an hour to the resulting tuple
    /// and set the minutes to 30, for example.
    pub fn new(h: u32, m: u32, s: u32) -> Duration {
        let total_seconds = s + 60 * m + 3600 * h;
        Duration::from_seconds(total_seconds as u64)
    }

    /// Same as `Duration::new()` but takes the total number of seconds
    /// as its argument and calculates the hours, minutes, and seconds
    /// from that.
    pub fn from_seconds(mut total_seconds: u64) -> Duration {
        let h = total_seconds / 3600;
        total_seconds -= h * 3600;
        let m = total_seconds / 60;
        total_seconds -= m * 60;
        Duration {
            h: h as u32,
            m: m as u8,
            s: total_seconds as u8,
        }
    }

    pub fn get_hours(&self) -> u32 {
        self.h
    }

    pub fn get_minutes(&self) -> u8 {
        self.m
    }

    pub fn get_seconds(&self) -> u8 {
        self.s
    }

    /// Produces a string such as 8:30 for 8 hours and 30 minutes.
    ///
    /// Hours field will expand as necessary; 150:30 is a possible result.
    ///
    /// Ignores seconds.
    pub fn to_hours_and_minutes_string(&self) -> String {
        format!("{}:{:02}", self.h, self.m)
    }

    /// Gets the total number of seconds in the Duration.
    pub fn to_seconds(&self) -> u64 {
        3600 * self.h as u64 + 60 * self.m as u64 + self.s as u64
    }

    /// Gets the total number of minutes in the Duration, ignoring seconds.
    pub fn to_minutes(&self) -> u32 {
        60 * self.h as u32 + self.m as u32
    }

    /// Adds a number of seconds to the Duration,
    /// wrapping the same way `Duration::new()` does.
    pub fn add_seconds(&mut self, seconds: u32) {
        let new_seconds = self.s as u32 + seconds;
        *self = Duration::new(self.h as u32, self.m as u32, new_seconds);
    }

    /// Subtracts a number of seconds from the Duration,
    /// wrapping the same way `Duration::new()` does.
    pub fn subtract_seconds(&mut self, seconds: u32) {
        let new_seconds = self.s as u32 - seconds;
        *self = Duration::new(self.h as u32, self.m as u32, new_seconds);
    }

    /// Adds a number of minutes to the Duration,
    /// wrapping the same way `Duration::new()` does.
    pub fn add_minutes(&mut self, minutes: u32) {
        let new_minutes = self.m as u32 + minutes;
        *self = Duration::new(self.h as u32, new_minutes, self.s as u32);
    }

    /// Subtracts a number of minutes from the Duration,
    /// wrapping the same way `Duration::new()` does.
    pub fn subtract_minutes(&mut self, minutes: u32) {
        let new_minutes = self.m as u32 - minutes;
        *self = Duration::new(self.h as u32, new_minutes, self.s as u32);
    }

    /// Adds a number of hours to the Duration,
    /// wrapping the same way `Duration::new()` does.
    pub fn add_hours(&mut self, hours: u32) {
        let new_hours = self.h as u32 + hours;
        *self = Duration::new(new_hours, self.m as u32, self.s as u32);
    }

    /// Subtracts a number of hours from the Duration,
    /// wrapping the same way `Duration::new()` does.
    pub fn subtract_hours(&mut self, hours: u32) {
        let new_hours = self.h as u32 - hours;
        *self = Duration::new(new_hours, self.m as u32, self.s as u32);
    }
}

impl fmt::Display for Duration {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}:{:02}:{:02}", self.h, self.m, self.s)
    }
}

impl FromStr for Duration {
    type Err = String;

    fn from_str(s: &str) -> Result<Duration, Self::Err> {
        let valid_format = Regex::new(r"^\d+:\d{2}:\d{2}$").unwrap();
        if !valid_format.is_match(s) {
            Err(format!(
                "Invalid str formatting of Duration: {}\nExpects a string formatted like 8:30:05",
                s
            ))
        } else {
            let mut parts = s.split(':');
            Ok(Duration::new(
                u32::from_str(parts.next().unwrap()).unwrap(),
                u32::from_str(parts.next().unwrap()).unwrap(),
                u32::from_str(parts.next().unwrap()).unwrap(),
            ))
        }
    }
}

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

impl Ord for Duration {
    fn cmp(&self, other: &Duration) -> Ordering {
        self.to_seconds().cmp(&other.to_seconds())
    }
}

impl Add for Duration {
    type Output = Duration;
    fn add(self, other: Duration) -> Duration {
        Duration::new(
            (self.h + other.h) as u32,
            (self.m + other.m) as u32,
            (self.s + other.s) as u32,
        )
    }
}

impl AddAssign for Duration {
    fn add_assign(&mut self, other: Duration) {
        *self = Duration::new(
            (self.h + other.h) as u32,
            (self.m + other.m) as u32,
            (self.s + other.s) as u32,
        );
    }
}

impl Sub for Duration {
    type Output = Duration;
    fn sub(self, other: Duration) -> Duration {
        Duration::new(
            (self.h - other.h) as u32,
            (self.m - other.m) as u32,
            (self.s - other.s) as u32,
        )
    }
}

impl SubAssign for Duration {
    fn sub_assign(&mut self, other: Duration) {
        *self = Duration::new(
            (self.h - other.h) as u32,
            (self.m - other.m) as u32,
            (self.s - other.s) as u32,
        );
    }
}

impl From<TimeTuple> for Duration {
    fn from(time: TimeTuple) -> Self {
        Duration::from_seconds(time.to_seconds() as u64)
    }
}

#[cfg(test)]
mod tests {

    #[test]
    fn test_no_seconds() {
        let tuple = super::TimeTuple::new(5, 30, 0);
        assert_eq!(5, tuple.h);
        assert_eq!(30, tuple.m);
        assert_eq!(0, tuple.s);
    }

    #[test]
    fn test_no_overlap() {
        let tuple = super::TimeTuple::new(5, 30, 30);
        assert_eq!(5, tuple.h);
        assert_eq!(30, tuple.m);
        assert_eq!(30, tuple.s);
    }

    #[test]
    fn test_second_overlap() {
        let tuple = super::TimeTuple::new(6, 30, 90);
        assert_eq!(6, tuple.h);
        assert_eq!(31, tuple.m);
        assert_eq!(30, tuple.s);
    }

    #[test]
    fn test_minute_overlap() {
        let tuple = super::TimeTuple::new(6, 90, 30);
        assert_eq!(7, tuple.h);
        assert_eq!(30, tuple.m);
        assert_eq!(30, tuple.s);
    }

    #[test]
    fn test_hour_overlap() {
        let tuple = super::TimeTuple::new(25, 30, 30);
        assert_eq!(1, tuple.h);
        assert_eq!(30, tuple.m);
        assert_eq!(30, tuple.s);
    }

    #[test]
    fn test_all_overlap() {
        let tuple = super::TimeTuple::new(25, 90, 90);
        assert_eq!(2, tuple.h);
        assert_eq!(31, tuple.m);
        assert_eq!(30, tuple.s);
    }

    #[test]
    fn test_minutes_to_hours_overlap() {
        let tuple = super::TimeTuple::new(18, 420, 0);
        assert_eq!(1, tuple.h);
        assert_eq!(0, tuple.m);
        assert_eq!(0, tuple.s);
    }

    #[test]
    fn test_negative_seconds() {
        let tuple = super::TimeTuple::new(6, 30, -60);
        assert_eq!(6, tuple.h);
        assert_eq!(29, tuple.m);
        assert_eq!(0, tuple.s);
    }

    #[test]
    fn test_all_negative_overlaps() {
        let tuple = super::TimeTuple::new(-3, -116, -301);
        assert_eq!(18, tuple.h);
        assert_eq!(58, tuple.m);
        assert_eq!(59, tuple.s);
    }

    #[test]
    fn test_to_string() {
        let tuple = super::TimeTuple::new(3, 0, 39);
        assert_eq!(String::from("03:00:39"), tuple.to_string())
    }

    #[test]
    fn test_to_hhmm_string() {
        let tuple = super::TimeTuple::new(3, 0, 39);
        assert_eq!(String::from("03:00"), tuple.to_hhmm_string())
    }

    #[test]
    fn test_operators() {
        let zeroes = super::TimeTuple::new(0, 0, 0);
        let ones = super::TimeTuple::new(1, 1, 1);
        let twos = super::TimeTuple::new(2, 2, 2);
        assert_eq!(twos, ones + ones);
        assert_eq!(zeroes, ones - ones);
        assert!(zeroes < ones);
        assert!(zeroes < twos);
        assert!(twos > ones);
        assert!(zeroes <= ones);
        assert!(ones <= ones);
    }

    #[test]
    fn test_to_seconds() {
        let tuple = super::TimeTuple::new(2, 30, 30);
        assert_eq!(9030, tuple.to_seconds());
    }

    #[test]
    fn test_to_minutes() {
        let tuple = super::TimeTuple::new(2, 30, 30);
        let duration = super::Duration::new(26, 30, 30);
        assert_eq!(150, tuple.to_minutes());
        assert_eq!(1590, duration.to_minutes());
    }

    #[test]
    fn test_from_seconds() {
        let tuple = super::TimeTuple::from_seconds(86400);
        assert_eq!(super::TimeTuple::new(0, 0, 0), tuple);
    }

    #[test]
    fn test_from_string() {
        let tuple = super::TimeTuple::new(5, 30, 4);
        assert_eq!(tuple, str::parse("05:30:04").unwrap());
        assert!(str::parse::<super::TimeTuple>("05:a:04").is_err());
    }

    #[test]
    fn test_manipulate_seconds() {
        let mut tuple = super::TimeTuple::new(10, 58, 59);
        tuple.add_seconds(3);
        assert_eq!(super::TimeTuple::new(10, 59, 2), tuple);
        tuple.subtract_seconds(1);
        tuple.subtract_seconds(2);
        assert_eq!(super::TimeTuple::new(10, 58, 59), tuple);
    }

    #[test]
    fn test_manipulate_minutes() {
        let mut tuple = super::TimeTuple::new(10, 58, 59);
        tuple.add_minutes(3);
        assert_eq!(super::TimeTuple::new(11, 1, 59), tuple);
        tuple.subtract_minutes(1);
        tuple.subtract_minutes(2);
        assert_eq!(super::TimeTuple::new(10, 58, 59), tuple);
    }

    #[test]
    fn test_manipulate_hours() {
        let mut tuple = super::TimeTuple::new(10, 58, 59);
        tuple.add_hours(3);
        assert_eq!(super::TimeTuple::new(13, 58, 59), tuple);
        tuple.subtract_hours(1);
        tuple.subtract_hours(2);
        assert_eq!(super::TimeTuple::new(10, 58, 59), tuple);
    }

    #[test]
    fn test_time_to_duration() {
        let time = super::Time::new(20, 20, 20);
        let duration = super::Duration::from(time);
        assert_eq!(super::Duration::new(20, 20, 20), duration);
    }

    #[test]
    fn test_large_duration() {
        let duration = super::Duration::new(200, 0, 0);
        assert_eq!(String::from("200:00:00"), duration.to_string());
    }

}