From 559cfa3cb3c0ff2de55a894bbf103f5629306435 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Fri, 9 Feb 2024 00:24:04 -0500 Subject: [PATCH] Add some docs --- README.md | 19 +++++++++++++++++++ src/datetime.rs | 8 ++++++++ 2 files changed, 27 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..9c6b2b5 --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +Contents +======== +- [Summary](#summary) +- [Usage](#usage) + +## Summary +Epoch is a small library for converting Unix timestamps to human readable datetime +and vice versa. It includes functionality for displaying a DateTime as a human +readable string, parsing from string, and comparison for equality or ordering. + +This crate is ideal when you want a lighter and simpler date/time library than the +excellent and very complete [chrono](https://crates.io/crates/chrono), which is +likely overkill for many use cases. + +## Usage +Add epoch to your Cargo.toml +```Toml +epoch = { git = "http://git.hitchhiker-linux.org/jeang3nie/epoch-rs.git" } +``` diff --git a/src/datetime.rs b/src/datetime.rs index 3ac71d6..2324396 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -62,6 +62,7 @@ impl DateTime { seconds } + #[allow(clippy::missing_panics_doc)] /// Converts a Unix timestamp to a `DateTime` struct pub fn from_timestamp(ts: i64) -> Self { if ts < 0 { @@ -149,14 +150,21 @@ impl DateTime { } } + #[allow(clippy::missing_panics_doc)] + /// Gets the day of the week for this `DateTime` pub fn weekday(&self) -> Weekday { let ts = self.timestamp_naive(); let mut days = ts / SECONDS_PER_DAY; + // For negative timestamps this number is actually the following day if ts < 0 { days -= 1 } + // Rusts `%` operator is modulo, not modulus. We have to use the + // operation which will gove the correct answer for either positive + // or negative remainders let rem = days.rem_euclid(7); rem.try_into().unwrap() } + /// Returns a string representing this date and time in human readable format pub fn display(&self) -> String { format!( "{} {} {} {:0>2}:{:0>2}:{:0>2} {} {}",