1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

time: let time.parse_rfc3339('2015-01-06T15:47:32.080254511Z') succeed (dockerd timestamps, Go's RFC3339Nano).

This commit is contained in:
Delyan Angelov
2022-05-08 13:56:50 +03:00
parent a0a3499bdc
commit 084f2867b6
2 changed files with 58 additions and 13 deletions

View File

@ -193,24 +193,46 @@ fn parse_iso8601_time(s string) ?(int, int, int, int, i64, bool) {
hour_ := 0
minute_ := 0
second_ := 0
microsecond_ := 0
mut microsecond_ := 0
mut nanosecond_ := 0
plus_min_z := `a`
offset_hour := 0
offset_minute := 0
mut count := unsafe {
C.sscanf(&char(s.str), c'%2d:%2d:%2d.%6d%c%2d:%2d', &hour_, &minute_, &second_,
&microsecond_, &char(&plus_min_z), &offset_hour, &offset_minute)
mut count := 0
count = unsafe {
C.sscanf(&char(s.str), c'%2d:%2d:%2d.%9d%c', &hour_, &minute_, &second_, &nanosecond_,
&char(&plus_min_z))
}
// Missread microsecond ([Sec Hour Minute].len == 3 < 4)
if count < 4 {
count = unsafe {
C.sscanf(&char(s.str), c'%2d:%2d:%2d%c%2d:%2d', &hour_, &minute_, &second_,
&char(&plus_min_z), &offset_hour, &offset_minute)
if count == 5 && plus_min_z == `Z` {
// normalise the nanoseconds:
mut ndigits := 0
if mut pos := s.index('.') {
pos++
for ; pos < s.len && s[pos].is_digit(); pos++ {
ndigits++
}
}
for ndigits < 9 {
nanosecond_ *= 10
ndigits++
}
microsecond_ = nanosecond_ / 1000
} else {
count = unsafe {
C.sscanf(&char(s.str), c'%2d:%2d:%2d.%6d%c%2d:%2d', &hour_, &minute_, &second_,
&microsecond_, &char(&plus_min_z), &offset_hour, &offset_minute)
}
// Missread microsecond ([Sec Hour Minute].len == 3 < 4)
if count < 4 {
count = unsafe {
C.sscanf(&char(s.str), c'%2d:%2d:%2d%c%2d:%2d', &hour_, &minute_, &second_,
&char(&plus_min_z), &offset_hour, &offset_minute)
}
count++ // Increment count because skipped microsecond
}
if count < 4 {
return error_invalid_time(10)
}
count++ // Increment count because skipped microsecond
}
if count < 4 {
return error_invalid_time(10)
}
is_local_time := plus_min_z == `a` && count == 4
is_utc := plus_min_z == `Z` && count == 5