Skip to content

Commit

Permalink
zeit: add time comparison methods
Browse files Browse the repository at this point in the history
  • Loading branch information
vascocosta authored and rockorager committed Sep 3, 2024
1 parent 9cca8ec commit a82f140
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/zeit.zig
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,12 @@ pub const Date = struct {
day: u5, // 1-31
};

pub const TimeComparison = enum(u2) {
after,
before,
equal,
};

pub const Time = struct {
year: i32 = 1970,
month: Month = .jan,
Expand Down Expand Up @@ -814,6 +820,37 @@ pub const Time = struct {
},
}
}

pub fn compare(self: Time, time: Time) TimeComparison {
const self_instant = self.instant();
const time_instant = time.instant();

if (self_instant.timestamp > time_instant.timestamp) {
return TimeComparison.after;
} else if (self_instant.timestamp < time_instant.timestamp) {
return TimeComparison.before;
} else {
return TimeComparison.equal;
}
}

pub fn after(self: Time, time: Time) bool {
const self_instant = self.instant();
const time_instant = time.instant();
return self_instant.timestamp > time_instant.timestamp;
}

pub fn before(self: Time, time: Time) bool {
const self_instant = self.instant();
const time_instant = time.instant();
return self_instant.timestamp < time_instant.timestamp;
}

pub fn equal(self: Time, time: Time) bool {
const self_instant = self.instant();
const time_instant = time.instant();
return self_instant.timestamp == time_instant.timestamp;
}
};

pub fn daysSinceEpoch(timestamp: i64) i64 {
Expand Down

0 comments on commit a82f140

Please sign in to comment.