From def785cd0a7697358321d23201bf494b8f33ac1b Mon Sep 17 00:00:00 2001 From: mertcandav Date: Thu, 28 Nov 2024 20:41:47 +0300 Subject: [PATCH] std/time: add the Compare method to Time --- std/time/time.jule | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/std/time/time.jule b/std/time/time.jule index 644c88f0..4edfa1bd 100644 --- a/std/time/time.jule +++ b/std/time/time.jule @@ -312,7 +312,23 @@ impl Time { // Reports whether the time instant is before u. fn Before(self, u: Time): bool { - ret self.sec < u.sec || self.sec == s.sec && self.nsec < u.nsec + ret self.sec < u.sec || self.sec == u.sec && self.nsec < u.nsec + } + + // Compares the time instant t(self) with u. If t is before u, it returns -1; + // if t is after u, it returns +1; if they're the same, it returns 0. + fn Compare(self, u: Time): int { + mut tc, mut uc := self.sec, u.sec + if tc == uc { + tc, uc = i64(self.nsec), i64(u.nsec) + } + match { + | tc < uc: + ret -1 + | tc > uc: + ret +1 + } + ret 0 } fn appendTo(self, mut b: []byte)!: []byte {