Skip to content

Commit

Permalink
[Date Parser] Fix %p 12pm case (#24)
Browse files Browse the repository at this point in the history
* fix 12pm case

* lint
  • Loading branch information
ohaibbq committed Feb 27, 2024
1 parent 3c54c7f commit 924cbd2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
10 changes: 5 additions & 5 deletions internal/function_time_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}},
},
Expand Down

0 comments on commit 924cbd2

Please sign in to comment.