Skip to content

Commit

Permalink
improve time functions to accept fewer element arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Aug 21, 2024
1 parent 21caffb commit 7f0828d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 34 deletions.
14 changes: 14 additions & 0 deletions cli/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5450,6 +5450,8 @@
[2017,6,14,2,40,0,5,194]
[2019,8,7,21,2,3.4560000889999998,6,249]
[2020,8,13,12,26,40.111000061,0,256]
[2024,7,20]
[2025]
expected: |
1500000000
"2017-07-14T02:40:00Z"
Expand All @@ -5469,6 +5471,18 @@
"2020-09-13T12:26:40-0700"
"2020-09-13T05:26:40-0700"
"2020-09-13T12:26:40Z"
1724112000
"2024-08-20T00:00:00Z"
"2024-08-20T00:00:00Z"
"2024-08-20T00:00:00-0700"
"2024-08-19T17:00:00-0700"
"2024-08-20T00:00:00Z"
1735603200
"2024-12-31T00:00:00Z"
"2024-12-31T00:00:00Z"
"2024-12-31T00:00:00-0700"
"2024-12-30T17:00:00-0700"
"2024-12-31T00:00:00Z"
- name: strptime, fromdate functions
args:
Expand Down
57 changes: 23 additions & 34 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -1936,41 +1936,30 @@ func funcStrptime(v, x any) any {

func arrayToTime(a []any, loc *time.Location) (time.Time, error) {
var t time.Time
if len(a) != 8 {
return t, &timeArrayError{}
}
var y, m, d, h, min, sec, nsec int
var ok bool
if y, ok = toInt(a[0]); !ok {
return t, &timeArrayError{}
}
if m, ok = toInt(a[1]); ok {
m++
} else {
return t, &timeArrayError{}
}
if d, ok = toInt(a[2]); !ok {
return t, &timeArrayError{}
}
if h, ok = toInt(a[3]); !ok {
return t, &timeArrayError{}
}
if min, ok = toInt(a[4]); !ok {
return t, &timeArrayError{}
}
if x, ok := toFloat(a[5]); ok {
sec = int(x)
nsec = int((x - math.Floor(x)) * 1e9)
} else {
return t, &timeArrayError{}
}
if _, ok = toFloat(a[6]); !ok {
return t, &timeArrayError{}
}
if _, ok = toFloat(a[7]); !ok {
return t, &timeArrayError{}
var year, month, day, hour, minute,
second, nanosecond, weekday, yearday int
for i, p := range []*int{
&year, &month, &day, &hour, &minute,
&second, &weekday, &yearday,
} {
if i >= len(a) {
break
}
if i == 5 {
if v, ok := toFloat(a[i]); ok {
*p = int(v)
nanosecond = int((v - math.Floor(v)) * 1e9)
} else {
return t, &timeArrayError{}
}
} else if v, ok := toInt(a[i]); ok {
*p = v
} else {
return t, &timeArrayError{}
}
}
return time.Date(y, time.Month(m), d, h, min, sec, nsec, loc), nil
return time.Date(year, time.Month(month+1), day,
hour, minute, second, nanosecond, loc), nil
}

func funcNow(any) any {
Expand Down

0 comments on commit 7f0828d

Please sign in to comment.