最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Golang實現(xiàn)自定義時間結(jié)構(gòu)體并支持Json&Gorm

 更新時間:2024年03月13日 11:50:50   作者:高山丿流水  
因為時區(qū)等問題,很多項目需要自定義時區(qū)和時間格式,所以這篇文章主要為大家介紹了Golang如何實現(xiàn)自定義時間結(jié)構(gòu)體并支持Json&Gorm,希望對大家有所幫助

前言

因為時區(qū)等問題,很多項目需要自定義時區(qū)和時間格式。還有需要自定義輸出顯示。

方案

代碼

package timetool

import (
	"database/sql/driver"
	"fmt"
	"time"
)

const TimeFormat = "2006-01-02 15:04:05"
const FormatISOTime = "2006-01-02T15:04:05Z"
const DayFormat = "20060102"
const SecondDateFormat = "20060102150405"
const FYear = "2006"     //年
const FMonth = "01"      //月
const FMonthNoZero = "1" //月,不帶先導(dǎo)零
const FDay = "02"        //日
const FDayNoZero = "2"   //日,不帶先導(dǎo)零
const FHour = "15"       // 小時,24小時格式
const FMinute = "04"     // 分鐘
const FSecond = "05"     // 秒

// 1. 創(chuàng)建 time.Time 類型的副本 XTime;
type MyTime struct {
	time.Time
}

// 2. 為 MyTime 重寫 MarshaJSON 方法,在此方法中實現(xiàn)自定義格式的轉(zhuǎn)換;
func (t MyTime) MarshalJSON() ([]byte, error) {

	loc := time.FixedZone(pkg.TimeLocal.TimeZone, pkg.TimeLocal.TimeOffset)
	output := fmt.Sprintf("\"%s\"", t.In(loc).Format(TimeFormat))
	return []byte(output), nil
}

// 3. 為 MyTime 實現(xiàn) Value 方法,寫入數(shù)據(jù)庫時會調(diào)用該方法將自定義時間類型轉(zhuǎn)換并寫入數(shù)據(jù)庫;
func (t MyTime) Value() (driver.Value, error) {
	var zeroTime time.Time
	if t.Time.UnixNano() == zeroTime.UnixNano() {
		return nil, nil
	}
	return t.Time, nil
}

// 4. 為 MyTime 實現(xiàn) Scan 方法,讀取數(shù)據(jù)庫時會調(diào)用該方法將時間數(shù)據(jù)轉(zhuǎn)換成自定義時間類型;
func (t *MyTime) Scan(v interface{}) error {
	value, ok := v.(time.Time)
	if ok {
		*t = MyTime{Time: value}
		return nil
	}
	return fmt.Errorf("can not convert %v to timestamp", v)
}

func (t *MyTime) String() string {
	loc := time.FixedZone(pkg.TimeLocal.TimeZone, pkg.TimeLocal.TimeOffset)
	return t.In(loc).String()
}

func (t *MyTime) GetTime() time.Time {
	loc := time.FixedZone(pkg.TimeLocal.TimeZone, pkg.TimeLocal.TimeOffset)
	return t.In(loc)
}

func (t *MyTime) UnmarshalJSON(data []byte) (err error) {
	// 空值不進(jìn)行解析
	if len(data) == 2 {
		return
	}

	if string(data) == "null" {
		return
	}

	var now time.Time
	// 指定解析的格式
	if now, err = time.ParseInLocation(TimeFormat, string(data), time.Local); err == nil {
		*t = MyTime{now}
		return
	}
	// 指定解析的格式
	if now, err = time.ParseInLocation('"'+TimeFormat+'"', string(data), time.Local); err == nil {
		*t = MyTime{now}
		return
	}
	//解析默認(rèn)格式
	if now, err = time.ParseInLocation('"'+time.RFC3339+'"', string(data), time.Local); err == nil {
		*t = MyTime{now}
		return
	}

	return
}

func MyTimeNow() MyTime {
	return MyTime{Time: time.Now()}
}

func NewMyTime(ts ...int64) *MyTime {
	if len(ts) == 0 {
		return &MyTime{Time: time.Now()}
	}
	loc := time.FixedZone(pkg.TimeLocal.TimeZone, pkg.TimeLocal.TimeOffset)
	return &MyTime{
		Time: time.Unix(ts[0], 0).In(loc),
	}
}

注意:

JSON 關(guān)鍵點:

  • 實現(xiàn) MarshalJSON 方法
  • 實現(xiàn) UnmarshalJSON 方法
  • 實現(xiàn) Value 方法

Gorm關(guān)鍵點:

  • 實現(xiàn) Scan 方法
  • 實現(xiàn) String 方法

到此這篇關(guān)于Golang實現(xiàn)自定義時間結(jié)構(gòu)體并支持Json&Gorm的文章就介紹到這了,更多相關(guān)Go自定義時間結(jié)構(gòu)體內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

涟水县| 黄陵县| 四会市| 都安| 石首市| 扶余县| 崇仁县| 清河县| 枝江市| 阿克| 保山市| 晋中市| 祁东县| 盐池县| 巴东县| 东乡县| 上林县| 民丰县| 上虞市| 五大连池市| 襄樊市| 晋宁县| 平顺县| 新河县| 蓝田县| 黑河市| 东乌珠穆沁旗| 威远县| 平原县| 饶河县| 鄂温| 团风县| 九龙县| 长泰县| 论坛| 济宁市| 南华县| 万年县| 三河市| 柘城县| 阳信县|