time: fix timezone problems of parse_iso8601 (#7272)

pull/7283/head
zakuro 2020-12-12 17:19:03 +09:00 committed by GitHub
parent 0aa9f5a007
commit 5fec0d785a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 50 deletions

View File

@ -95,6 +95,14 @@ jobs:
run: ./v -o v2 cmd/v && ./v2 -o v3 cmd/v && ./v3 -o v4 cmd/v run: ./v -o v2 cmd/v && ./v2 -o v3 cmd/v && ./v3 -o v4 cmd/v
- name: Fixed tests - name: Fixed tests
run: ./v -silent test-fixed run: ./v -silent test-fixed
- name: Test time functions in a timezone UTC-12
run: TZ=Etc/GMT+12 ./v test vlib/time/
- name: Test time functions in a timezone UTC-3
run: TZ=Etc/GMT+3 ./v test vlib/time/
- name: Test time functions in a timezone UTC+3
run: TZ=Etc/GMT-3 ./v test vlib/time/
- name: Test time functions in a timezone UTC+12
run: TZ=Etc/GMT-12 ./v test vlib/time/
- name: Test building v tools - name: Test building v tools
run: ./v -silent build-tools run: ./v -silent build-tools
- name: v doctor - name: v doctor

View File

@ -91,11 +91,8 @@ pub fn parse_iso8601(s string) ?Time {
second: second second: second
microsecond: mic_second microsecond: mic_second
}) })
if is_utc {
return t
}
if is_local_time { if is_local_time {
return to_local_time(t) return t // Time already local time
} }
mut unix_time := t.unix mut unix_time := t.unix
mut unix_offset := int(0) mut unix_offset := int(0)

View File

@ -1,4 +1,5 @@
import time import time
import math
fn test_parse() { fn test_parse() {
s := '2018-01-27 12:48:34' s := '2018-01-27 12:48:34'
@ -48,61 +49,60 @@ fn test_parse_rfc2822_invalid() {
assert false assert false
} }
fn test_iso8601_parse_utc() { fn test_parse_iso8601() {
// Because there is a small difference between time.now() - time.utc and actual offset,
// round to the nearest hour.
offset := time.Duration(i64(math.round((time.now() - time.utc()).hours())) * time.hour)
formats := [ formats := [
'2020-06-05T15:38:06.015959Z',
'2020-06-05T15:38:06Z', '2020-06-05T15:38:06Z',
'2020-06-05T15:38:06.015959Z',
'2020-06-05T15:38:06.015959+00:00',
'2020-06-05T15:38:06.015959+02:00',
'2020-06-05T15:38:06.015959-02:00',
] ]
times := [ times := [
[2020, 6, 5, 15, 38, 6, 15959],
[2020, 6, 5, 15, 38, 6, 0], [2020, 6, 5, 15, 38, 6, 0],
[2020, 6, 5, 15, 38, 6, 15959],
[2020, 6, 5, 15, 38, 6, 15959],
[2020, 6, 5, 13, 38, 6, 15959],
[2020, 6, 5, 17, 38, 6, 15959],
] ]
for i, format in formats { for i, format in formats {
t := time.parse_iso8601(format) or { panic(err) } t := time.parse_iso8601(format) or {
tt := times[i] assert false
assert t.year == tt[0] continue
assert t.month == tt[1] }
assert t.day == tt[2] data := times[i]
assert t.hour == tt[3] t2 := time.new_time(
assert t.minute == tt[4] year: data[0]
assert t.second == tt[5] month: data[1]
assert t.microsecond == tt[6] day: data[2]
hour: data[3]
minute: data[4]
second: data[5]
microsecond: data[6]
).add(offset)
assert t.year == t2.year
assert t.month == t2.month
assert t.day == t2.day
assert t.hour == t2.hour
assert t.minute == t2.minute
assert t.second == t2.second
assert t.microsecond == t2.microsecond
} }
} }
fn test_iso8601_parse_local() { fn test_parse_iso8601_local() {
format_utc := '2020-06-05T15:38:06.015959' format := '2020-06-05T15:38:06.015959'
t_utc := time.parse_iso8601(format_utc) or { t := time.parse_iso8601(format) or {
panic(err) assert false
return
} }
assert t_utc.year == 2020 assert t.year == 2020
assert t_utc.month == 6 assert t.month == 6
assert t_utc.day == 5 assert t.day == 5
} assert t.hour == 15
assert t.minute == 38
fn test_iso8601_parse_utc_diff() { assert t.second == 6
format_utc := '2020-06-05T15:38:06.015959+00:00' assert t.microsecond == 15959
format_cest := '2020-06-05T15:38:06.015959+02:00'
t_utc := time.parse_iso8601(format_utc) or {
panic(err)
}
t_cest := time.parse_iso8601(format_cest) or {
panic(err)
}
assert t_utc.year == 2020
assert t_cest.year == 2020
assert t_utc.month == 6
assert t_cest.month == 6
assert t_utc.day == 5
assert t_cest.day == 5
// if it was formatted in utc it should be
// two hours before if it was formatted in
// cest time
assert t_utc.hour == (t_cest.hour + 2)
assert t_utc.minute == 38
assert t_cest.minute == 38
assert t_utc.second == 6
assert t_cest.second == 6
assert t_utc.microsecond == 15959
assert t_cest.microsecond == 15959
} }