1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use std::cmp::Ordering;

use libc::{c_char, c_int};

use crate::raw;
use crate::util::Binding;

/// Time in a signature
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Time {
    raw: raw::git_time,
}

/// Time structure used in a git index entry.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct IndexTime {
    raw: raw::git_index_time,
}

impl Time {
    /// Creates a new time structure from its components.
    pub fn new(time: i64, offset: i32) -> Time {
        unsafe {
            Binding::from_raw(raw::git_time {
                time: time as raw::git_time_t,
                offset: offset as c_int,
                sign: if offset < 0 { '-' } else { '+' } as c_char,
            })
        }
    }

    /// Return the time, in seconds, from epoch
    pub fn seconds(&self) -> i64 {
        self.raw.time as i64
    }

    /// Return the timezone offset, in minutes
    pub fn offset_minutes(&self) -> i32 {
        self.raw.offset as i32
    }

    /// Return whether the offset was positive or negative. Primarily useful
    /// in case the offset is specified as a negative zero.
    pub fn sign(&self) -> char {
        self.raw.sign as u8 as char
    }
}

impl PartialOrd for Time {
    fn partial_cmp(&self, other: &Time) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Time {
    fn cmp(&self, other: &Time) -> Ordering {
        (self.raw.time, self.raw.offset).cmp(&(other.raw.time, other.raw.offset))
    }
}

impl Binding for Time {
    type Raw = raw::git_time;
    unsafe fn from_raw(raw: raw::git_time) -> Time {
        Time { raw }
    }
    fn raw(&self) -> raw::git_time {
        self.raw
    }
}

impl IndexTime {
    /// Creates a new time structure from its components.
    pub fn new(seconds: i32, nanoseconds: u32) -> IndexTime {
        unsafe {
            Binding::from_raw(raw::git_index_time {
                seconds,
                nanoseconds,
            })
        }
    }

    /// Returns the number of seconds in the second component of this time.
    pub fn seconds(&self) -> i32 {
        self.raw.seconds
    }
    /// Returns the nanosecond component of this time.
    pub fn nanoseconds(&self) -> u32 {
        self.raw.nanoseconds
    }
}

impl Binding for IndexTime {
    type Raw = raw::git_index_time;
    unsafe fn from_raw(raw: raw::git_index_time) -> IndexTime {
        IndexTime { raw }
    }
    fn raw(&self) -> raw::git_index_time {
        self.raw
    }
}

impl PartialOrd for IndexTime {
    fn partial_cmp(&self, other: &IndexTime) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for IndexTime {
    fn cmp(&self, other: &IndexTime) -> Ordering {
        let me = (self.raw.seconds, self.raw.nanoseconds);
        let other = (other.raw.seconds, other.raw.nanoseconds);
        me.cmp(&other)
    }
}

#[cfg(test)]
mod tests {
    use crate::Time;

    #[test]
    fn smoke() {
        assert_eq!(Time::new(1608839587, -300).seconds(), 1608839587);
        assert_eq!(Time::new(1608839587, -300).offset_minutes(), -300);
        assert_eq!(Time::new(1608839587, -300).sign(), '-');
        assert_eq!(Time::new(1608839587, 300).sign(), '+');
    }
}