Trait Git2TimeToChronoExt

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

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

§Examples

use git_iblame::extensions::Git2TimeToChronoExt;

// 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()
}

Required Methods§

Source

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

Convert git2::Time to chrono::DateTime<chrono::FixedOffset>. The time zone offset is the value in the git2::Time.

§Examples
use git_iblame::extensions::Git2TimeToChronoExt;

// 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>>

Convert git2::Time to chrono::DateTime in the specified time zone.

§Examples
use git_iblame::extensions::Git2TimeToChronoExt;
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>>

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

let local_datetime = time.to_date_time_in(&chrono::Local);

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 Git2TimeToChronoExt for Time

Implementors§