summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/date.rs2
-rw-r--r--src/lib.rs2
-rw-r--r--src/time.rs127
3 files changed, 131 insertions, 0 deletions
diff --git a/src/date.rs b/src/date.rs
index 6cd5f48..2751e53 100644
--- a/src/date.rs
+++ b/src/date.rs
@@ -49,3 +49,5 @@ impl Date {
self.day
}
}
+
+// TODO addition
diff --git a/src/lib.rs b/src/lib.rs
index 4fbfaf9..7fee16c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -2,10 +2,12 @@
mod date;
mod month;
+mod time;
mod weekday;
mod year;
pub use date::Date;
pub use month::Month;
+pub use time::Time;
pub use weekday::Weekday;
pub use year::Year;
diff --git a/src/time.rs b/src/time.rs
new file mode 100644
index 0000000..6981c94
--- /dev/null
+++ b/src/time.rs
@@ -0,0 +1,127 @@
+use std::ops::Add;
+
+#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
+pub struct Time {
+ hour: u8,
+ minute: u8,
+ second: u8,
+ nanosecond: u32,
+}
+
+impl Time {
+ /// A `Time` that is exactly midnight
+ pub const MIDNIGHT: Self = unsafe { Self::from_hms_unchecked(0, 0, 0) };
+
+ // TODO validated versions of the following:
+ // TODO examples
+
+ /// Create a `Time` from an hour, minute, and second
+ ///
+ /// # Safety
+ ///
+ /// Creating a time where the hour is greater than 23, minute is greater than 59, or second is
+ /// greater than 60 results in undefined behavior
+ pub const unsafe fn from_hms_unchecked(hour: u8, minute: u8, second: u8) -> Self {
+ Self {
+ hour,
+ minute,
+ second,
+ nanosecond: 0,
+ }
+ }
+
+ /// Create a `Time` from an hour, minute, second, and millisecond
+ ///
+ /// # Safety
+ ///
+ /// Creating a time where the hour is greater than 23, minute is greater than 59, second is
+ /// greater than 60, or millisecond is greater than 999 results in undefined behavior
+ pub const unsafe fn from_hms_milli_unchecked(
+ hour: u8,
+ minute: u8,
+ second: u8,
+ millisecond: u16,
+ ) -> Self {
+ Self {
+ hour,
+ minute,
+ second,
+ nanosecond: millisecond as u32 * 1_000_000,
+ }
+ }
+
+ /// Create a `Time` from an hour, minute, second, and microsecond
+ ///
+ /// # Safety
+ ///
+ /// Creating a time where the hour is greater than 23, minute is greater than 59, second is
+ /// greater than 60, or microsecond is greater than 999,999 results in undefined behavior
+ pub const unsafe fn from_hms_micro_unchecked(
+ hour: u8,
+ minute: u8,
+ second: u8,
+ microsecond: u32,
+ ) -> Self {
+ Self {
+ hour,
+ minute,
+ second,
+ nanosecond: microsecond * 1_000,
+ }
+ }
+
+ /// Create a `Time` from an hour, minute, second, and nanosecond
+ ///
+ /// # Safety
+ ///
+ /// Creating a time where the hour is greater than 23, minute is greater than 59, second is
+ /// greater than 60, or nanosecond is greater than 999,999,999 results in undefined behavior
+ pub const unsafe fn from_hms_nano_unchecked(
+ hour: u8,
+ minute: u8,
+ second: u8,
+ nanosecond: u32,
+ ) -> Self {
+ Self {
+ hour,
+ minute,
+ second,
+ nanosecond,
+ }
+ }
+
+ /// Get the clock hour. The returned value will always be in the range `0..24`
+ pub const fn hour(self) -> u8 {
+ self.hour
+ }
+
+ /// Get the minute within the hour. The returned value will always be in the range `0..60`
+ pub const fn minute(self) -> u8 {
+ self.minute
+ }
+
+ // Get the second within the minute. The returned value will always be in the range `0..=60`
+ pub const fn second(self) -> u8 {
+ self.second
+ }
+
+ // Get the millisecond within the second.
+ // The returned value will always be in the range `0..1_000`
+ pub const fn millisecond(self) -> u8 {
+ (self.nanosecond / 1_000_000) as u8
+ }
+
+ // Get the microsecond within the second.
+ // The returned value will always be in the range `0..1_000_000`
+ pub const fn microsecond(self) -> u8 {
+ (self.nanosecond / 1_000) as u8
+ }
+
+ // Get the nanosecond within the second.
+ // The returned value will always be in the range `0..1_000_000`
+ pub const fn nanosecond(self) -> u32 {
+ self.nanosecond
+ }
+}
+
+// TODO addition