Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement dayOfYearParser #197

Merged
merged 4 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion internal/function_time_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,24 @@ func hour12Formatter(t *time.Time) ([]rune, error) {
}

func dayOfYearParser(text []rune, t *time.Time) (int, error) {
return 0, fmt.Errorf("unimplemented day of year matcher")
progress, d, err := parseDigitRespectingOptionalPlaces(text, 1, 366)
if err != nil {
return 0, fmt.Errorf("could not parse day of year number: %s", err)
}
dayOfYear := int(d) - 1
year := t.Year()
stubDate := time.Date(year, time.January, 1, 0, 0, 0, 0, time.UTC).AddDate(0, 0, dayOfYear)
*t = time.Date(
year,
stubDate.Month(),
stubDate.Day(),
t.Hour(),
t.Minute(),
t.Second(),
t.Nanosecond(),
t.Location(),
)
return progress, nil
}

func dayOfYearFormatter(t *time.Time) ([]rune, error) {
Expand Down
80 changes: 80 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3981,6 +3981,76 @@ WITH example AS (
{"1970-03-01"},
},
},
{
name: "base date is epoch julian",
query: `SELECT PARSE_DATE("%j", "001")`,
expectedRows: [][]interface{}{
{"1970-01-01"},
},
},
{
name: "base datetime is epoch julian",
query: `SELECT PARSE_DATETIME("%j", "001")`,
expectedRows: [][]interface{}{
{"1970-01-01T00:00:00"},
},
},
{
name: "base date is epoch julian different day",
query: `SELECT PARSE_DATE("%j", "002")`,
expectedRows: [][]interface{}{
{"1970-01-02"},
},
},
{
name: "parse date with two digit year and julian day",
query: `SELECT PARSE_DATE("%y%j", "70002")`,
expectedRows: [][]interface{}{
{"1970-01-02"},
},
},
{
name: "parse date with two digit year before 2000 and julian day",
query: `SELECT PARSE_DATE("%y%j", "95033")`,
expectedRows: [][]interface{}{
{"1995-02-02"},
},
},
{
name: "parse datetime with two digit year before 2000 and julian day",
query: `SELECT PARSE_DATETIME("%y%j%H%M%S", "95033101010")`,
expectedRows: [][]interface{}{
{"1995-02-02T10:10:10"},
},
},
{
name: "parse date with two digit year after 2000 and julian day",
query: `SELECT PARSE_DATE("%y%j", "22120")`,
expectedRows: [][]interface{}{
{"2022-04-30"},
},
},
{
name: "parse datetime with two digit year after 2000 and julian day",
query: `SELECT PARSE_DATETIME("%y%j-%H:%M:%S", "22120-10:10:10")`,
expectedRows: [][]interface{}{
{"2022-04-30T10:10:10"},
},
},
{
name: "parse date with two digit year after 2000 and julian day leap year",
query: `SELECT PARSE_DATE("%y%j", "24120")`,
expectedRows: [][]interface{}{
{"2024-04-29"},
},
},
{
name: "parse datetime with two digit year after 2000 and julian day leap year",
query: `SELECT PARSE_DATETIME("%y%j %H:%M", "24120 02:04")`,
expectedRows: [][]interface{}{
{"2024-04-29T02:04:00"},
},
},
{
name: "extract date",
query: `
Expand Down Expand Up @@ -4100,6 +4170,16 @@ SELECT date, EXTRACT(ISOYEAR FROM date), EXTRACT(YEAR FROM date), EXTRACT(MONTH
query: `SELECT PARSE_DATE("%d", "0")`,
expectedErr: "error parsing [0] with format [%d]: could not parse day number: part [0] is less than minimum value [1]",
},
{
name: "parse date exceeding day of year maximum",
query: `SELECT PARSE_DATE("%j", "367")`,
expectedErr: "error parsing [367] with format [%j]: could not parse day of year number: part [367] is greater than maximum value [366]",
},
{
name: "parse date beneath day of year minimum",
query: `SELECT PARSE_DATE("%j", "0")`,
expectedErr: "error parsing [0] with format [%j]: could not parse day of year number: part [0] is less than minimum value [1]",
},
{
name: "parse date with single-digit month %m",
query: `SELECT PARSE_DATE("%m", "03"), PARSE_DATE("%m", "3"), PARSE_DATE("%m%Y", "032024")`,
Expand Down
Loading