Radish alpha
r
rad:z6cFWeWpnZNHh9rUW8phgA3b5yGt
Git libraries for Radicle
Radicle
Git
git-ext: Display/parse timezone offset in the format git2 expects.
xphoniex committed 2 years ago
commit 98a6e693799a58ebe6e36feeb780c407a9ebe478
parent 0897f91
2 files changed +36 -3
modified radicle-git-ext/src/author.rs
@@ -60,7 +60,9 @@ impl From<git2::Time> for Time {
impl fmt::Display for Time {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let sign = if self.offset.is_negative() { '-' } else { '+' };
-
        write!(f, "{} {}{:0>4}", self.seconds, sign, self.offset.abs())
+
        let hours = self.offset.abs() / 60;
+
        let minutes = self.offset.abs() % 60;
+
        write!(f, "{} {}{:0>2}{:0>2}", self.seconds, sign, hours, minutes)
    }
}

@@ -110,7 +112,17 @@ impl FromStr for Author {
        let mut components = s.split(' ');
        let offset = match components.next_back() {
            None => return Err(ParseError::Missing("offset")),
-
            Some(offset) => offset.parse::<i32>().map_err(ParseError::Offset)?,
+
            Some(offset) => {
+
                // The offset is in the form of timezone offset,
+
                // e.g. +0200, -0100.  This needs to be converted into
+
                // minutes. The first two digits in the offset are the
+
                // number of hours in the offset, while the latter two
+
                // digits are the number of minutes in the offset.
+
                let tz_offset = offset.parse::<i32>().map_err(ParseError::Offset)?;
+
                let hours = tz_offset / 100;
+
                let minutes = tz_offset % 100;
+
                hours * 60 + minutes
+
            },
        };
        let time = match components.next_back() {
            None => return Err(ParseError::Missing("time")),
modified radicle-git-ext/t/src/commit.rs
@@ -1,4 +1,4 @@
-
use std::{io, str::FromStr as _};
+
use std::{io, str::FromStr as _, string::ToString as _};

use radicle_git_ext::{
    author::{self, Author},
@@ -242,3 +242,24 @@ fn write_valid_commit() {
    .write(&repo);
    assert!(valid.is_ok())
}
+

+
#[test]
+
fn author_roundtrip() {
+
    let author = "author Fintan Halpenny <fintan.halpenny@gmail.com> 1669292989 +0000";
+
    assert_eq!(
+
        author.parse::<Author>().unwrap().to_string(),
+
        author.to_string()
+
    );
+

+
    let author = "author Alexis Sellier <alexis@radicle.xyz> 1664467633 +0200";
+
    assert_eq!(
+
        author.parse::<Author>().unwrap().to_string(),
+
        author.to_string()
+
    );
+

+
    let author = "author Alexis Sellier <alexis@radicle.xyz> 1664467633 -0200";
+
    assert_eq!(
+
        author.parse::<Author>().unwrap().to_string(),
+
        author.to_string()
+
    );
+
}