]>
git.proxmox.com Git - rustc.git/blob - extra/git2/src/time.rs
1 use std
::cmp
::Ordering
;
3 use libc
::{c_char, c_int}
;
6 use crate::util
::Binding
;
8 /// Time in a signature
9 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
14 /// Time structure used in a git index entry.
15 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
16 pub struct IndexTime
{
17 raw
: raw
::git_index_time
,
21 /// Creates a new time structure from its components.
22 pub fn new(time
: i64, offset
: i32) -> Time
{
24 Binding
::from_raw(raw
::git_time
{
25 time
: time
as raw
::git_time_t
,
26 offset
: offset
as c_int
,
27 sign
: if offset
< 0 { '-' }
else { '+' }
as c_char
,
32 /// Return the time, in seconds, from epoch
33 pub fn seconds(&self) -> i64 {
37 /// Return the timezone offset, in minutes
38 pub fn offset_minutes(&self) -> i32 {
39 self.raw
.offset
as i32
42 /// Return whether the offset was positive or negative. Primarily useful
43 /// in case the offset is specified as a negative zero.
44 pub fn sign(&self) -> char {
45 self.raw
.sign
as u8 as char
49 impl PartialOrd
for Time
{
50 fn partial_cmp(&self, other
: &Time
) -> Option
<Ordering
> {
56 fn cmp(&self, other
: &Time
) -> Ordering
{
57 (self.raw
.time
, self.raw
.offset
).cmp(&(other
.raw
.time
, other
.raw
.offset
))
61 impl Binding
for Time
{
62 type Raw
= raw
::git_time
;
63 unsafe fn from_raw(raw
: raw
::git_time
) -> Time
{
66 fn raw(&self) -> raw
::git_time
{
72 /// Creates a new time structure from its components.
73 pub fn new(seconds
: i32, nanoseconds
: u32) -> IndexTime
{
75 Binding
::from_raw(raw
::git_index_time
{
82 /// Returns the number of seconds in the second component of this time.
83 pub fn seconds(&self) -> i32 {
86 /// Returns the nanosecond component of this time.
87 pub fn nanoseconds(&self) -> u32 {
92 impl Binding
for IndexTime
{
93 type Raw
= raw
::git_index_time
;
94 unsafe fn from_raw(raw
: raw
::git_index_time
) -> IndexTime
{
97 fn raw(&self) -> raw
::git_index_time
{
102 impl PartialOrd
for IndexTime
{
103 fn partial_cmp(&self, other
: &IndexTime
) -> Option
<Ordering
> {
104 Some(self.cmp(other
))
108 impl Ord
for IndexTime
{
109 fn cmp(&self, other
: &IndexTime
) -> Ordering
{
110 let me
= (self.raw
.seconds
, self.raw
.nanoseconds
);
111 let other
= (other
.raw
.seconds
, other
.raw
.nanoseconds
);
122 assert_eq
!(Time
::new(1608839587, -300).seconds(), 1608839587);
123 assert_eq
!(Time
::new(1608839587, -300).offset_minutes(), -300);
124 assert_eq
!(Time
::new(1608839587, -300).sign(), '
-'
);
125 assert_eq
!(Time
::new(1608839587, 300).sign(), '
+'
);