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

evalengine: Implement FROM_DAYS #15058

Merged
merged 8 commits into from
Jan 29, 2024
Prev Previous commit
Next Next commit
Rename mysqlDateFromDayNumber to export
Signed-off-by: Noble Mittal <noblemittal@outlook.com>
  • Loading branch information
beingnoble03 committed Jan 27, 2024
commit d8453079b59f2f179c0446a96f3caf09310f04d9
4 changes: 2 additions & 2 deletions go/mysql/datetime/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,13 +615,13 @@ func (dt *DateTime) addInterval(itv *Interval) bool {
return false
}

dt.Date.year, dt.Date.month, dt.Date.day = mysqlDateFromDayNumber(daynum)
dt.Date.year, dt.Date.month, dt.Date.day = MysqlDateFromDayNumber(daynum)
return true

case itv.unit.HasDayParts():
daynum := mysqlDayNumber(dt.Date.Year(), dt.Date.Month(), dt.Date.Day())
daynum += itv.day
dt.Date.year, dt.Date.month, dt.Date.day = mysqlDateFromDayNumber(daynum)
dt.Date.year, dt.Date.month, dt.Date.day = MysqlDateFromDayNumber(daynum)
return true

case itv.unit.HasMonthParts():
Expand Down
8 changes: 4 additions & 4 deletions go/mysql/datetime/mydate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ package datetime
// day count that traditional datetime systems use (e.g. the daycount
// algorithm in the Go standard library). It is often off by one, possibly
// because of incorrect leap year handling, but the inverse of the algorithm
// in mysqlDateFromDayNumber takes this into account. Hence, the results
// of this function can ONLY be passed to mysqlDateFromDayNumber; using
// in MysqlDateFromDayNumber takes this into account. Hence, the results
// of this function can ONLY be passed to MysqlDateFromDayNumber; using
// a day number with one of Go's datetime APIs will return incorrect results.
// This API should only be used when performing datetime calculations (addition
// and subtraction), so that the results match MySQL's. All other date handling
Expand All @@ -46,15 +46,15 @@ func mysqlDayNumber(year, month, day int) int {
return days + year/4 - leapAdjust
}

// mysqlDateFromDayNumber converts an absolute day number into a date (a year, month, day triplet).
// MysqlDateFromDayNumber converts an absolute day number into a date (a year, month, day triplet).
// This is an algorithm that has been reverse engineered from MySQL;
// the tables used as a reference can be found in `testdata/daynr_to_date.json`.
// See the warning from mysqlDayNumber: the day number used as an argument to
// this function must come from mysqlDayNumber or the results won't be correct.
// This API should only be used when performing datetime calculations (addition
// and subtraction), so that the results match MySQL's. All other date handling
// operations must use our helpers based on Go's standard library.
func mysqlDateFromDayNumber(daynr int) (uint16, uint8, uint8) {
func MysqlDateFromDayNumber(daynr int) (uint16, uint8, uint8) {
if daynr <= 365 || daynr >= 3652500 {
return 0, 0, 0
}
Expand Down
2 changes: 1 addition & 1 deletion go/mysql/datetime/mydate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestDayNumberFields(t *testing.T) {
require.NoError(t, err)

for _, tc := range expected {
y, m, d := mysqlDateFromDayNumber(tc[0])
y, m, d := MysqlDateFromDayNumber(tc[0])
assert.Equal(t, tc[1], int(y))
assert.Equal(t, tc[2], int(m))
assert.Equal(t, tc[3], int(d))
Expand Down