diff --git a/internal/function_time_parser.go b/internal/function_time_parser.go index 76781f0..e99ca6f 100644 --- a/internal/function_time_parser.go +++ b/internal/function_time_parser.go @@ -873,22 +873,22 @@ func largeAMPMParser(text []rune, t *time.Time) (int, error) { if len(text) < 2 { return progress, fmt.Errorf("cannot parse am/pm format: remaining text [%s]", string(text)) } - progress += 2 - toParse := string(text[:progress]) + toParse := string(text[:2]) timeOfDay := strings.ToLower(toParse) if timeOfDay != "am" && timeOfDay != "pm" { return 0, fmt.Errorf("cannot parse am/pm format: [%s] is not am/pm", string(text)) } + progress += 2 return progress, nil } func ampmPostProcessor(text []rune, t *time.Time) { morning := strings.ToLower(string(text)) == "am" hour := t.Hour() - if hour == 12 && morning { - hour = 0 + if morning { + hour = hour % 12 } - if !morning { + if !morning && hour < 12 { hour += 12 } *t = time.Date( diff --git a/query_test.go b/query_test.go index 38f49b8..55ffc6e 100644 --- a/query_test.go +++ b/query_test.go @@ -4629,11 +4629,11 @@ SELECT date, EXTRACT(ISOYEAR FROM date), EXTRACT(YEAR FROM date), EXTRACT(MONTH }, { name: "parse timestamp with %p", - query: `SELECT PARSE_TIMESTAMP("%I%p", "9am"), PARSE_TIMESTAMP("%I%p", "12am"), PARSE_TIMESTAMP("%l%p", " 2am"), PARSE_TIMESTAMP("%I%p", "10PM");`, + query: `SELECT PARSE_TIMESTAMP("%I%p", "9am"), PARSE_TIMESTAMP("%I%p", "12am"), PARSE_TIMESTAMP("%l%p", " 12pm"), PARSE_TIMESTAMP("%I%p", "10PM");`, expectedRows: [][]interface{}{{ createTimestampFormatFromString("1970-01-01 09:00:00+00"), createTimestampFormatFromString("1970-01-01 00:00:00+00"), - createTimestampFormatFromString("1970-01-01 02:00:00+00"), + createTimestampFormatFromString("1970-01-01 12:00:00+00"), createTimestampFormatFromString("1970-01-01 22:00:00+00"), }}, },