2019-07-03 19:55:07 +03:00
|
|
|
import time
|
|
|
|
|
2019-12-22 22:58:41 +03:00
|
|
|
const (
|
2020-01-20 17:06:15 +03:00
|
|
|
time_to_test = time.new_time(time.Time{
|
|
|
|
year: 1980
|
|
|
|
month: 7
|
|
|
|
day: 11
|
|
|
|
hour: 21
|
|
|
|
minute: 23
|
|
|
|
second: 42
|
2019-12-22 22:58:41 +03:00
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2019-07-03 19:55:07 +03:00
|
|
|
fn test_is_leap_year() {
|
2019-12-22 22:58:41 +03:00
|
|
|
// 1996 % 4 = 0 and 1996 % 100 > 0
|
|
|
|
assert time.is_leap_year(1996) == true
|
|
|
|
// 2000 % 4 = 0 and 2000 % 400 = 0
|
|
|
|
assert time.is_leap_year(2000) == true
|
|
|
|
// 1996 % 4 > 0
|
|
|
|
assert time.is_leap_year(1997) == false
|
|
|
|
// 2000 % 4 = 0 and 2000 % 100 = 0
|
|
|
|
assert time.is_leap_year(2100) == false
|
2019-07-14 17:43:57 +03:00
|
|
|
}
|
|
|
|
|
2019-12-31 21:42:16 +03:00
|
|
|
fn test_now_format() {
|
|
|
|
t := time.now()
|
2020-01-20 17:06:15 +03:00
|
|
|
u := t.unix
|
2019-12-31 21:42:16 +03:00
|
|
|
println(u)
|
|
|
|
println(t.format())
|
|
|
|
println(time.unix(u).format())
|
|
|
|
assert t.format() == time.unix(u).format()
|
|
|
|
}
|
|
|
|
|
2019-12-22 22:58:41 +03:00
|
|
|
fn check_days_in_month(month, year, expected int) bool {
|
2019-07-14 17:47:02 +03:00
|
|
|
res := time.days_in_month(month, year) or {
|
2019-07-14 17:43:57 +03:00
|
|
|
return false
|
|
|
|
}
|
2019-12-22 22:58:41 +03:00
|
|
|
return res == expected
|
2019-07-14 17:43:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_days_in_month() {
|
2019-12-22 22:58:41 +03:00
|
|
|
days_in_month := [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
|
|
|
for i, days in days_in_month {
|
|
|
|
month := i + 1
|
|
|
|
assert check_days_in_month(month, 2001, days)
|
|
|
|
}
|
|
|
|
}
|
2019-07-29 19:21:36 +03:00
|
|
|
|
|
|
|
fn test_unix() {
|
2019-12-22 22:58:41 +03:00
|
|
|
t := time.unix(1564366499)
|
2019-07-29 19:21:36 +03:00
|
|
|
assert t.year == 2019
|
2019-12-22 22:58:41 +03:00
|
|
|
assert t.month == 7
|
|
|
|
assert t.day == 29
|
|
|
|
assert t.hour == 2
|
|
|
|
assert t.minute == 14
|
|
|
|
assert t.second == 59
|
2019-12-31 19:11:47 +03:00
|
|
|
t2 := time.unix(1078058096)
|
|
|
|
assert t2.year == 2004
|
|
|
|
assert t2.month == 2
|
|
|
|
assert t2.day == 29
|
|
|
|
assert t2.hour == 12
|
|
|
|
assert t2.minute == 34
|
|
|
|
assert t2.second == 56
|
|
|
|
t3 := time.unix(1070236799)
|
|
|
|
assert t3.year == 2003
|
|
|
|
assert t3.month == 11
|
|
|
|
assert t3.day == 30
|
|
|
|
assert t3.hour == 23
|
|
|
|
assert t3.minute == 59
|
|
|
|
assert t3.second == 59
|
|
|
|
t4 := time.unix(1577783439)
|
|
|
|
assert t4.year == 2019
|
|
|
|
assert t4.month == 12
|
|
|
|
assert t4.day == 31
|
|
|
|
assert t4.hour == 9
|
|
|
|
assert t4.minute == 10
|
|
|
|
assert t4.second == 39
|
|
|
|
t5 := time.unix(-1824922433)
|
|
|
|
assert t5.year == 1912
|
|
|
|
assert t5.month == 3
|
|
|
|
assert t5.day == 4
|
|
|
|
assert t5.hour == 5
|
|
|
|
assert t5.minute == 6
|
|
|
|
assert t5.second == 7
|
2020-01-01 10:00:23 +03:00
|
|
|
t6 := time.unix(1577858969)
|
|
|
|
assert t6.year == 2020
|
|
|
|
assert t6.month == 1
|
|
|
|
assert t6.day == 1
|
|
|
|
assert t6.hour == 6
|
|
|
|
assert t6.minute == 9
|
|
|
|
assert t6.second == 29
|
2019-12-22 22:58:41 +03:00
|
|
|
}
|
2019-07-29 19:21:36 +03:00
|
|
|
|
2019-11-01 02:13:03 +03:00
|
|
|
fn test_format_ss() {
|
2019-12-22 22:58:41 +03:00
|
|
|
assert '11.07.1980 21:23:42' == time_to_test.get_fmt_str(.dot, .hhmmss24, .ddmmyyyy)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_smonth() {
|
2020-01-20 17:06:15 +03:00
|
|
|
month_names := ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
|
|
|
|
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
|
2019-12-22 22:58:41 +03:00
|
|
|
for i, name in month_names {
|
|
|
|
month_num := i + 1
|
2020-01-20 17:06:15 +03:00
|
|
|
t := time.Time{
|
|
|
|
year: 1980
|
|
|
|
month: month_num
|
|
|
|
day: 1
|
|
|
|
hour: 0
|
|
|
|
minute: 0
|
|
|
|
second: 0
|
|
|
|
unix: 0
|
2019-12-22 22:58:41 +03:00
|
|
|
}
|
|
|
|
assert t.smonth() == name
|
|
|
|
}
|
2019-11-01 02:13:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_format() {
|
2019-12-22 22:58:41 +03:00
|
|
|
assert '11.07.1980 21:23' == time_to_test.get_fmt_str(.dot, .hhmm24, .ddmmyyyy)
|
2019-11-01 02:13:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_hhmm() {
|
2019-12-22 22:58:41 +03:00
|
|
|
assert '21:23' == time_to_test.hhmm()
|
2019-11-01 02:13:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_hhmm12() {
|
2019-12-22 22:58:41 +03:00
|
|
|
assert '9:23 p.m.' == time_to_test.hhmm12()
|
2019-11-01 02:13:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_hhmmss() {
|
2019-12-22 22:58:41 +03:00
|
|
|
assert '21:23:42' == time_to_test.hhmmss()
|
2019-11-01 02:13:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_ymmdd() {
|
2019-12-22 22:58:41 +03:00
|
|
|
assert '1980-07-11' == time_to_test.ymmdd()
|
2019-11-01 02:13:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_ddmmy() {
|
2019-12-22 22:58:41 +03:00
|
|
|
assert '11.07.1980' == time_to_test.ddmmy()
|
2019-11-01 02:13:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_md() {
|
2019-12-22 22:58:41 +03:00
|
|
|
assert 'Jul 11' == time_to_test.md()
|
|
|
|
}
|
2019-11-01 02:13:03 +03:00
|
|
|
|
2019-12-22 22:58:41 +03:00
|
|
|
fn test_day_of_week() {
|
|
|
|
for i := 0; i < 7; i++ {
|
|
|
|
day_of_week := i + 1
|
|
|
|
// 2 Dec 2019 is Monday
|
2020-01-20 17:06:15 +03:00
|
|
|
t := time.Time{
|
|
|
|
year: 2019
|
|
|
|
month: 12
|
|
|
|
day: 2 + i
|
|
|
|
hour: 0
|
|
|
|
minute: 0
|
|
|
|
second: 0
|
|
|
|
unix: 0
|
2019-12-22 22:58:41 +03:00
|
|
|
}
|
|
|
|
assert day_of_week == t.day_of_week()
|
|
|
|
}
|
2019-11-01 02:13:03 +03:00
|
|
|
}
|
|
|
|
|
2019-12-22 22:58:41 +03:00
|
|
|
fn test_weekday_str() {
|
|
|
|
day_names := ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
|
|
|
|
for i, name in day_names {
|
|
|
|
// 2 Dec 2019 is Monday
|
2020-01-20 17:06:15 +03:00
|
|
|
t := time.Time{
|
|
|
|
year: 2019
|
|
|
|
month: 12
|
|
|
|
day: 2 + i
|
|
|
|
hour: 0
|
|
|
|
minute: 0
|
|
|
|
second: 0
|
|
|
|
unix: 0
|
2019-12-22 22:58:41 +03:00
|
|
|
}
|
|
|
|
assert t.weekday_str() == name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_add_days() {
|
|
|
|
num_of_days := 3
|
|
|
|
t := time_to_test.add_days(num_of_days)
|
|
|
|
assert t.day == time_to_test.day + num_of_days
|
2020-02-04 14:17:04 +03:00
|
|
|
assert t.unix == time_to_test.unix + 86400 * num_of_days
|
2019-12-22 22:58:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_get_fmt_time_str() {
|
|
|
|
assert '21:23:42' == time_to_test.get_fmt_time_str(.hhmmss24)
|
|
|
|
assert '21:23' == time_to_test.get_fmt_time_str(.hhmm24)
|
|
|
|
assert '9:23:42 p.m.' == time_to_test.get_fmt_time_str(.hhmmss12)
|
|
|
|
assert '9:23 p.m.' == time_to_test.get_fmt_time_str(.hhmm12)
|
2019-11-01 02:13:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_get_fmt_date_str() {
|
2019-12-22 22:58:41 +03:00
|
|
|
assert '11.07.1980' == time_to_test.get_fmt_date_str(.dot, .ddmmyyyy)
|
|
|
|
assert '11/07/1980' == time_to_test.get_fmt_date_str(.slash, .ddmmyyyy)
|
|
|
|
assert '11-07-1980' == time_to_test.get_fmt_date_str(.hyphen, .ddmmyyyy)
|
|
|
|
assert '11 07 1980' == time_to_test.get_fmt_date_str(.space, .ddmmyyyy)
|
|
|
|
assert '07.11.1980' == time_to_test.get_fmt_date_str(.dot, .mmddyyyy)
|
|
|
|
assert '07/11/1980' == time_to_test.get_fmt_date_str(.slash, .mmddyyyy)
|
2020-01-20 17:06:15 +03:00
|
|
|
assert '07-11-1980' == time_to_test.get_fmt_date_str(.hyphen, .mmddyyyy)
|
2019-12-22 22:58:41 +03:00
|
|
|
assert '07 11 1980' == time_to_test.get_fmt_date_str(.space, .mmddyyyy)
|
|
|
|
assert '11.07.80' == time_to_test.get_fmt_date_str(.dot, .ddmmyy)
|
|
|
|
assert '11/07/80' == time_to_test.get_fmt_date_str(.slash, .ddmmyy)
|
|
|
|
assert '11-07-80' == time_to_test.get_fmt_date_str(.hyphen, .ddmmyy)
|
|
|
|
assert '11 07 80' == time_to_test.get_fmt_date_str(.space, .ddmmyy)
|
|
|
|
assert '07.11.80' == time_to_test.get_fmt_date_str(.dot, .mmddyy)
|
|
|
|
assert '07/11/80' == time_to_test.get_fmt_date_str(.slash, .mmddyy)
|
|
|
|
assert '07-11-80' == time_to_test.get_fmt_date_str(.hyphen, .mmddyy)
|
|
|
|
assert '07 11 80' == time_to_test.get_fmt_date_str(.space, .mmddyy)
|
|
|
|
assert 'Jul 11' == time_to_test.get_fmt_date_str(.space, .mmmd)
|
|
|
|
assert 'Jul 11' == time_to_test.get_fmt_date_str(.space, .mmmdd)
|
|
|
|
assert 'Jul 11 1980' == time_to_test.get_fmt_date_str(.space, .mmmddyyyy)
|
|
|
|
assert '1980-07-11' == time_to_test.get_fmt_date_str(.hyphen, .yyyymmdd)
|
2019-11-01 02:13:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_get_fmt_str() {
|
2019-12-22 22:58:41 +03:00
|
|
|
// Since get_fmt_time_str and get_fmt_date_str do have comprehensive
|
|
|
|
// tests I don't want to exaggerate here with all possible
|
|
|
|
// combinations.
|
2020-01-20 17:06:15 +03:00
|
|
|
assert '11.07.1980 21:23:42' == time_to_test.get_fmt_str(.dot, .hhmmss24, .ddmmyyyy)
|
2019-11-01 02:13:03 +03:00
|
|
|
}
|
2019-12-23 13:25:44 +03:00
|
|
|
|
|
|
|
fn test_parse() {
|
|
|
|
s := '2018-01-27 12:48:34'
|
|
|
|
t := time.parse(s)
|
2020-01-20 17:06:15 +03:00
|
|
|
assert t.year == 2018 && t.month == 1 && t.day == 27 && t.hour == 12 && t.minute == 48 && t.second == 34
|
2020-02-05 08:13:11 +03:00
|
|
|
assert t.unix == 1517057314
|
2019-12-23 13:25:44 +03:00
|
|
|
}
|
|
|
|
|
2019-12-23 13:36:51 +03:00
|
|
|
fn test_parse_iso() {
|
|
|
|
s1 := 'Thu, 12 Dec 2019 06:07:45 GMT'
|
|
|
|
t1 := time.parse_iso(s1)
|
2020-01-20 17:06:15 +03:00
|
|
|
assert t1.year == 2019 && t1.month == 12 && t1.day == 12 && t1.hour == 6 && t1.minute == 7 && t1.second == 45
|
2020-02-05 08:13:11 +03:00
|
|
|
assert t1.unix == 1576130865
|
2019-12-23 13:36:51 +03:00
|
|
|
s2 := 'Thu 12 Dec 2019 06:07:45 +0800'
|
|
|
|
t2 := time.parse_iso(s2)
|
2020-01-20 17:06:15 +03:00
|
|
|
assert t2.year == 2019 && t2.month == 12 && t2.day == 12 && t2.hour == 6 && t2.minute == 7 && t2.second == 45
|
2020-02-05 08:13:11 +03:00
|
|
|
assert t2.unix == 1576130865
|
2019-12-23 13:36:51 +03:00
|
|
|
s3 := 'Thu 12 Foo 2019 06:07:45 +0800'
|
|
|
|
t3 := time.parse_iso(s3)
|
2020-01-20 17:06:15 +03:00
|
|
|
assert t3.year == 0 && t3.month == 0 && t3.day == 0 && t3.hour == 0 && t3.minute == 0 && t3.second == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_str() {
|
|
|
|
assert '1980-07-11 21:23:42' == time_to_test.str()
|
2019-12-23 13:36:51 +03:00
|
|
|
}
|