v
Зеркало из https://github.com/vlang/v
1import time
2import math
3
4const local_time_to_test = time.new(
5year: 1980
6month: 7
7day: 11
8hour: 21
9minute: 23
10second: 42
11nanosecond: 123456789
12is_local: true
13)
14
15const utc_time_to_test = time.new(
16year: 1980
17month: 7
18day: 11
19hour: 21
20minute: 23
21second: 42
22nanosecond: 123456789
23is_local: false
24)
25
26fn test_is_leap_year() {
27// 1996 % 4 = 0 and 1996 % 100 > 0
28assert time.is_leap_year(1996) == true
29// 2000 % 4 = 0 and 2000 % 400 = 0
30assert time.is_leap_year(2000) == true
31// 1996 % 4 > 0
32assert time.is_leap_year(1997) == false
33// 2000 % 4 = 0 and 2000 % 100 = 0
34assert time.is_leap_year(2100) == false
35}
36
37fn check_days_in_month(month int, year int, expected int) bool {
38res := time.days_in_month(month, year) or { return false }
39return res == expected
40}
41
42fn test_days_in_month() {
43days_in_month := [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
44for i, days in days_in_month {
45month := i + 1
46assert check_days_in_month(month, 2001, days)
47}
48}
49
50fn test_unix() {
51t := time.unix(1564366499)
52assert t.year == 2019
53assert t.month == 7
54assert t.day == 29
55assert t.hour == 2
56assert t.minute == 14
57assert t.second == 59
58t2 := time.unix(1078058096)
59assert t2.year == 2004
60assert t2.month == 2
61assert t2.day == 29
62assert t2.hour == 12
63assert t2.minute == 34
64assert t2.second == 56
65t3 := time.unix(1070236799)
66assert t3.year == 2003
67assert t3.month == 11
68assert t3.day == 30
69assert t3.hour == 23
70assert t3.minute == 59
71assert t3.second == 59
72t4 := time.unix(1577783439)
73assert t4.year == 2019
74assert t4.month == 12
75assert t4.day == 31
76assert t4.hour == 9
77assert t4.minute == 10
78assert t4.second == 39
79t5 := time.unix(-1824922433)
80assert t5.year == 1912
81assert t5.month == 3
82assert t5.day == 4
83assert t5.hour == 5
84assert t5.minute == 6
85assert t5.second == 7
86t6 := time.unix(1577858969)
87assert t6.year == 2020
88assert t6.month == 1
89assert t6.day == 1
90assert t6.hour == 6
91assert t6.minute == 9
92assert t6.second == 29
93assert local_time_to_test.unix() == 332198622
94assert utc_time_to_test.unix() == 332198622
95}
96
97fn test_format_rfc3339() {
98// assert '1980-07-11T19:23:42.123Z'
99res := local_time_to_test.format_rfc3339()
100assert res.ends_with('23:42.123Z')
101assert res.starts_with('1980-07-1')
102assert res.contains('T')
103
104// assert '1980-07-11T19:23:42.123Z'
105utc_res := utc_time_to_test.format_rfc3339()
106assert utc_res.ends_with('23:42.123Z')
107assert utc_res.starts_with('1980-07-1')
108assert utc_res.contains('T')
109}
110
111fn test_format_rfc3339_nano() {
112res := local_time_to_test.format_rfc3339_nano()
113assert res.ends_with('23:42.123456789Z')
114assert res.starts_with('1980-07-1')
115assert res.contains('T')
116
117utc_res := utc_time_to_test.format_rfc3339_nano()
118assert utc_res.ends_with('23:42.123456789Z')
119assert utc_res.starts_with('1980-07-1')
120assert utc_res.contains('T')
121}
122
123fn test_format_ss() {
124assert '11.07.1980 21:23:42' == local_time_to_test.get_fmt_str(.dot, .hhmmss24, .ddmmyyyy)
125
126assert '11.07.1980 21:23:42' == utc_time_to_test.get_fmt_str(.dot, .hhmmss24, .ddmmyyyy)
127}
128
129fn test_format_ss_milli() {
130assert '11.07.1980 21:23:42.123' == local_time_to_test.get_fmt_str(.dot, .hhmmss24_milli,
131.ddmmyyyy)
132assert '1980-07-11 21:23:42.123' == local_time_to_test.format_ss_milli()
133
134assert '11.07.1980 21:23:42.123' == utc_time_to_test.get_fmt_str(.dot, .hhmmss24_milli,
135.ddmmyyyy)
136assert '1980-07-11 21:23:42.123' == utc_time_to_test.format_ss_milli()
137}
138
139fn test_format_ss_micro() {
140assert '11.07.1980 21:23:42.123456' == local_time_to_test.get_fmt_str(.dot, .hhmmss24_micro,
141.ddmmyyyy)
142assert '1980-07-11 21:23:42.123456' == local_time_to_test.format_ss_micro()
143
144assert '11.07.1980 21:23:42.123456' == utc_time_to_test.get_fmt_str(.dot, .hhmmss24_micro,
145.ddmmyyyy)
146assert '1980-07-11 21:23:42.123456' == utc_time_to_test.format_ss_micro()
147}
148
149fn test_format_ss_nano() {
150assert '11.07.1980 21:23:42.123456789' == local_time_to_test.get_fmt_str(.dot, .hhmmss24_nano,
151.ddmmyyyy)
152assert '1980-07-11 21:23:42.123456789' == local_time_to_test.format_ss_nano()
153
154assert '11.07.1980 21:23:42.123456789' == utc_time_to_test.get_fmt_str(.dot, .hhmmss24_nano,
155.ddmmyyyy)
156assert '1980-07-11 21:23:42.123456789' == utc_time_to_test.format_ss_nano()
157}
158
159fn test_smonth() {
160month_names := ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov',
161'Dec']
162for i, name in month_names {
163month_num := i + 1
164t := time.Time{
165year: 1980
166month: month_num
167day: 1
168hour: 0
169minute: 0
170second: 0
171}
172assert t.smonth() == name
173}
174}
175
176fn test_day_of_week() {
177for i in 0 .. 7 {
178day_of_week := i + 1
179// 2 Dec 2019 is Monday
180t := time.Time{
181year: 2019
182month: 12
183day: 2 + i
184hour: 0
185minute: 0
186second: 0
187}
188assert day_of_week == t.day_of_week()
189}
190}
191
192fn test_year_day() {
193// testing if December 31st in a leap year is numbered as 366
194assert time.parse('2024-12-31 20:00:00')!.year_day() == 366
195
196// testing December 31st's number in a non leap year
197assert time.parse('2025-12-31 20:00:00')!.year_day() == 365
198
199assert time.parse('2024-02-28 20:00:00')!.year_day() == 59
200assert time.parse('2024-02-29 20:00:00')!.year_day() == 60
201assert time.parse('2024-03-01 20:00:00')!.year_day() == 61
202assert time.parse('2024-03-02 20:00:00')!.year_day() == 62
203
204assert time.parse('2025-02-28 20:00:00')!.year_day() == 59
205assert time.parse('2025-03-01 20:00:00')!.year_day() == 60
206
207assert time.parse('2024-01-01 20:00:00')!.year_day() == 1
208assert time.parse('2025-01-01 20:00:00')!.year_day() == 1
209}
210
211fn test_weekday_str() {
212day_names := ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
213for i, name in day_names {
214// 2 Dec 2019 is Monday
215t := time.Time{
216year: 2019
217month: 12
218day: 2 + i
219hour: 0
220minute: 0
221second: 0
222}
223assert t.weekday_str() == name
224}
225}
226
227fn test_add() {
228d_seconds := 3
229d_nanoseconds := 13
230duration := time.Duration(d_seconds * time.second + d_nanoseconds * time.nanosecond)
231// dump(duration.debug())
232t1 := local_time_to_test
233// dump(t1.debug())
234t2 := local_time_to_test.add(duration)
235// dump(t2.debug())
236assert t2.second == t1.second + d_seconds
237assert t2.nanosecond == t1.nanosecond + d_nanoseconds
238assert t2.unix() == t1.unix() + d_seconds
239assert t2.is_local == t1.is_local
240
241t3 := local_time_to_test.add(-duration)
242// dump(t3.debug())
243assert t3.second == t1.second - d_seconds
244assert t3.nanosecond == t1.nanosecond - d_nanoseconds
245assert t3.unix() == t1.unix() - d_seconds
246assert t3.is_local == t1.is_local
247
248t4 := local_time_to_test.as_local()
249// dump(t4.debug())
250t5 := t4.add(duration)
251// dump(t5.debug())
252assert t5.is_local == t4.is_local
253
254t := time.Time{
255year: 2024
256month: 4
257day: 3
258}
259t_5am := t.add(time.hour * 5)
260assert t_5am.hour == 5
261next_day := t_5am.add_days(1)
262assert next_day.day == 4 && next_day.day == t_5am.day + 1
263assert next_day.year == t_5am.year && next_day.month == t.month
264assert next_day.month == t_5am.month && next_day.month == t.month
265assert next_day.hour == t_5am.hour && next_day.month == t.month
266}
267
268fn test_add_days() {
269num_of_days := 3
270t := local_time_to_test.add_days(num_of_days)
271assert t.day == local_time_to_test.day + num_of_days
272assert t.unix() == local_time_to_test.unix() + 86400 * num_of_days
273}
274
275fn test_str() {
276assert '1980-07-11 21:23:42' == local_time_to_test.str()
277
278assert '1980-07-11 21:23:42' == utc_time_to_test.str()
279}
280
281// not optimal test but will find obvious bugs
282fn test_now() {
283now := time.now()
284// The year the test was built
285assert now.year >= 2020
286assert now.month > 0
287assert now.month <= 12
288assert now.minute >= 0
289assert now.minute < 60
290assert now.second >= 0
291assert now.second <= 60 // <= 60 cause of leap seconds
292assert now.nanosecond >= 0
293assert now.nanosecond < time.second
294}
295
296fn test_utc() {
297now := time.utc()
298// The year the test was built
299// dump(now.debug())
300assert now.year >= 2020
301assert now.month > 0
302assert now.month <= 12
303assert now.minute >= 0
304assert now.minute < 60
305assert now.second >= 0
306assert now.second <= 60 // <= 60 cause of leap seconds
307assert now.nanosecond >= 0
308assert now.nanosecond < time.second
309}
310
311fn test_unix_time() {
312t1 := time.utc()
313time.sleep(50 * time.millisecond)
314t2 := time.utc()
315eprintln(' t1: ${t1}')
316eprintln(' t2: ${t2}')
317ut1 := t1.unix()
318ut2 := t2.unix()
319eprintln(' ut1: ${ut1}')
320eprintln(' ut2: ${ut2}')
321assert ut2 - ut1 < 2
322
323utm1 := t1.unix_milli()
324utm2 := t2.unix_milli()
325eprintln('utm1: ${utm1}')
326eprintln('utm2: ${utm2}')
327assert (utm1 - ut1 * 1000) < 1000
328assert (utm2 - ut2 * 1000) < 1000
329
330assert utm2 - utm1 > 2
331assert utm2 - utm1 < 999
332}
333
334fn test_offset() {
335u := time.utc()
336n := time.now()
337
338mut diff_seconds := 0
339if u.day != n.day {
340if u.day > n.day {
341diff_seconds = int(math.abs(((u.hour * 60 + u.minute) - (n.hour * 60 + n.minute)) * 60)) - 86400
342} else {
343diff_seconds = 86400 - int(math.abs(((u.hour * 60 + u.minute) - (n.hour * 60 + n.minute)) * 60))
344}
345if math.abs(u.day - n.day) > 1 { // different month
346diff_seconds = diff_seconds * -1
347}
348} else { // same day
349diff_seconds = ((n.hour * 60 + n.minute) - (u.hour * 60 + u.minute)) * 60
350}
351
352assert diff_seconds == time.offset()
353}
354
355fn test_since() {
356t1 := time.now()
357time.sleep(20 * time.millisecond)
358d1 := time.since(t1)
359assert d1 >= 20_000_000
360time.sleep(20 * time.millisecond)
361d2 := time.since(t1)
362assert d2 >= 40_000_000
363}
364
365// issue relate https://github.com/vlang/v/issues/13828
366// problem: the local method add 2h on the time in a Linux machine
367// the other machine are not tested in a local env
368fn test_recursive_local_call() {
369now_tm := time.now()
370assert now_tm.str() == now_tm.local().str()
371assert now_tm.local().str() == now_tm.local().local().str()
372}
373
374fn test_strftime() {
375assert '1980 July 11' == local_time_to_test.strftime('%Y %B %d')
376
377assert '1980 July 11' == utc_time_to_test.strftime('%Y %B %d')
378}
379
380fn test_add_seconds_to_time() {
381now_tm := time.now()
382future_tm := now_tm.add_seconds(60)
383assert now_tm.unix() < future_tm.unix()
384}
385
386fn test_plus_equals_duration() {
387mut d := time.second
388d += time.second
389assert d == 2 * time.second
390}
391
392fn test_parse_three_letters_month() {
393tm := time.now()
394format := 'MMM DD HH:mm:ss YYYY'
395tm_s := tm.custom_format(format)
396tm_tm := time.parse_format(tm_s, format)!
397assert tm_tm.month == tm.month
398}
399
400fn test_parse_ordinal_weekday_d() {
401format := 'd MMM DD HH:mm:ss YYYY'
402dt := '0 Jan 01 00:00:00 1970'
403tm := time.parse_format(dt, format)!
404tm_s := tm.custom_format(format)
405assert tm_s == '4 Jan 01 00:00:00 1970'
406}
407
408fn test_parse_ordinal_weekday_c() {
409format := 'c MMM DD HH:mm:ss YYYY'
410dt := '7 Jan 01 00:00:00 1970'
411tm := time.parse_format(dt, format)!
412tm_s := tm.custom_format(format)
413assert tm_s == '4 Jan 01 00:00:00 1970'
414}
415
416fn test_parse_two_letters_weekday() {
417format := 'dd MMM DD HH:mm:ss YYYY'
418dt := 'Su Jan 01 00:00:00 1970'
419tm := time.parse_format(dt, format)!
420tm_s := tm.custom_format(format)
421assert tm_s == 'Th Jan 01 00:00:00 1970'
422}
423
424fn test_parse_three_letters_weekday() {
425format := 'ddd MMM DD HH:mm:ss YYYY'
426dt := 'Sun Jan 01 00:00:00 1970'
427tm := time.parse_format(dt, format)!
428tm_s := tm.custom_format(format)
429assert tm_s == 'Thu Jan 01 00:00:00 1970'
430}
431
432fn test_parse_weekday() {
433format := 'dddd MMM DD HH:mm:ss YYYY'
434dt := 'Sunday Jan 01 00:00:00 1970'
435tm := time.parse_format(dt, format)!
436tm_s := tm.custom_format(format)
437assert tm_s == 'Thursday Jan 01 00:00:00 1970'
438}
439