v / vlib / time / time_format_test.v
98 lines · 82 sloc · 3.22 KB · 39ef9b2a9ee20cfc47b179298ca966d551dc374e
Raw
1import time
2
3const time_to_test = time.Time{
4 year: 1980
5 month: 7
6 day: 11
7 hour: 21
8 minute: 23
9 second: 42
10}
11
12fn test_now_format() {
13 t := time.now()
14 u := t.unix()
15 assert t.format() == time.unix(int(u)).utc_to_local().format()
16}
17
18fn test_format() {
19 assert '11.07.1980 21:23' == time_to_test.get_fmt_str(.dot, .hhmm24, .ddmmyyyy)
20}
21
22fn test_hhmm() {
23 assert '21:23' == time_to_test.hhmm()
24}
25
26fn test_hhmm12() {
27 assert '9:23 p.m.' == time_to_test.hhmm12()
28}
29
30fn test_hhmmss() {
31 assert '21:23:42' == time_to_test.hhmmss()
32}
33
34fn test_ymmdd() {
35 assert '1980-07-11' == time_to_test.ymmdd()
36}
37
38fn test_ddmmy() {
39 assert '11.07.1980' == time_to_test.ddmmy()
40}
41
42fn test_md() {
43 assert 'Jul 11' == time_to_test.md()
44}
45
46fn test_get_fmt_time_str() {
47 assert '21:23:42' == time_to_test.get_fmt_time_str(.hhmmss24)
48 assert '21:23' == time_to_test.get_fmt_time_str(.hhmm24)
49 assert '9:23:42 p.m.' == time_to_test.get_fmt_time_str(.hhmmss12)
50 assert '9:23 p.m.' == time_to_test.get_fmt_time_str(.hhmm12)
51}
52
53fn test_get_fmt_date_str() {
54 assert '11.07.1980' == time_to_test.get_fmt_date_str(.dot, .ddmmyyyy)
55 assert '11/07/1980' == time_to_test.get_fmt_date_str(.slash, .ddmmyyyy)
56 assert '11-07-1980' == time_to_test.get_fmt_date_str(.hyphen, .ddmmyyyy)
57 assert '11 07 1980' == time_to_test.get_fmt_date_str(.space, .ddmmyyyy)
58 assert '07.11.1980' == time_to_test.get_fmt_date_str(.dot, .mmddyyyy)
59 assert '07/11/1980' == time_to_test.get_fmt_date_str(.slash, .mmddyyyy)
60 assert '07-11-1980' == time_to_test.get_fmt_date_str(.hyphen, .mmddyyyy)
61 assert '07 11 1980' == time_to_test.get_fmt_date_str(.space, .mmddyyyy)
62 assert '11.07.80' == time_to_test.get_fmt_date_str(.dot, .ddmmyy)
63 assert '11/07/80' == time_to_test.get_fmt_date_str(.slash, .ddmmyy)
64 assert '11-07-80' == time_to_test.get_fmt_date_str(.hyphen, .ddmmyy)
65 assert '11 07 80' == time_to_test.get_fmt_date_str(.space, .ddmmyy)
66 assert '07.11.80' == time_to_test.get_fmt_date_str(.dot, .mmddyy)
67 assert '07/11/80' == time_to_test.get_fmt_date_str(.slash, .mmddyy)
68 assert '07-11-80' == time_to_test.get_fmt_date_str(.hyphen, .mmddyy)
69 assert '07 11 80' == time_to_test.get_fmt_date_str(.space, .mmddyy)
70 assert 'Jul 11' == time_to_test.get_fmt_date_str(.space, .mmmd)
71 assert 'Jul 11' == time_to_test.get_fmt_date_str(.space, .mmmdd)
72 assert 'Jul 11 80' == time_to_test.get_fmt_date_str(.space, .mmmddyy)
73 assert 'Jul 11 1980' == time_to_test.get_fmt_date_str(.space, .mmmddyyyy)
74 assert '1980-07-11' == time_to_test.get_fmt_date_str(.hyphen, .yyyymmdd)
75 assert '80.07.11' == time_to_test.get_fmt_date_str(.dot, .yymmdd)
76}
77
78fn test_get_fmt_str() {
79 // Since get_fmt_time_str and get_fmt_date_str do have comprehensive
80 // tests I don't want to exaggerate here with all possible
81 // combinations.
82 assert '11.07.1980 21:23:42' == time_to_test.get_fmt_str(.dot, .hhmmss24, .ddmmyyyy)
83}
84
85fn test_utc_string() {
86 assert 'Fri, 11 Jul 1980 21:23:42 UTC' == time_to_test.utc_string()
87}
88
89fn test_http_header_string() {
90 assert 'Fri, 11 Jul 1980 21:23:42 GMT' == time_to_test.http_header_string()
91}
92
93fn test_push_to_http_header() {
94 mut http1_1_buffer := 'HTTP/1.1 200 OK\r\nDate: '.bytes()
95 time_to_test.push_to_http_header(mut http1_1_buffer)
96
97 assert http1_1_buffer.bytestr() == 'HTTP/1.1 200 OK\r\nDate: Fri, 11 Jul 1980 21:23:42 GMT'
98}
99