GoFrame框架之gtime時間模塊,支持自定義時間格式化語法

文章來源:http://gf.johng.cn/494387php

通用時間管理模塊,封裝了經常使用的時間/日期相關的方法。並支持自定義的日期格式化語法,格式化語法相似PHP的date語法。html

使用方式:git

import "gitee.com/johng/gf/g/os/gtime"

方法列表: godoc.org/github.com/johng-cn/gf/g/os/gtimegithub

時間格式

gtime模塊最大的特色是支持自定義的時間格式,參考PHP日期時間格式語法,如下是支持的時間格式語法列表:
圖片描述工具

時間對象

方法列表:spa

type Time
    func New(t ...time.Time) *Time
    func NewFromStr(str string) *Time
    func NewFromStrFormat(str string, format string) *Time
    func NewFromStrLayout(str string, layout string) *Time
    func NewFromTime(t time.Time) *Time
    func NewFromTimeStamp(timestamp int64) *Time
    func Now() *Time
    func (t *Time) Add(d time.Duration) *Time
    func (t *Time) AddDate(years int, months int, days int) *Time
    func (t *Time) Clone() *Time
    func (t *Time) Format(format string) string
    func (t *Time) Layout(layout string) string
    func (t *Time) Local() *Time
    func (t *Time) Microsecond() int64
    func (t *Time) Millisecond() int64
    func (t *Time) Nanosecond() int64
    func (t *Time) Round(d time.Duration) *Time
    func (t *Time) Second() int64
    func (t *Time) String() string
    func (t *Time) ToLocation(location *time.Location) *Time
    func (t *Time) ToTime() time.Time
    func (t *Time) Truncate(d time.Duration) *Time
    func (t *Time) UTC() *Time

建立gtime.Time對象能夠經過標準庫time.Time對象、Unix時間戳、時間字符串(如:2018-07-18 12:01:00)、自定義時間字符串(須要給定格式,支持自定義格式及標準庫格式)。.net

示例1,自定義時間格式化語法

package main

import (
    "fmt"
    "gitee.com/johng/gf/g/os/gtime"
)

func main() {
    formats := []string{
        "Y-m-d H:i:s.u",
        "D M d H:i:s T O Y",
        "\\T\\i\\m\\e \\i\\s: h:i:s a",
        "2006-01-02T15:04:05.000000000Z07:00",
    }
    t := gtime.Now()
    for _, f := range formats {
        fmt.Println(t.Format(f))
    }
}

在該示例中,咱們給定了四種format格式,並將當前時間用這四種格式轉換後打印出來。執行後,輸出結果以下:code

2018-07-22 11:17:13.797
Sun Jul 22 11:17:13 CST +0800 2018
Time is: 11:17:13 am
2006-01-02CST15:04:05.000000000Z07:00

能夠看到,這個示例演示了幾個須要注意的地方:orm

  1. 若是使用的字母與格式化字符衝突時,可使用\符號轉移該字符,這樣時間格式解析器會認爲該字符不是格式化字符,而是普通字母。所以這裏的第三個字符串示例輸出爲:Time is: 11:17:13 am
  2. 使用Format方法接收的是自定義的時間格式化語法(如:Y-m-d H:i:s),而不是標準庫的事件格式語法(如:2006-01-02 15:04:05),且兩種語法不能混用,所以在這裏的第四個字符串示例中原樣輸出參數值;

示例2,標準庫時間格式化語法

package main

import (
    "fmt"
    "gitee.com/johng/gf/g/os/gtime"
)

func main() {
    formats := []string{
        "2006-01-02 15:04:05.000",
        "Mon Jan _2 15:04:05 MST 2006",
        "Time is: 03:04:05 PM",
        "2006-01-02T15:04:05.000000000Z07:00 MST",
    }
    t := gtime.Now()
    for _, f := range formats {
        fmt.Println(t.Layout(f))
    }
}

在該示例中,咱們使用四種標準庫的時間格式化語法格式化當前的時間並輸出結果到終端。執行後,輸出結果爲:htm

2018-07-22 11:28:13.945
Sun Jul 22 11:28:13 CST 2018
Time is: 11:28:13 AM
2018-07-22T11:28:13.945153275+08:00 CST

根絕這個示例,也有幾個須要說明的地方:

  1. 自定義時間格式化語法與標準庫時間格式化語法並不衝突,前者使用Format方法,後者使用Layout語法進行格式化,相互獨立,互不衝突,沒法混用;
  2. 標準庫的時間格式化語法自有特色,是否是感受有點複雜;

示例3,時間對象鏈式操做

package main

import (
    "fmt"
    "gitee.com/johng/gf/g/os/gtime"
    "time"
)

func main() {
    // 去年今日
    fmt.Println(gtime.Now().AddDate(-1, 0, 0).Format("Y-m-d"))

    // 去年今日,UTC時間
    fmt.Println(gtime.Now().AddDate(-1, 0, 0).Format("Y-m-d H:i:s T"))
    fmt.Println(gtime.Now().AddDate(-1, 0, 0).UTC().Format("Y-m-d H:i:s T"))

    // 下個月1號凌晨0點整
    fmt.Println(gtime.Now().AddDate(0, 1, 0).Format("Y-m-d 00:00:00"))

    // 2個小時前
    fmt.Println(gtime.Now().Add(-time.Hour).Format("Y-m-d H:i:s"))
}

執行後,輸出結果爲:

2017-07-22
2017-07-22 11:42:36 CST
2017-07-22 03:42:36 UTC
2018-08-22 00:00:00
2018-07-22 10:42:36

該示例比較簡單,便很少贅述。

工具方法

godoc.org/github.com/johng-cn/gf/g/os/gtime

func Date() string
func Datetime() string
func Microsecond() int64
func Millisecond() int64
func Nanosecond() int64
func Second() int64
func SetInterval(t time.Duration, callback func() bool)
func SetTimeZone(zone string) error
func SetTimeout(t time.Duration, callback func())
func StrToTime(str string) (time.Time, error)
func StrToTimeFormat(str string, format string) (time.Time, error)
func StrToTimeLayout(str string, layout string) (time.Time, error)

方法比較簡單,比較經常使用的是如下幾個方法;

  1. Second用於得到當前時間戳,MillisecondMicrosecondNanosecond用於得到當前的毫秒、微秒和納秒值;
  2. DateDatetime用於得到當前日期及當前日期時間;
  3. SetTimeZone用於設置當前進程的全局時區;
  4. 其餘方法說明請查看接口文檔;

簡單示例:

package main

import (
    "fmt"
    "gitee.com/johng/gf/g/os/gtime"
)

func main() {
    fmt.Println("Date       :", gtime.Date())
    fmt.Println("Datetime   :", gtime.Datetime())
    fmt.Println("Second     :", gtime.Second())
    fmt.Println("Millisecond:", gtime.Millisecond())
    fmt.Println("Microsecond:", gtime.Microsecond())
    fmt.Println("Nanosecond :", gtime.Nanosecond())
}

執行後,輸出結果爲:

Date       : 2018-07-22
Datetime   : 2018-07-22 11:52:22
Second     : 1532231542
Millisecond: 1532231542688
Microsecond: 1532231542688688
Nanosecond : 1532231542688690259
相關文章
相關標籤/搜索