mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 21:02:27 +08:00
feat: add Timestamp,TimestampMilli,TimestampMicro, TimestampNano
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
125
docs/datetime.md
125
docs/datetime.md
@@ -61,6 +61,10 @@ import (
|
||||
- [DayOfYear](#DayOfYear)
|
||||
- [IsWeekend](#IsWeekend)
|
||||
- [NowDateOrTime](#NowDateOrTime)
|
||||
- [Timestamp](#Timestamp)
|
||||
- [TimestampMilli](#TimestampMilli)
|
||||
- [TimestampMicro](#TimestampMicro)
|
||||
- [TimestampNano](#TimestampNano)
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
@@ -1341,3 +1345,124 @@ func main() {
|
||||
// 2023-07-26 02:01:30
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Timestamp">Timestamp</span>
|
||||
|
||||
<p>Return current second timestamp.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func Timestamp(timezone ...string) int64
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/datetime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ts := datetime.Timestamp()
|
||||
|
||||
fmt.Println(ts)
|
||||
|
||||
// Output:
|
||||
// 1690363051
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### <span id="TimestampMilli">TimestampMilli</span>
|
||||
|
||||
<p>Return current mill second timestamp.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func TimestampMilli(timezone ...string) int64
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/datetime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ts := datetime.TimestampMilli()
|
||||
|
||||
fmt.Println(ts)
|
||||
|
||||
// Output:
|
||||
// 1690363051331
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="TimestampMicro">TimestampMicro</span>
|
||||
|
||||
<p>Return current micro second timestamp.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func TimestampMicro(timezone ...string) int64
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/datetime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ts := datetime.TimestampMicro()
|
||||
|
||||
fmt.Println(ts)
|
||||
|
||||
// Output:
|
||||
// 1690363051331784
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="TimestampNano">TimestampNano</span>
|
||||
|
||||
<p>Return current nano second timestamp.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func TimestampNano(timezone ...string) int64
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/datetime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ts := datetime.TimestampNano()
|
||||
|
||||
fmt.Println(ts)
|
||||
|
||||
// Output:
|
||||
// 1690363051331788000
|
||||
}
|
||||
```
|
||||
|
||||
@@ -60,6 +60,10 @@ import (
|
||||
- [DayOfYear](#DayOfYear)
|
||||
- [IsWeekend](#IsWeekend)
|
||||
- [NowDateOrTime](#NowDateOrTime)
|
||||
- [Timestamp](#Timestamp)
|
||||
- [TimestampMilli](#TimestampMilli)
|
||||
- [TimestampMicro](#TimestampMicro)
|
||||
- [TimestampNano](#TimestampNano)
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
@@ -1340,3 +1344,124 @@ func main() {
|
||||
// 2023-07-26 02:01:30
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Timestamp">Timestamp</span>
|
||||
|
||||
<p>返回当前秒级时间戳。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func Timestamp(timezone ...string) int64
|
||||
```
|
||||
|
||||
<b>实例:</b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/datetime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ts := datetime.Timestamp()
|
||||
|
||||
fmt.Println(ts)
|
||||
|
||||
// Output:
|
||||
// 1690363051
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### <span id="TimestampMilli">TimestampMilli</span>
|
||||
|
||||
<p>返回当前毫秒级时间戳。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func TimestampMilli(timezone ...string) int64
|
||||
```
|
||||
|
||||
<b>实例:</b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/datetime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ts := datetime.TimestampMilli()
|
||||
|
||||
fmt.Println(ts)
|
||||
|
||||
// Output:
|
||||
// 1690363051331
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="TimestampMicro">TimestampMicro</span>
|
||||
|
||||
<p>返回当前微秒级时间戳。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func TimestampMicro(timezone ...string) int64
|
||||
```
|
||||
|
||||
<b>实例:</b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/datetime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ts := datetime.TimestampMicro()
|
||||
|
||||
fmt.Println(ts)
|
||||
|
||||
// Output:
|
||||
// 1690363051331784
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="TimestampNano">TimestampNano</span>
|
||||
|
||||
<p>返回当前纳秒级时间戳。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func TimestampNano(timezone ...string) int64
|
||||
```
|
||||
|
||||
<b>实例:</b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/datetime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ts := datetime.TimestampNano()
|
||||
|
||||
fmt.Println(ts)
|
||||
|
||||
// Output:
|
||||
// 1690363051331788000
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user