Skip to content

Commit

Permalink
Parse julian day of year in PARSE_DATE and PARSE_DATETIME (#35)
Browse files Browse the repository at this point in the history
* Basic day of year parsing works

* Handle and test leap year

* Test PARSE_DATETIME

* Test invalid values
  • Loading branch information
recidinick authored Mar 28, 2024
1 parent 2425672 commit ed775cb
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
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 := int(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(),
int(stubDate.Day()),
int(t.Hour()),
int(t.Minute()),
int(t.Second()),
int(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 @@ -4108,6 +4108,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 @@ -4233,6 +4303,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

0 comments on commit ed775cb

Please sign in to comment.