From 0e2f76a313f5224fc8174a69a39d3826a0d4e88d Mon Sep 17 00:00:00 2001
From: sandeepkru
Date: Mon, 3 Jan 2022 16:46:26 -0800
Subject: [PATCH] Update `Epoch` example to add UnixMilli from Time package
Standard library time package has support for UnixMilli - https://pkg.go.dev/time#Time.UnixMilli
Updated the example to use the method on Time instead of calculating.
---
examples/epoch/epoch.go | 22 ++++++++--------------
examples/epoch/epoch.hash | 4 ++--
public/epoch | 29 +++++++++++------------------
3 files changed, 21 insertions(+), 34 deletions(-)
diff --git a/examples/epoch/epoch.go b/examples/epoch/epoch.go
index ed7f442..ff37ce5 100644
--- a/examples/epoch/epoch.go
+++ b/examples/epoch/epoch.go
@@ -12,24 +12,18 @@ import (
func main() {
- // Use `time.Now` with `Unix` or `UnixNano` to get
- // elapsed time since the Unix epoch in seconds or
- // nanoseconds, respectively.
+ // Use `time.Now` with `Unix` or `UnixMilli` or `UnixNano`
+ // to get elapsed time since the Unix epoch in seconds or
+ // milliseconds or nanoseconds, respectively.
now := time.Now()
- secs := now.Unix()
- nanos := now.UnixNano()
fmt.Println(now)
- // Note that there is no `UnixMillis`, so to get the
- // milliseconds since epoch you'll need to manually
- // divide from nanoseconds.
- millis := nanos / 1000000
- fmt.Println(secs)
- fmt.Println(millis)
- fmt.Println(nanos)
+ fmt.Println(now.Unix())
+ fmt.Println(now.UnixMilli())
+ fmt.Println(now.UnixNano())
// You can also convert integer seconds or nanoseconds
// since the epoch into the corresponding `time`.
- fmt.Println(time.Unix(secs, 0))
- fmt.Println(time.Unix(0, nanos))
+ fmt.Println(time.Unix(now.Unix(), 0))
+ fmt.Println(time.Unix(0, now.UnixNano()))
}
diff --git a/examples/epoch/epoch.hash b/examples/epoch/epoch.hash
index ad0a471..e0920da 100644
--- a/examples/epoch/epoch.hash
+++ b/examples/epoch/epoch.hash
@@ -1,2 +1,2 @@
-3b1fc502f41a978f1c8150335801aa9096db8954
-0ooeler0RfR
+53610b08e885c8a36dc1a2c63a70e415ce6dfe2a
+LSdohP_slOu
diff --git a/public/epoch b/public/epoch
index 0998c94..3d69363 100644
--- a/public/epoch
+++ b/public/epoch
@@ -44,7 +44,7 @@ Here’s how to do it in Go.
- 
+ 
package main
|
@@ -77,17 +77,15 @@ Here’s how to do it in Go.
- Use time.Now with Unix or UnixNano to get
-elapsed time since the Unix epoch in seconds or
-nanoseconds, respectively.
+ Use time.Now with Unix or UnixMilli or UnixNano
+to get elapsed time since the Unix epoch in seconds or
+milliseconds or nanoseconds, respectively.
|
now := time.Now()
- secs := now.Unix()
- nanos := now.UnixNano()
fmt.Println(now)
|
@@ -95,18 +93,13 @@ nanoseconds, respectively.
- Note that there is no UnixMillis , so to get the
-milliseconds since epoch you’ll need to manually
-divide from nanoseconds.
-
+
|
-
- millis := nanos / 1000000
- fmt.Println(secs)
- fmt.Println(millis)
- fmt.Println(nanos)
+ fmt.Println(now.Unix())
+ fmt.Println(now.UnixMilli())
+ fmt.Println(now.UnixNano())
|
@@ -120,8 +113,8 @@ since the epoch into the corresponding time
.
- fmt.Println(time.Unix(secs, 0))
- fmt.Println(time.Unix(0, nanos))
+ fmt.Println(time.Unix(now.Unix(), 0))
+ fmt.Println(time.Unix(0, now.UnixNano()))
}
|
@@ -174,7 +167,7 @@ parsing and formatting.