diff --git a/datetime/datetime.go b/datetime/datetime.go index c517ffa..c67df0d 100644 --- a/datetime/datetime.go +++ b/datetime/datetime.go @@ -315,3 +315,68 @@ func NowDateOrTime(format string, timezone ...string) string { return time.Now().Format(tf) } + +// Timestamp return current second timestamp. +// Play: todo +func Timestamp(timezone ...string) int64 { + t := time.Now() + + if timezone != nil && timezone[0] != "" { + loc, err := time.LoadLocation(timezone[0]) + if err != nil { + return 0 + } + + t = t.In(loc) + } + + return t.Unix() +} + +// TimestampMilli return current mill second timestamp. +// Play: todo +func TimestampMilli(timezone ...string) int64 { + t := time.Now() + + if timezone != nil && timezone[0] != "" { + loc, err := time.LoadLocation(timezone[0]) + if err != nil { + return 0 + } + t = t.In(loc) + } + + return int64(time.Nanosecond) * t.UnixNano() / int64(time.Millisecond) +} + +// TimestampMicro return current micro second timestamp. +// Play: todo +func TimestampMicro(timezone ...string) int64 { + t := time.Now() + + if timezone != nil && timezone[0] != "" { + loc, err := time.LoadLocation(timezone[0]) + if err != nil { + return 0 + } + t = t.In(loc) + } + + return int64(time.Nanosecond) * t.UnixNano() / int64(time.Microsecond) +} + +// TimestampNano return current nano second timestamp. +// Play: todo +func TimestampNano(timezone ...string) int64 { + t := time.Now() + + if timezone != nil && timezone[0] != "" { + loc, err := time.LoadLocation(timezone[0]) + if err != nil { + return 0 + } + t = t.In(loc) + } + + return t.UnixNano() +} diff --git a/datetime/datetime_test.go b/datetime/datetime_test.go index 6c9d908..6066dd0 100644 --- a/datetime/datetime_test.go +++ b/datetime/datetime_test.go @@ -394,3 +394,19 @@ func TestNowDateOrTime(t *testing.T) { t.Log(result) } } + +func TestTimestamp(t *testing.T) { + t.Parallel() + + ts1 := Timestamp() + t.Log(ts1) + + ts2 := TimestampMilli() + t.Log(ts2) + + ts3 := TimestampMicro() + t.Log(ts3) + + ts4 := TimestampNano() + t.Log(ts4) +} diff --git a/docs/datetime.md b/docs/datetime.md index 46a1392..0805b3a 100644 --- a/docs/datetime.md +++ b/docs/datetime.md @@ -61,6 +61,10 @@ import ( - [DayOfYear](#DayOfYear) - [IsWeekend](#IsWeekend) - [NowDateOrTime](#NowDateOrTime) +- [Timestamp](#Timestamp) +- [TimestampMilli](#TimestampMilli) +- [TimestampMicro](#TimestampMicro) +- [TimestampNano](#TimestampNano)
@@ -1341,3 +1345,124 @@ func main() { // 2023-07-26 02:01:30 } ``` + +### Timestamp + +

Return current second timestamp.

+ +Signature: + +```go +func Timestamp(timezone ...string) int64 +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + ts := datetime.Timestamp() + + fmt.Println(ts) + + // Output: + // 1690363051 +} +``` + + +### TimestampMilli + +

Return current mill second timestamp.

+ +Signature: + +```go +func TimestampMilli(timezone ...string) int64 +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + ts := datetime.TimestampMilli() + + fmt.Println(ts) + + // Output: + // 1690363051331 +} +``` + +### TimestampMicro + +

Return current micro second timestamp.

+ +Signature: + +```go +func TimestampMicro(timezone ...string) int64 +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + ts := datetime.TimestampMicro() + + fmt.Println(ts) + + // Output: + // 1690363051331784 +} +``` + +### TimestampNano + +

Return current nano second timestamp.

+ +Signature: + +```go +func TimestampNano(timezone ...string) int64 +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + ts := datetime.TimestampNano() + + fmt.Println(ts) + + // Output: + // 1690363051331788000 +} +``` diff --git a/docs/datetime_zh-CN.md b/docs/datetime_zh-CN.md index 962783b..7237099 100644 --- a/docs/datetime_zh-CN.md +++ b/docs/datetime_zh-CN.md @@ -60,6 +60,10 @@ import ( - [DayOfYear](#DayOfYear) - [IsWeekend](#IsWeekend) - [NowDateOrTime](#NowDateOrTime) +- [Timestamp](#Timestamp) +- [TimestampMilli](#TimestampMilli) +- [TimestampMicro](#TimestampMicro) +- [TimestampNano](#TimestampNano)
@@ -1340,3 +1344,124 @@ func main() { // 2023-07-26 02:01:30 } ``` + +### Timestamp + +

返回当前秒级时间戳。

+ +函数签名: + +```go +func Timestamp(timezone ...string) int64 +``` + +实例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + ts := datetime.Timestamp() + + fmt.Println(ts) + + // Output: + // 1690363051 +} +``` + + +### TimestampMilli + +

返回当前毫秒级时间戳。

+ +函数签名: + +```go +func TimestampMilli(timezone ...string) int64 +``` + +实例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + ts := datetime.TimestampMilli() + + fmt.Println(ts) + + // Output: + // 1690363051331 +} +``` + +### TimestampMicro + +

返回当前微秒级时间戳。

+ +函数签名: + +```go +func TimestampMicro(timezone ...string) int64 +``` + +实例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + ts := datetime.TimestampMicro() + + fmt.Println(ts) + + // Output: + // 1690363051331784 +} +``` + +### TimestampNano + +

返回当前纳秒级时间戳。

+ +函数签名: + +```go +func TimestampNano(timezone ...string) int64 +``` + +实例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + ts := datetime.TimestampNano() + + fmt.Println(ts) + + // Output: + // 1690363051331788000 +} +``` \ No newline at end of file