Skip to content

Commit

Permalink
Merge branch 'format_git_date_time'
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Aug 13, 2022
2 parents ee7b5bb + b139d70 commit 99e12be
Show file tree
Hide file tree
Showing 11 changed files with 266 additions and 182 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion etc/check-package-size.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ echo "in root: gitoxide CLI"
(enter git-traverse && indent cargo diet -n --package-size-limit 10KB)
(enter git-url && indent cargo diet -n --package-size-limit 15KB)
(enter git-validate && indent cargo diet -n --package-size-limit 5KB)
(enter git-date && indent cargo diet -n --package-size-limit 5KB)
(enter git-date && indent cargo diet -n --package-size-limit 10KB)
(enter git-filter && indent cargo diet -n --package-size-limit 5KB)
(enter git-lfs && indent cargo diet -n --package-size-limit 5KB)
(enter git-note && indent cargo diet -n --package-size-limit 5KB)
Expand Down
2 changes: 1 addition & 1 deletion git-date/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ serde1 = ["serde", "bstr/serde1"]
bstr = { version = "0.2.13", default-features = false, features = ["std"]}
serde = { version = "1.0.114", optional = true, default-features = false, features = ["derive"]}
itoa = "1.0.1"
time = { version = "0.3.2", default-features = false, features = ["local-offset"] }
time = { version = "0.3.2", default-features = false, features = ["local-offset", "formatting", "macros"] }

document-features = { version = "0.2.0", optional = true }

Expand Down
174 changes: 0 additions & 174 deletions git-date/src/time.rs

This file was deleted.

27 changes: 27 additions & 0 deletions git-date/src/time/format.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::Time;
use time::format_description::FormatItem;
use time::formatting::Formattable;
use time::macros::format_description;

/// E.g. `2018-12-24`
pub const SHORT: &[FormatItem<'_>] = format_description!("[year]-[month]-[day]");

/// Formatting
impl Time {
/// Format this instance according to the given `format`.
///
/// Use the [`format_description`] macro to create and validate formats at compile time, courtesy of the [`time`] crate.
pub fn format(&self, format: &(impl Formattable + ?Sized)) -> String {
self.to_time()
.format(&format)
.expect("well-known format into memory never fails")
}
}

impl Time {
fn to_time(self) -> time::OffsetDateTime {
time::OffsetDateTime::from_unix_timestamp(self.seconds_since_unix_epoch as i64)
.expect("always valid unix time")
.replace_offset(time::UtcOffset::from_whole_seconds(self.offset_in_seconds).expect("valid offset"))
}
}
69 changes: 69 additions & 0 deletions git-date/src/time/init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use crate::time::Sign;
use crate::Time;
use std::convert::TryInto;
use std::ops::Sub;

/// Instantiation
impl Time {
/// Create a new instance from seconds and offset.
pub fn new(seconds_since_unix_epoch: u32, offset_in_seconds: i32) -> Self {
Time {
seconds_since_unix_epoch,
offset_in_seconds,
sign: offset_in_seconds.into(),
}
}

/// Return the current time without figuring out a timezone offset
pub fn now_utc() -> Self {
let seconds_since_unix_epoch = time::OffsetDateTime::now_utc()
.sub(std::time::SystemTime::UNIX_EPOCH)
.whole_seconds()
.try_into()
.expect("this is not year 2038");
Self {
seconds_since_unix_epoch,
offset_in_seconds: 0,
sign: Sign::Plus,
}
}

/// Return the current local time, or `None` if the local time wasn't available.
pub fn now_local() -> Option<Self> {
let now = time::OffsetDateTime::now_utc();
let seconds_since_unix_epoch = now
.sub(std::time::SystemTime::UNIX_EPOCH)
.whole_seconds()
.try_into()
.expect("this is not year 2038");
// TODO: make this work without cfg(unsound_local_offset), see
// https://github.com/time-rs/time/issues/293#issuecomment-909158529
let offset = time::UtcOffset::local_offset_at(now).ok()?;
Self {
seconds_since_unix_epoch,
offset_in_seconds: offset.whole_seconds(),
sign: Sign::Plus,
}
.into()
}

/// Return the current local time, or the one at UTC if the local time wasn't available.
pub fn now_local_or_utc() -> Self {
let now = time::OffsetDateTime::now_utc();
let seconds_since_unix_epoch = now
.sub(std::time::SystemTime::UNIX_EPOCH)
.whole_seconds()
.try_into()
.expect("this is not year 2038");
// TODO: make this work without cfg(unsound_local_offset), see
// https://github.com/time-rs/time/issues/293#issuecomment-909158529
let offset_in_seconds = time::UtcOffset::local_offset_at(now)
.map(|ofs| ofs.whole_seconds())
.unwrap_or(0);
Self {
seconds_since_unix_epoch,
offset_in_seconds,
sign: Sign::Plus,
}
}
}
57 changes: 57 additions & 0 deletions git-date/src/time/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use crate::Time;

/// Access
impl Time {
/// Return true if this time has been initialized to anything non-default, i.e. 0.
pub fn is_set(&self) -> bool {
*self != Self::default()
}

/// Return the passed seconds since epoch since this signature was made.
pub fn seconds(&self) -> u32 {
self.seconds_since_unix_epoch
}
}

/// Indicates if a number is positive or negative for use in [`Time`].
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
#[allow(missing_docs)]
pub enum Sign {
Plus,
Minus,
}

///
pub mod format;
mod init;
mod write;

mod sign {
use crate::time::Sign;

impl From<i32> for Sign {
fn from(v: i32) -> Self {
if v < 0 {
Sign::Minus
} else {
Sign::Plus
}
}
}
}

mod impls {
use crate::time::Sign;
use crate::Time;

impl Default for Time {
fn default() -> Self {
Time {
seconds_since_unix_epoch: 0,
offset_in_seconds: 0,
sign: Sign::Plus,
}
}
}
}

0 comments on commit 99e12be

Please sign in to comment.