Skip to content

Commit 47db448

Browse files
Zjmainstayhailaz
andauthored
fix(os/gtime): fix gtime time string handle logic (#4409)
1、gtime的StrToTime方法优化对时间字段格式兼容处理,并且针对时间越界抛出异常。 2、移除部分无用代码: <img width="697" height="282" alt="image" src="https://github.com/user-attachments/assets/92a88140-37c0-4ee1-aef7-c6418e9edd06" /> Fixes #4394 --------- Signed-off-by: Zjmainstay <[email protected]> Co-authored-by: hailaz <[email protected]>
1 parent a39498f commit 47db448

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

os/gtime/gtime.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,13 @@ func StrToTime(str string, format ...string) (*Time, error) {
222222
} else if match = timeRegex2.FindStringSubmatch(str); len(match) > 0 && match[1] != "" {
223223
year, month, day = parseDateStr(match[1])
224224
} else if match = timeRegex3.FindStringSubmatch(str); len(match) > 0 && match[1] != "" {
225-
s := strings.ReplaceAll(match[2], ":", "")
226-
if len(s) < 6 {
227-
s += strings.Repeat("0", 6-len(s))
228-
}
229225
hour, _ = strconv.Atoi(match[1])
230226
min, _ = strconv.Atoi(match[2])
231227
sec, _ = strconv.Atoi(match[3])
232228
nsec, _ = strconv.Atoi(match[4])
229+
if hour > 23 || min > 59 || sec > 59 {
230+
return nil, gerror.NewCodef(gcode.CodeInvalidParameter, `invalid time string "%s"`, str)
231+
}
233232
for i := 0; i < 9-len(match[4]); i++ {
234233
nsec *= 10
235234
}
@@ -240,13 +239,19 @@ func StrToTime(str string, format ...string) (*Time, error) {
240239

241240
// Time
242241
if len(match[2]) > 0 {
243-
s := strings.ReplaceAll(match[2], ":", "")
244-
if len(s) < 6 {
245-
s += strings.Repeat("0", 6-len(s))
242+
parts := strings.Split(match[2], ":")
243+
if len(parts) >= 1 && parts[0] != "" {
244+
hour, _ = strconv.Atoi(parts[0])
245+
}
246+
if len(parts) >= 2 && parts[1] != "" {
247+
min, _ = strconv.Atoi(parts[1])
248+
}
249+
if len(parts) >= 3 && parts[2] != "" {
250+
sec, _ = strconv.Atoi(parts[2])
251+
}
252+
if hour > 23 || min > 59 || sec > 59 {
253+
return nil, gerror.NewCodef(gcode.CodeInvalidParameter, `invalid time string "%s"`, str)
246254
}
247-
hour, _ = strconv.Atoi(s[0:2])
248-
min, _ = strconv.Atoi(s[2:4])
249-
sec, _ = strconv.Atoi(s[4:6])
250255
}
251256
// Nanoseconds, check and perform bits filling
252257
if len(match[3]) > 0 {

os/gtime/gtime_z_unit_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,46 @@ func Test_StrToTime(t *testing.T) {
183183
}
184184
}
185185

186+
// test special time string
187+
var testSpecialDateTimes = []string{
188+
"2006-01-02 8:04:05",
189+
"2006-01-02 8:4:05",
190+
"2006-01-02 8:4:5",
191+
"2006-01-02 8:04:05.000",
192+
"2006/01/02 8:4:5",
193+
"2006.01.02 8:4:5.000",
194+
"2006.01.02 - 8:4:5",
195+
"2006.01.02 8:4:5 +0800 CST",
196+
"2006-01-02T5:5:5+05:01",
197+
"2006-01-01T19:3:5-05:01",
198+
"2006-01-02T8:4:5",
199+
"02-jan-2006 8:4:5",
200+
"02/jan/2006 8:4:5",
201+
"02.jan.2006 8:4:5",
202+
"02.jan.2006:8:4:5",
203+
}
204+
for _, item := range testSpecialDateTimes {
205+
timeTemp, err := gtime.StrToTime(item)
206+
t.AssertNil(err)
207+
t.Assert(timeTemp.Time.Local().Format("2006-01-02 15:04:05"), "2006-01-02 08:04:05")
208+
}
209+
210+
// test error time string
211+
var testErrorDateTimes = []string{
212+
"2006-01-02 28:4:5",
213+
"2006-01-02 8:60:5",
214+
"2006-01-02 8:4:60",
215+
"28:20:20",
216+
"8:60:20",
217+
"8:20:60",
218+
}
219+
for _, item := range testErrorDateTimes {
220+
_, err := gtime.StrToTime(item)
221+
if err == nil {
222+
t.Error("test fail")
223+
}
224+
}
225+
186226
// test err
187227
_, err := gtime.StrToTime("2006-01-02 15:04:05", "aabbccdd")
188228
if err == nil {

0 commit comments

Comments
 (0)