PHASEXave/types/data/time.rs
1/*
2 * Copyright 2024 Stanislav Mikhailov (xavetar)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 * THE SOFTWARE.
27 */
28
29use std::{
30 time::{
31 Duration,
32 SystemTime,
33 UNIX_EPOCH
34 }
35};
36
37use super::{
38 zone::{
39 Sign, Zone
40 }
41};
42
43use crate::types::{
44 planets::{
45 earth::{
46 calendar::{
47 constants::{
48 seconds::{
49 SECONDS_IN_DAY,
50 SECONDS_IN_HOUR,
51 SECONDS_IN_MINUTE
52 }
53 },
54 }
55 }
56 }
57};
58
59#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
60pub struct Time {
61 pub hours: u8,
62 pub minutes: u8,
63 pub seconds: u8,
64 pub time_zone: Zone,
65 pub unix_time: u128
66}
67
68impl Time {
69 pub(crate) fn unix() -> Duration {
70 return SystemTime::now()
71 .duration_since(UNIX_EPOCH)
72 .expect("Error calling SystemTime::now().duration_since(UNIX_EPOCH)");
73 }
74
75 pub(crate) fn of(mut unix_time: u128, time_zone: Zone, zone_in_unix: bool) -> Time {
76 if !zone_in_unix {
77 let time_zone_seconds: u128 = time_zone.to_seconds() as u128;
78
79 if unix_time < time_zone_seconds && time_zone.sign == Sign::Signed {
80 panic!("[OVERFLOW]: Overflow type, unix time - time zone < zero!")
81 } else if unix_time > u128::MAX - time_zone_seconds && time_zone.sign == Sign::Unsigned {
82 panic!("[OVERFLOW]: Overflow type, unix time + time zone > type!")
83 }
84
85 if time_zone.sign == Sign::Signed {
86 unix_time -= time_zone_seconds;
87 } else if time_zone.sign == Sign::Unsigned {
88 unix_time += time_zone_seconds;
89 }
90 }
91
92 return Time::from_seconds(unix_time, time_zone);
93 }
94
95 pub(crate) const fn from_seconds(unix: u128, zone: Zone) -> Time {
96 return Time {
97 hours: ((unix % SECONDS_IN_DAY) / SECONDS_IN_HOUR) as u8,
98 minutes: ((unix % SECONDS_IN_HOUR) / SECONDS_IN_MINUTE) as u8,
99 seconds: (unix % SECONDS_IN_MINUTE) as u8,
100 time_zone: zone,
101 unix_time: unix
102 }
103 }
104}