From eb24c37143aa26cf9ee641c9985159418401fa3f Mon Sep 17 00:00:00 2001 From: dudaodong Date: Thu, 24 Mar 2022 16:07:17 +0800 Subject: [PATCH] docs: add doc for unix time --- docs/datetime.md | 226 ++++++++++++++++++++++++++++++++++++++++ docs/datetime_zh-CN.md | 227 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 453 insertions(+) diff --git a/docs/datetime.md b/docs/datetime.md index 1da0160..67c61cc 100644 --- a/docs/datetime.md +++ b/docs/datetime.md @@ -43,6 +43,14 @@ import ( - [FormatTimeToStr](#FormatTimeToStr) - [FormatStrToTime](#FormatStrToTime) +- [NewUnixNow](#NewUnixNow) +- [NewUnix](#NewUnix) +- [NewFormat](#NewFormat) +- [NewISO8601](#NewISO8601) +- [ToUnix](#ToUnix) +- [ToFormat](#ToFormat) +- [ToFormatForTpl](#ToFormatForTpl) +- [ToIso8601](#ToIso8601)
@@ -667,3 +675,221 @@ func main() { +### NewUnixNow +

Return unix timestamp of current time

+ +Signature: + +```go +type theTime struct { + unix int64 +} +func NewUnixNow() *theTime +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + tm := datetime.NewUnixNow() + fmt.Println(tm) //&{1647597438} +} +``` + + +### NewUnix +

Return unix timestamp of specified int64 value.

+ +Signature: + +```go +type theTime struct { + unix int64 +} +func NewUnix(unix int64) *theTime +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + tm := datetime.NewUnix(1647597438) + fmt.Println(tm) //&{1647597438} +} +``` + + + +### NewFormat +

Return unix timestamp of specified time string, t should be "yyyy-mm-dd hh:mm:ss".

+ +Signature: + +```go +type theTime struct { + unix int64 +} +func NewFormat(t string) (*theTime, error) +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + tm, err := datetime.NewFormat("2022-03-18 17:04:05") + fmt.Println(tm) //&{1647594245} +} +``` + + + + +### NewISO8601 +

Return unix timestamp of specified iso8601 time string.

+ +Signature: + +```go +type theTime struct { + unix int64 +} +func NewISO8601(iso8601 string) (*theTime, error) +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + tm, err := datetime.NewISO8601("2006-01-02T15:04:05.999Z") + fmt.Println(tm) //&{1136214245} +} +``` + + + +### ToUnix +

Return unix timestamp.

+ +Signature: + +```go +func (t *theTime) ToUnix() int64 +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + tm := datetime.NewUnixNow() + fmt.Println(tm.ToUnix()) //1647597438 +} +``` + + + +### ToFormat +

Return time string 'yyyy-mm-dd hh:mm:ss'.

+ +Signature: + +```go +func (t *theTime) ToFormat() string +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + tm, _ := datetime.NewFormat("2022-03-18 17:04:05") + fmt.Println(tm.ToFormat()) //"2022-03-18 17:04:05" +} +``` + + + +### ToFormatForTpl +

Return the time string which format is specified tpl.

+ +Signature: + +```go +func (t *theTime) ToFormatForTpl(tpl string) string +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + tm, _ := datetime.NewFormat("2022-03-18 17:04:05") + ts := tm.ToFormatForTpl("2006/01/02 15:04:05") + fmt.Println(ts) //"2022/03/18 17:04:05" +} +``` + + +### ToIso8601 +

Return iso8601 time string.

+ +Signature: + +```go +func (t *theTime) ToIso8601() string +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + tm, _ := datetime.NewISO8601("2006-01-02T15:04:05.999Z") + ts := tm.ToIso8601() + fmt.Println(ts) //"2006-01-02T23:04:05+08:00" +} +``` diff --git a/docs/datetime_zh-CN.md b/docs/datetime_zh-CN.md index 67ca138..dcd9e9e 100644 --- a/docs/datetime_zh-CN.md +++ b/docs/datetime_zh-CN.md @@ -42,6 +42,14 @@ import ( - [GetNightTimestamp](#GetNightTimestamp) - [FormatTimeToStr](#FormatTimeToStr) - [FormatStrToTime](#FormatStrToTime) +- [NewUnixNow](#NewUnixNow) +- [NewUnix](#NewUnix) +- [NewFormat](#NewFormat) +- [NewISO8601](#NewISO8601) +- [ToUnix](#ToUnix) +- [ToFormat](#ToFormat) +- [ToFormatForTpl](#ToFormatForTpl) +- [ToIso8601](#ToIso8601)
@@ -666,3 +674,222 @@ func main() { +### NewUnixNow +

创建一个当前时间的unix时间戳

+ +函数签名: + +```go +type theTime struct { + unix int64 +} +func NewUnixNow() *theTime +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + tm := datetime.NewUnixNow() + fmt.Println(tm) //&{1647597438} +} +``` + + +### NewUnix +

创建一个unix时间戳

+ +函数签名: + +```go +type theTime struct { + unix int64 +} +func NewUnix(unix int64) *theTime +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + tm := datetime.NewUnix(1647597438) + fmt.Println(tm) //&{1647597438} +} +``` + + + +### NewFormat +

创建一个yyyy-mm-dd hh:mm:ss格式时间字符串的unix时间戳

+ +函数签名: + +```go +type theTime struct { + unix int64 +} +func NewFormat(t string) (*theTime, error) +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + tm, err := datetime.NewFormat("2022-03-18 17:04:05") + fmt.Println(tm) //&{1647594245} +} +``` + + + + +### NewISO8601 +

创建一个iso8601格式时间字符串的unix时间戳

+ +函数签名: + +```go +type theTime struct { + unix int64 +} +func NewISO8601(iso8601 string) (*theTime, error) +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + tm, err := datetime.NewISO8601("2006-01-02T15:04:05.999Z") + fmt.Println(tm) //&{1136214245} +} +``` + + + +### ToUnix +

返回unix时间戳

+ +函数签名: + +```go +func (t *theTime) ToUnix() int64 +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + tm := datetime.NewUnixNow() + fmt.Println(tm.ToUnix()) //1647597438 +} +``` + + + +### ToFormat +

返回格式'yyyy-mm-dd hh:mm:ss'的日期字符串

+ +函数签名: + +```go +func (t *theTime) ToFormat() string +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + tm, _ := datetime.NewFormat("2022-03-18 17:04:05") + fmt.Println(tm.ToFormat()) //"2022-03-18 17:04:05" +} +``` + + + +### ToFormatForTpl +

返回tpl格式指定的日期字符串

+ +函数签名: + +```go +func (t *theTime) ToFormatForTpl(tpl string) string +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + tm, _ := datetime.NewFormat("2022-03-18 17:04:05") + ts := tm.ToFormatForTpl("2006/01/02 15:04:05") + fmt.Println(ts) //"2022/03/18 17:04:05" +} +``` + + +### ToIso8601 +

返回iso8601日期字符串

+ +函数签名: + +```go +func (t *theTime) ToIso8601() string +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + tm, _ := datetime.NewISO8601("2006-01-02T15:04:05.999Z") + ts := tm.ToIso8601() + fmt.Println(ts) //"2006-01-02T23:04:05+08:00" +} +``` +