Skip to main content

Git2TimeChronoExt

Trait Git2TimeChronoExt 

Source
pub trait Git2TimeChronoExt {
    // Required methods
    fn to_date_time(&self) -> Result<DateTime<FixedOffset>, Error>;
    fn to_date_time_in<Tz: TimeZone>(
        &self,
        tz: &Tz,
    ) -> Result<DateTime<Tz>, Error>;
    fn to_local_date_time(&self) -> Result<DateTime<Local>, Error>;
    fn to_date_time_opt(
        &self,
    ) -> Result<MappedLocalTime<DateTime<FixedOffset>>, Error>;
}
Expand description

An extension trait to convert git2::Time to chrono::DateTime.

§Examples

use git2_time_chrono_ext::{Error, Git2TimeChronoExt};

// Print `git2::Time` to `stdout`.
fn print_git2_time(time: &git2::Time) {
  println!("{}", time.to_local_date_time().unwrap());
}

// Convert `git2::Time` to `Stirng` in the specified format.
fn git2_time_to_string(time: &git2::Time) -> String {
  time.to_local_date_time().unwrap().format("%Y-%m-%d %H:%M").to_string()
}

// Convert `git2::Time` to ISO 8601 (RFC 3339) string.
fn git2_time_to_rfc3339(time: &git2::Time) -> Result<String, Error> {
  Ok(time.to_date_time_in(&chrono::Utc)?.to_rfc3339())
}

Required Methods§

Source

fn to_date_time(&self) -> Result<DateTime<FixedOffset>, Error>

Convert git2::Time to chrono::DateTime retaining the original timezone.

§Examples
use git2_time_chrono_ext::Git2TimeChronoExt;

// The Eastern Hemisphere time zone.
let east_time = git2::Time::new(1745693791, 540);
let east_datetime = east_time.to_date_time();
assert!(east_datetime.is_ok());
assert_eq!(east_datetime.unwrap().to_string(), "2025-04-27 03:56:31 +09:00");
// The Western Hemisphere time zone.
let west_time = git2::Time::new(1745196130, -420);
let west_datetime = west_time.to_date_time();
assert!(west_datetime.is_ok());
assert_eq!(west_datetime.unwrap().to_string(), "2025-04-20 17:42:10 -07:00");
Source

fn to_date_time_in<Tz: TimeZone>(&self, tz: &Tz) -> Result<DateTime<Tz>, Error>

Convert git2::Time to chrono::DateTime in the specified chrono::TimeZone.

§Examples
use git2_time_chrono_ext::Git2TimeChronoExt;

let time = git2::Time::new(1745196130, -420);
let utc_datetime = time.to_date_time_in(&chrono::Utc);
assert_eq!(utc_datetime.unwrap().to_string(), "2025-04-21 00:42:10 UTC");
Source

fn to_local_date_time(&self) -> Result<DateTime<Local>, Error>

Convert git2::Time to chrono::DateTime in the local time zone. This function is a shorthand of:

time.to_date_time_in(&chrono::Local)
Source

fn to_date_time_opt( &self, ) -> Result<MappedLocalTime<DateTime<FixedOffset>>, Error>

to_date_time returns the latest time when the given time is ambiguous.

This function is useful when you want to handle ambiguous time. Please see chrono::MappedLocalTime for more details.

§Examples
use git2_time_chrono_ext::Git2TimeChronoExt;
use chrono::MappedLocalTime;

let time = git2::Time::new(1745196130, -420);
let mapped = time.to_date_time_opt().unwrap();
if let MappedLocalTime::Single(datetime) = mapped {
    assert_eq!(datetime.to_string(), "2025-04-20 17:42:10 -07:00");
} else {
    panic!("should be Single");
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Git2TimeChronoExt for Time

Implementors§