v

Зеркало из https://github.com/vlang/v
Форк
0
/
time_test.v 
438 строк · 11.2 Кб
1
import time
2
import math
3

4
const local_time_to_test = time.new(
5
	year:       1980
6
	month:      7
7
	day:        11
8
	hour:       21
9
	minute:     23
10
	second:     42
11
	nanosecond: 123456789
12
	is_local:   true
13
)
14

15
const utc_time_to_test = time.new(
16
	year:       1980
17
	month:      7
18
	day:        11
19
	hour:       21
20
	minute:     23
21
	second:     42
22
	nanosecond: 123456789
23
	is_local:   false
24
)
25

26
fn test_is_leap_year() {
27
	// 1996 % 4 = 0 and 1996 % 100 > 0
28
	assert time.is_leap_year(1996) == true
29
	// 2000 % 4 = 0 and 2000 % 400 = 0
30
	assert time.is_leap_year(2000) == true
31
	// 1996 % 4 > 0
32
	assert time.is_leap_year(1997) == false
33
	// 2000 % 4 = 0 and 2000 % 100 = 0
34
	assert time.is_leap_year(2100) == false
35
}
36

37
fn check_days_in_month(month int, year int, expected int) bool {
38
	res := time.days_in_month(month, year) or { return false }
39
	return res == expected
40
}
41

42
fn test_days_in_month() {
43
	days_in_month := [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
44
	for i, days in days_in_month {
45
		month := i + 1
46
		assert check_days_in_month(month, 2001, days)
47
	}
48
}
49

50
fn test_unix() {
51
	t := time.unix(1564366499)
52
	assert t.year == 2019
53
	assert t.month == 7
54
	assert t.day == 29
55
	assert t.hour == 2
56
	assert t.minute == 14
57
	assert t.second == 59
58
	t2 := time.unix(1078058096)
59
	assert t2.year == 2004
60
	assert t2.month == 2
61
	assert t2.day == 29
62
	assert t2.hour == 12
63
	assert t2.minute == 34
64
	assert t2.second == 56
65
	t3 := time.unix(1070236799)
66
	assert t3.year == 2003
67
	assert t3.month == 11
68
	assert t3.day == 30
69
	assert t3.hour == 23
70
	assert t3.minute == 59
71
	assert t3.second == 59
72
	t4 := time.unix(1577783439)
73
	assert t4.year == 2019
74
	assert t4.month == 12
75
	assert t4.day == 31
76
	assert t4.hour == 9
77
	assert t4.minute == 10
78
	assert t4.second == 39
79
	t5 := time.unix(-1824922433)
80
	assert t5.year == 1912
81
	assert t5.month == 3
82
	assert t5.day == 4
83
	assert t5.hour == 5
84
	assert t5.minute == 6
85
	assert t5.second == 7
86
	t6 := time.unix(1577858969)
87
	assert t6.year == 2020
88
	assert t6.month == 1
89
	assert t6.day == 1
90
	assert t6.hour == 6
91
	assert t6.minute == 9
92
	assert t6.second == 29
93
	assert local_time_to_test.unix() == 332198622
94
	assert utc_time_to_test.unix() == 332198622
95
}
96

97
fn test_format_rfc3339() {
98
	// assert '1980-07-11T19:23:42.123Z'
99
	res := local_time_to_test.format_rfc3339()
100
	assert res.ends_with('23:42.123Z')
101
	assert res.starts_with('1980-07-1')
102
	assert res.contains('T')
103

104
	// assert '1980-07-11T19:23:42.123Z'
105
	utc_res := utc_time_to_test.format_rfc3339()
106
	assert utc_res.ends_with('23:42.123Z')
107
	assert utc_res.starts_with('1980-07-1')
108
	assert utc_res.contains('T')
109
}
110

111
fn test_format_rfc3339_nano() {
112
	res := local_time_to_test.format_rfc3339_nano()
113
	assert res.ends_with('23:42.123456789Z')
114
	assert res.starts_with('1980-07-1')
115
	assert res.contains('T')
116

117
	utc_res := utc_time_to_test.format_rfc3339_nano()
118
	assert utc_res.ends_with('23:42.123456789Z')
119
	assert utc_res.starts_with('1980-07-1')
120
	assert utc_res.contains('T')
121
}
122

123
fn test_format_ss() {
124
	assert '11.07.1980 21:23:42' == local_time_to_test.get_fmt_str(.dot, .hhmmss24, .ddmmyyyy)
125

126
	assert '11.07.1980 21:23:42' == utc_time_to_test.get_fmt_str(.dot, .hhmmss24, .ddmmyyyy)
127
}
128

129
fn test_format_ss_milli() {
130
	assert '11.07.1980 21:23:42.123' == local_time_to_test.get_fmt_str(.dot, .hhmmss24_milli,
131
		.ddmmyyyy)
132
	assert '1980-07-11 21:23:42.123' == local_time_to_test.format_ss_milli()
133

134
	assert '11.07.1980 21:23:42.123' == utc_time_to_test.get_fmt_str(.dot, .hhmmss24_milli,
135
		.ddmmyyyy)
136
	assert '1980-07-11 21:23:42.123' == utc_time_to_test.format_ss_milli()
137
}
138

139
fn test_format_ss_micro() {
140
	assert '11.07.1980 21:23:42.123456' == local_time_to_test.get_fmt_str(.dot, .hhmmss24_micro,
141
		.ddmmyyyy)
142
	assert '1980-07-11 21:23:42.123456' == local_time_to_test.format_ss_micro()
143

144
	assert '11.07.1980 21:23:42.123456' == utc_time_to_test.get_fmt_str(.dot, .hhmmss24_micro,
145
		.ddmmyyyy)
146
	assert '1980-07-11 21:23:42.123456' == utc_time_to_test.format_ss_micro()
147
}
148

149
fn test_format_ss_nano() {
150
	assert '11.07.1980 21:23:42.123456789' == local_time_to_test.get_fmt_str(.dot, .hhmmss24_nano,
151
		.ddmmyyyy)
152
	assert '1980-07-11 21:23:42.123456789' == local_time_to_test.format_ss_nano()
153

154
	assert '11.07.1980 21:23:42.123456789' == utc_time_to_test.get_fmt_str(.dot, .hhmmss24_nano,
155
		.ddmmyyyy)
156
	assert '1980-07-11 21:23:42.123456789' == utc_time_to_test.format_ss_nano()
157
}
158

159
fn test_smonth() {
160
	month_names := ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov',
161
		'Dec']
162
	for i, name in month_names {
163
		month_num := i + 1
164
		t := time.Time{
165
			year:   1980
166
			month:  month_num
167
			day:    1
168
			hour:   0
169
			minute: 0
170
			second: 0
171
		}
172
		assert t.smonth() == name
173
	}
174
}
175

176
fn test_day_of_week() {
177
	for i in 0 .. 7 {
178
		day_of_week := i + 1
179
		// 2 Dec 2019 is Monday
180
		t := time.Time{
181
			year:   2019
182
			month:  12
183
			day:    2 + i
184
			hour:   0
185
			minute: 0
186
			second: 0
187
		}
188
		assert day_of_week == t.day_of_week()
189
	}
190
}
191

192
fn test_year_day() {
193
	// testing if December 31st in a leap year is numbered as 366
194
	assert time.parse('2024-12-31 20:00:00')!.year_day() == 366
195

196
	// testing December 31st's number in a non leap year
197
	assert time.parse('2025-12-31 20:00:00')!.year_day() == 365
198

199
	assert time.parse('2024-02-28 20:00:00')!.year_day() == 59
200
	assert time.parse('2024-02-29 20:00:00')!.year_day() == 60
201
	assert time.parse('2024-03-01 20:00:00')!.year_day() == 61
202
	assert time.parse('2024-03-02 20:00:00')!.year_day() == 62
203

204
	assert time.parse('2025-02-28 20:00:00')!.year_day() == 59
205
	assert time.parse('2025-03-01 20:00:00')!.year_day() == 60
206

207
	assert time.parse('2024-01-01 20:00:00')!.year_day() == 1
208
	assert time.parse('2025-01-01 20:00:00')!.year_day() == 1
209
}
210

211
fn test_weekday_str() {
212
	day_names := ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
213
	for i, name in day_names {
214
		// 2 Dec 2019 is Monday
215
		t := time.Time{
216
			year:   2019
217
			month:  12
218
			day:    2 + i
219
			hour:   0
220
			minute: 0
221
			second: 0
222
		}
223
		assert t.weekday_str() == name
224
	}
225
}
226

227
fn test_add() {
228
	d_seconds := 3
229
	d_nanoseconds := 13
230
	duration := time.Duration(d_seconds * time.second + d_nanoseconds * time.nanosecond)
231
	// dump(duration.debug())
232
	t1 := local_time_to_test
233
	// dump(t1.debug())
234
	t2 := local_time_to_test.add(duration)
235
	// dump(t2.debug())
236
	assert t2.second == t1.second + d_seconds
237
	assert t2.nanosecond == t1.nanosecond + d_nanoseconds
238
	assert t2.unix() == t1.unix() + d_seconds
239
	assert t2.is_local == t1.is_local
240

241
	t3 := local_time_to_test.add(-duration)
242
	// dump(t3.debug())
243
	assert t3.second == t1.second - d_seconds
244
	assert t3.nanosecond == t1.nanosecond - d_nanoseconds
245
	assert t3.unix() == t1.unix() - d_seconds
246
	assert t3.is_local == t1.is_local
247

248
	t4 := local_time_to_test.as_local()
249
	// dump(t4.debug())
250
	t5 := t4.add(duration)
251
	// dump(t5.debug())
252
	assert t5.is_local == t4.is_local
253

254
	t := time.Time{
255
		year:  2024
256
		month: 4
257
		day:   3
258
	}
259
	t_5am := t.add(time.hour * 5)
260
	assert t_5am.hour == 5
261
	next_day := t_5am.add_days(1)
262
	assert next_day.day == 4 && next_day.day == t_5am.day + 1
263
	assert next_day.year == t_5am.year && next_day.month == t.month
264
	assert next_day.month == t_5am.month && next_day.month == t.month
265
	assert next_day.hour == t_5am.hour && next_day.month == t.month
266
}
267

268
fn test_add_days() {
269
	num_of_days := 3
270
	t := local_time_to_test.add_days(num_of_days)
271
	assert t.day == local_time_to_test.day + num_of_days
272
	assert t.unix() == local_time_to_test.unix() + 86400 * num_of_days
273
}
274

275
fn test_str() {
276
	assert '1980-07-11 21:23:42' == local_time_to_test.str()
277

278
	assert '1980-07-11 21:23:42' == utc_time_to_test.str()
279
}
280

281
// not optimal test but will find obvious bugs
282
fn test_now() {
283
	now := time.now()
284
	// The year the test was built
285
	assert now.year >= 2020
286
	assert now.month > 0
287
	assert now.month <= 12
288
	assert now.minute >= 0
289
	assert now.minute < 60
290
	assert now.second >= 0
291
	assert now.second <= 60 // <= 60 cause of leap seconds
292
	assert now.nanosecond >= 0
293
	assert now.nanosecond < time.second
294
}
295

296
fn test_utc() {
297
	now := time.utc()
298
	// The year the test was built
299
	// dump(now.debug())
300
	assert now.year >= 2020
301
	assert now.month > 0
302
	assert now.month <= 12
303
	assert now.minute >= 0
304
	assert now.minute < 60
305
	assert now.second >= 0
306
	assert now.second <= 60 // <= 60 cause of leap seconds
307
	assert now.nanosecond >= 0
308
	assert now.nanosecond < time.second
309
}
310

311
fn test_unix_time() {
312
	t1 := time.utc()
313
	time.sleep(50 * time.millisecond)
314
	t2 := time.utc()
315
	eprintln('  t1: ${t1}')
316
	eprintln('  t2: ${t2}')
317
	ut1 := t1.unix()
318
	ut2 := t2.unix()
319
	eprintln(' ut1: ${ut1}')
320
	eprintln(' ut2: ${ut2}')
321
	assert ut2 - ut1 < 2
322

323
	utm1 := t1.unix_milli()
324
	utm2 := t2.unix_milli()
325
	eprintln('utm1: ${utm1}')
326
	eprintln('utm2: ${utm2}')
327
	assert (utm1 - ut1 * 1000) < 1000
328
	assert (utm2 - ut2 * 1000) < 1000
329

330
	assert utm2 - utm1 > 2
331
	assert utm2 - utm1 < 999
332
}
333

334
fn test_offset() {
335
	u := time.utc()
336
	n := time.now()
337

338
	mut diff_seconds := 0
339
	if u.day != n.day {
340
		if u.day > n.day {
341
			diff_seconds = int(math.abs(((u.hour * 60 + u.minute) - (n.hour * 60 + n.minute)) * 60)) - 86400
342
		} else {
343
			diff_seconds = 86400 - int(math.abs(((u.hour * 60 + u.minute) - (n.hour * 60 + n.minute)) * 60))
344
		}
345
		if math.abs(u.day - n.day) > 1 { // different month
346
			diff_seconds = diff_seconds * -1
347
		}
348
	} else { // same day
349
		diff_seconds = ((n.hour * 60 + n.minute) - (u.hour * 60 + u.minute)) * 60
350
	}
351

352
	assert diff_seconds == time.offset()
353
}
354

355
fn test_since() {
356
	t1 := time.now()
357
	time.sleep(20 * time.millisecond)
358
	d1 := time.since(t1)
359
	assert d1 >= 20_000_000
360
	time.sleep(20 * time.millisecond)
361
	d2 := time.since(t1)
362
	assert 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
368
fn test_recursive_local_call() {
369
	now_tm := time.now()
370
	assert now_tm.str() == now_tm.local().str()
371
	assert now_tm.local().str() == now_tm.local().local().str()
372
}
373

374
fn test_strftime() {
375
	assert '1980 July 11' == local_time_to_test.strftime('%Y %B %d')
376

377
	assert '1980 July 11' == utc_time_to_test.strftime('%Y %B %d')
378
}
379

380
fn test_add_seconds_to_time() {
381
	now_tm := time.now()
382
	future_tm := now_tm.add_seconds(60)
383
	assert now_tm.unix() < future_tm.unix()
384
}
385

386
fn test_plus_equals_duration() {
387
	mut d := time.second
388
	d += time.second
389
	assert d == 2 * time.second
390
}
391

392
fn test_parse_three_letters_month() {
393
	tm := time.now()
394
	format := 'MMM DD HH:mm:ss YYYY'
395
	tm_s := tm.custom_format(format)
396
	tm_tm := time.parse_format(tm_s, format)!
397
	assert tm_tm.month == tm.month
398
}
399

400
fn test_parse_ordinal_weekday_d() {
401
	format := 'd MMM DD HH:mm:ss YYYY'
402
	dt := '0 Jan 01 00:00:00 1970'
403
	tm := time.parse_format(dt, format)!
404
	tm_s := tm.custom_format(format)
405
	assert tm_s == '4 Jan 01 00:00:00 1970'
406
}
407

408
fn test_parse_ordinal_weekday_c() {
409
	format := 'c MMM DD HH:mm:ss YYYY'
410
	dt := '7 Jan 01 00:00:00 1970'
411
	tm := time.parse_format(dt, format)!
412
	tm_s := tm.custom_format(format)
413
	assert tm_s == '4 Jan 01 00:00:00 1970'
414
}
415

416
fn test_parse_two_letters_weekday() {
417
	format := 'dd MMM DD HH:mm:ss YYYY'
418
	dt := 'Su Jan 01 00:00:00 1970'
419
	tm := time.parse_format(dt, format)!
420
	tm_s := tm.custom_format(format)
421
	assert tm_s == 'Th Jan 01 00:00:00 1970'
422
}
423

424
fn test_parse_three_letters_weekday() {
425
	format := 'ddd MMM DD HH:mm:ss YYYY'
426
	dt := 'Sun Jan 01 00:00:00 1970'
427
	tm := time.parse_format(dt, format)!
428
	tm_s := tm.custom_format(format)
429
	assert tm_s == 'Thu Jan 01 00:00:00 1970'
430
}
431

432
fn test_parse_weekday() {
433
	format := 'dddd MMM DD HH:mm:ss YYYY'
434
	dt := 'Sunday Jan 01 00:00:00 1970'
435
	tm := time.parse_format(dt, format)!
436
	tm_s := tm.custom_format(format)
437
	assert tm_s == 'Thursday Jan 01 00:00:00 1970'
438
}
439

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.