feat(user):完成用户部分所有接口的迁移。

This commit is contained in:
徐涛
2023-06-02 15:51:08 +08:00
parent cd723e98e3
commit 097e25f070
9 changed files with 447 additions and 68 deletions

View File

@@ -6,6 +6,8 @@ import (
"encoding/json"
"fmt"
"time"
et "electricity_bill_calc/tools/time"
)
type Date struct {
@@ -13,32 +15,24 @@ type Date struct {
}
func NewDate(t time.Time) Date {
loc, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
panic(err)
}
t = t.In(loc)
t = t.In(et.Loc)
return Date{
Time: time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, loc),
Time: time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, et.Loc),
}
}
func SpecificDate(year int, month time.Month, date int) Date {
return NewDate(et.Time(year, month, date, 0, 0, 0, 0))
}
func NewEmptyDate() Date {
loc, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
panic(err)
}
return Date{
Time: time.Time{}.In(loc),
Time: time.Time{}.In(et.Loc),
}
}
func ParseDate(t string) (Date, error) {
loc, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
return NewEmptyDate(), fmt.Errorf("unable to load time zone, %w", err)
}
d, err := time.ParseInLocation("2006-01-02", t, loc)
d, err := time.ParseInLocation("2006-01-02", t, et.Loc)
if err != nil {
return NewEmptyDate(), fmt.Errorf("unable to parse given time, %w", err)
}
@@ -62,30 +56,22 @@ func (d Date) ToString() string {
var _ driver.Valuer = (*Date)(nil)
func (d Date) Value() (driver.Value, error) {
loc, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
panic(err)
}
return d.In(loc).Format("2006-01-02"), nil
return d.In(et.Loc).Format("2006-01-02"), nil
}
var _ sql.Scanner = (*Date)(nil)
// Scan scans the time parsing it if necessary using timeFormat.
func (d *Date) Scan(src interface{}) (err error) {
loc, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
panic(err)
}
switch src := src.(type) {
case time.Time:
*d = NewDate(src)
return nil
case string:
d.Time, err = time.ParseInLocation("2006-01-02", src, loc)
d.Time, err = time.ParseInLocation("2006-01-02", src, et.Loc)
return err
case []byte:
d.Time, err = time.ParseInLocation("2006-01-02", string(src), loc)
d.Time, err = time.ParseInLocation("2006-01-02", string(src), et.Loc)
return err
case nil:
d.Time = time.Time{}
@@ -104,15 +90,11 @@ func (d Date) MarshalJSON() ([]byte, error) {
var _ json.Unmarshaler = (*Date)(nil)
func (d *Date) UnmarshalJSON(raw []byte) error {
loc, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
return fmt.Errorf("unable to load time zone, %w", err)
}
var s string
err = json.Unmarshal(raw, &s)
err := json.Unmarshal(raw, &s)
if err != nil {
return fmt.Errorf("unable to unmarshal value, %w", err)
}
d.Time, err = time.ParseInLocation("2006-01-02", s, loc)
d.Time, err = time.ParseInLocation("2006-01-02", s, et.Loc)
return err
}