v / vlib / time / operator.v
27 lines · 24 sloc · 900 bytes · 21c46f4ae5132ef01ded0b9eda90d25e08a01bca
Raw
1module time
2
3// operator `==` returns true if provided time is equal to time
4@[inline]
5pub fn (t1 Time) == (t2 Time) bool {
6 return t1.nanosecond == t2.nanosecond && t1.is_local == t2.is_local
7 && t1.local_unix() == t2.local_unix()
8}
9
10// operator `<` returns true if provided time is less than time
11@[inline]
12pub fn (t1 Time) < (t2 Time) bool {
13 t1u := t1.unix()
14 t2u := t2.unix()
15 return t1u < t2u || (t1u == t2u && t1.nanosecond < t2.nanosecond)
16}
17
18// Time subtract using operator overloading.
19@[inline]
20pub fn (lhs Time) - (rhs Time) Duration {
21 // lhs.unix * 1_000_000_000 + i64(lhs.nanosecond) will overflow i64, for years > 3000 .
22 // Doing the diff first, and *then* multiplying by `second`, is less likely to overflow,
23 // since lhs and rhs will be likely close to each other.
24 unixs := i64(lhs.unix() - rhs.unix()) * second
25 nanos := lhs.nanosecond - rhs.nanosecond
26 return unixs + nanos
27}
28