47 lines
1.6 KiB
Go
47 lines
1.6 KiB
Go
// Copyright 2025 Dolthub, Inc.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package functions
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/dolthub/go-mysql-server/sql"
|
|
|
|
"github.com/dolthub/doltgresql/server/functions/framework"
|
|
pgtypes "github.com/dolthub/doltgresql/server/types"
|
|
)
|
|
|
|
// initToDate registers the functions to the catalog.
|
|
func initToDate() {
|
|
framework.RegisterFunction(to_date_text_text)
|
|
}
|
|
|
|
// to_date_text_text represents the PostgreSQL function of the same name, taking text input and format pattern.
|
|
var to_date_text_text = framework.Function2{
|
|
Name: "to_date",
|
|
Return: pgtypes.Date,
|
|
Parameters: [2]*pgtypes.DoltgresType{pgtypes.Text, pgtypes.Text},
|
|
Strict: true,
|
|
Callable: func(ctx *sql.Context, _ [3]*pgtypes.DoltgresType, val1, val2 any) (any, error) {
|
|
input := val1.(string)
|
|
format := val2.(string)
|
|
|
|
// Parse the date using PostgreSQL format patterns
|
|
t, err := getDateTimeFromFormat(ctx, input, format)
|
|
// We return a version of the time but with the timezone completely stripped since they do not use timezones
|
|
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.UTC), err
|
|
},
|
|
}
|