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)文章
Go語言中使用Swagger 生成 API 文檔及常見問題解決
Swagger 是一個規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化 RESTful風(fēng)格的Web服務(wù),本文就來詳細(xì)的介紹一下如何實現(xiàn)Go語言中使用Swagger 生成 API 文檔,感興趣的可以了解一下2026-01-01
Go語言使用goimports格式化代碼的完整教學(xué)
goimports?是 Go 語言中一個非常常用的工具,用于自動格式化 Go 源代碼并管理 import 聲明,下面小編就和大家詳細(xì)介紹一下如何使用goimports進(jìn)行格式化代碼吧2026-01-01
解決golang sync.Wait()不執(zhí)行的問題
這篇文章主要介紹了解決golang sync.Wait()不執(zhí)行的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
Go源碼字符串規(guī)范檢查lint工具strchecker使用詳解
這篇文章主要為大家介紹了Go源碼字符串規(guī)范檢查lint工具strchecker使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06

