From c639dd03d0f405821ab7ac7812134c3636111ead Mon Sep 17 00:00:00 2001 From: zakuro Date: Mon, 21 Dec 2020 17:28:00 +0900 Subject: [PATCH] time: move - operator to operator.v (#7433) --- vlib/time/operator.v | 8 ++++++++ vlib/time/operator_test.v | 22 ++++++++++++++++++++++ vlib/time/time.v | 7 ------- vlib/time/time_test.v | 14 -------------- 4 files changed, 30 insertions(+), 21 deletions(-) diff --git a/vlib/time/operator.v b/vlib/time/operator.v index 56c5a79b95..c944fbdfc1 100644 --- a/vlib/time/operator.v +++ b/vlib/time/operator.v @@ -44,3 +44,11 @@ pub fn (t1 Time) gt(t2 Time) bool { pub fn (t1 Time) ge(t2 Time) bool { return t1.gt(t2) || t1.eq(t2) } + +// Time subtract using eperator overloading +[inline] +pub fn (lhs Time) -(rhs Time) Duration { + lhs_micro := lhs.unix * 1000 * 1000 + u64(lhs.microsecond) + rhs_micro := rhs.unix * 1000 * 1000 + u64(rhs.microsecond) + return (i64(lhs_micro) - i64(rhs_micro)) * microsecond +} diff --git a/vlib/time/operator_test.v b/vlib/time/operator_test.v index caf854823a..d12760ae4c 100644 --- a/vlib/time/operator_test.v +++ b/vlib/time/operator_test.v @@ -367,3 +367,25 @@ fn test_time2_copied_from_time1_should_be_equal() { t2 := new_time(t1) assert t2.eq(t1) } + +fn test_subtract() { + d_seconds := 3 + d_microseconds := 13 + duration := d_seconds * second + d_microseconds * microsecond + t1 := new_time(Time{ + year: 2000 + month: 5 + day: 10 + hour: 22 + minute: 11 + second: 3 + microsecond: 100 + }) + t2 := unix2(int(t1.unix) + d_seconds, t1.microsecond + d_microseconds) + d1 := t2 - t1 + d2 := t1 - t2 + assert d1 > 0 + assert d1 == duration + assert d2 < 0 + assert d2 == -duration +} diff --git a/vlib/time/time.v b/vlib/time/time.v index 371f633437..8c6c9d61fd 100644 --- a/vlib/time/time.v +++ b/vlib/time/time.v @@ -355,13 +355,6 @@ pub fn (t Time) str() string { return t.format_ss() } -// Time subtract using eperator overloading -pub fn (lhs Time) -(rhs Time) Duration { - lhs_micro := lhs.unix * 1000 * 1000 + u64(lhs.microsecond) - rhs_micro := rhs.unix * 1000 * 1000 + u64(rhs.microsecond) - return (i64(lhs_micro) - i64(rhs_micro)) * microsecond -} - fn convert_ctime(t C.tm, microsecond int) Time { return Time{ year: t.tm_year + 1900 diff --git a/vlib/time/time_test.v b/vlib/time/time_test.v index 7a7b2ee71b..858a9231fc 100644 --- a/vlib/time/time_test.v +++ b/vlib/time/time_test.v @@ -177,20 +177,6 @@ fn test_str() { assert '1980-07-11 21:23:42' == time_to_test.str() } -fn test_subtract() { - d_seconds := 3 - d_microseconds := 13 - duration := d_seconds * time.second + d_microseconds * time.microsecond - t1 := time_to_test - t2 := time.unix2(int(t1.unix) + d_seconds, t1.microsecond + d_microseconds) - d1 := t2 - t1 - d2 := t1 - t2 - assert d1 > 0 - assert d1 == duration - assert d2 < 0 - assert d2 == -duration -} - // not optimal test but will find obvious bugs fn test_now() { now := time.now()