diff --git a/java/java.go b/java/java.go index ca0c9233..8230b911 100644 --- a/java/java.go +++ b/java/java.go @@ -23,6 +23,7 @@ import ( "time" hessian2_exception "github.com/kitex-contrib/codec-dubbo/pkg/hessian2/exception" + _ "github.com/kitex-contrib/codec-dubbo/pkg/hessian2/sql" ) type Object = interface{} diff --git a/pkg/hessian2/sql/timestamp.go b/pkg/hessian2/sql/timestamp.go new file mode 100644 index 00000000..4244e59e --- /dev/null +++ b/pkg/hessian2/sql/timestamp.go @@ -0,0 +1,72 @@ +/* + * Copyright 2023 CloudWeGo Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 sql + +import ( + "time" + + "github.com/apache/dubbo-go-hessian2" +) + +func init() { + hessian.RegisterPOJO(&Timestamp{}) + hessian.SetJavaSqlTimeSerialize(&Timestamp{}) +} + +type Timestamp struct { + time.Time +} + +func (d *Timestamp) GetTime() time.Time { + return d.Time +} + +func (d *Timestamp) SetTime(time time.Time) { + d.Time = time +} + +func (Timestamp) JavaClassName() string { + return "java.sql.Timestamp" +} + +func (d *Timestamp) ValueOf(dateStr string) error { + // todo(DMwangnima): change layout + date, err := time.Parse("2006-01-02", dateStr) + if err != nil { + return err + } + d.Time = date + return nil +} + +// nolint +func (d *Timestamp) Year() int { + return d.Time.Year() +} + +// nolint +func (d *Timestamp) Month() time.Month { + return d.Time.Month() +} + +// nolint +func (d *Timestamp) Day() int { + return d.Time.Day() +}