2020-06-07 16:19:09 +03:00
|
|
|
module time
|
|
|
|
|
|
|
|
fn assert_greater_time(ms int, t1 Time) {
|
2021-07-28 09:22:19 +03:00
|
|
|
sleep(ms * millisecond)
|
2020-06-07 16:19:09 +03:00
|
|
|
t2 := now()
|
2021-01-05 13:43:34 +03:00
|
|
|
assert t2 > t1
|
2020-06-07 16:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests the now in all platform and the gt operator function with at least ms resolution
|
|
|
|
fn test_now_always_results_in_greater_time() {
|
|
|
|
t1 := now()
|
|
|
|
$if macos {
|
|
|
|
assert_greater_time(1, t1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
$if windows {
|
|
|
|
// Lower resolution of time for windows
|
|
|
|
assert_greater_time(15, t1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
$if linux {
|
|
|
|
assert_greater_time(1, t1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
$if solaris {
|
|
|
|
assert_greater_time(1, t1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// other platforms may have more accurate resolution,
|
|
|
|
// but we do not know that ... so wait at least 1s:
|
|
|
|
assert_greater_time(1001, t1)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_time1_should_be_same_as_time2() {
|
2020-12-06 17:19:39 +03:00
|
|
|
t1 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 3
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 100
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
2020-12-06 17:19:39 +03:00
|
|
|
t2 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 3
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 100
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
2021-01-05 13:43:34 +03:00
|
|
|
assert t1 == t2
|
2020-06-07 16:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_time1_should_not_be_same_as_time2() {
|
2020-12-06 17:19:39 +03:00
|
|
|
t1 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 3
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 100
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
2023-08-05 23:41:23 +03:00
|
|
|
// Difference is one nanosecond
|
2020-12-06 17:19:39 +03:00
|
|
|
t2 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 3
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 101
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
2020-12-06 17:19:39 +03:00
|
|
|
t3 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 3
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 0
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
|
|
|
// Difference is one second
|
2020-12-06 17:19:39 +03:00
|
|
|
t4 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 4
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 0
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
2021-01-05 13:43:34 +03:00
|
|
|
assert t1 != t2
|
|
|
|
assert t3 != t4
|
2020-06-07 16:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_time1_should_be_greater_than_time2() {
|
2020-12-06 17:19:39 +03:00
|
|
|
t1 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 3
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 102
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
2023-08-05 23:41:23 +03:00
|
|
|
// Difference is one nanosecond
|
2020-12-06 17:19:39 +03:00
|
|
|
t2 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 3
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 101
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
2020-12-06 17:19:39 +03:00
|
|
|
t3 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 5
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 0
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
|
|
|
// Difference is one second
|
2020-12-06 17:19:39 +03:00
|
|
|
t4 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 4
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 0
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
2021-01-05 13:43:34 +03:00
|
|
|
assert t1 > t2
|
|
|
|
assert t3 > t4
|
2020-06-07 16:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_time2_should_be_less_than_time1() {
|
2020-12-06 17:19:39 +03:00
|
|
|
t1 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 3
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 102
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
2023-08-05 23:41:23 +03:00
|
|
|
// Difference is one nanosecond
|
2020-12-06 17:19:39 +03:00
|
|
|
t2 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 3
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 101
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
2020-12-06 17:19:39 +03:00
|
|
|
t3 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 3
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 0
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
|
|
|
// Difference is one second
|
2020-12-06 17:19:39 +03:00
|
|
|
t4 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 2
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 0
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
2021-01-05 13:43:34 +03:00
|
|
|
assert t2 < t1
|
|
|
|
assert t4 < t3
|
2020-06-07 16:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_time1_should_be_greater_or_equal_to_time2_when_gt() {
|
2020-12-06 17:19:39 +03:00
|
|
|
t1 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 3
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 102
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
2023-08-05 23:41:23 +03:00
|
|
|
// Difference is one nanosecond
|
2020-12-06 17:19:39 +03:00
|
|
|
t2 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 3
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 101
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
2020-12-06 17:19:39 +03:00
|
|
|
t3 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 5
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 0
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
|
|
|
// Difference is one second
|
2020-12-06 17:19:39 +03:00
|
|
|
t4 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 4
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 0
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
2021-01-10 23:05:05 +03:00
|
|
|
assert t1 >= t2
|
|
|
|
assert t3 >= t4
|
2020-06-07 16:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_time1_should_be_greater_or_equal_to_time2_when_eq() {
|
2020-12-06 17:19:39 +03:00
|
|
|
t1 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 3
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 100
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
2023-08-05 23:41:23 +03:00
|
|
|
// Difference is one nanosecond
|
2020-12-06 17:19:39 +03:00
|
|
|
t2 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 3
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 100
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
2020-12-06 17:19:39 +03:00
|
|
|
t3 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 3
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 0
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
|
|
|
// Difference is one second
|
2020-12-06 17:19:39 +03:00
|
|
|
t4 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 3
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 0
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
2021-01-10 23:05:05 +03:00
|
|
|
assert t1 >= t2
|
|
|
|
assert t3 >= t4
|
2020-06-07 16:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_time1_should_be_less_or_equal_to_time2_when_lt() {
|
2020-12-06 17:19:39 +03:00
|
|
|
t1 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 3
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 100
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
2023-08-05 23:41:23 +03:00
|
|
|
// Difference is one nanosecond
|
2020-12-06 17:19:39 +03:00
|
|
|
t2 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 3
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 101
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
2020-12-06 17:19:39 +03:00
|
|
|
t3 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 3
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 0
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
|
|
|
// Difference is one second
|
2020-12-06 17:19:39 +03:00
|
|
|
t4 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 4
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 0
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
2021-01-10 23:05:05 +03:00
|
|
|
assert t1 <= t2
|
|
|
|
assert t3 <= t4
|
2020-06-07 16:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_time1_should_be_less_or_equal_to_time2_when_eq() {
|
2020-12-06 17:19:39 +03:00
|
|
|
t1 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 3
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 100
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
2023-08-05 23:41:23 +03:00
|
|
|
// Difference is one nanosecond
|
2020-12-06 17:19:39 +03:00
|
|
|
t2 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 3
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 100
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
2020-12-06 17:19:39 +03:00
|
|
|
t3 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 3
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 0
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
|
|
|
// Difference is one second
|
2020-12-06 17:19:39 +03:00
|
|
|
t4 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 3
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 0
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
2021-01-10 23:05:05 +03:00
|
|
|
assert t1 <= t2
|
|
|
|
assert t3 <= t4
|
2020-06-07 16:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_time2_copied_from_time1_should_be_equal() {
|
2020-12-06 17:19:39 +03:00
|
|
|
t1 := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 3
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 100
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
|
|
|
t2 := new_time(t1)
|
2021-01-05 13:43:34 +03:00
|
|
|
assert t2 == t1
|
2020-06-07 16:19:09 +03:00
|
|
|
}
|
2020-12-21 11:28:00 +03:00
|
|
|
|
|
|
|
fn test_subtract() {
|
|
|
|
d_seconds := 3
|
2023-08-05 23:41:23 +03:00
|
|
|
d_nanoseconds := 13
|
|
|
|
duration := d_seconds * second + d_nanoseconds * nanosecond
|
2020-12-21 11:28:00 +03:00
|
|
|
t1 := new_time(Time{
|
|
|
|
year: 2000
|
|
|
|
month: 5
|
|
|
|
day: 10
|
|
|
|
hour: 22
|
|
|
|
minute: 11
|
|
|
|
second: 3
|
2023-08-05 23:41:23 +03:00
|
|
|
nanosecond: 100
|
2020-12-21 11:28:00 +03:00
|
|
|
})
|
2023-08-05 23:41:23 +03:00
|
|
|
t2 := unix_nanosecond(i64(t1.unix) + d_seconds, t1.nanosecond + d_nanoseconds)
|
2020-12-21 11:28:00 +03:00
|
|
|
d1 := t2 - t1
|
|
|
|
d2 := t1 - t2
|
|
|
|
assert d1 > 0
|
|
|
|
assert d1 == duration
|
|
|
|
assert d2 < 0
|
|
|
|
assert d2 == -duration
|
|
|
|
}
|