2024-05-02 07:56:46 +00:00
|
|
|
package time
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var f = fmt.Println
|
|
|
|
|
|
|
|
func epoch() {
|
|
|
|
now := time.Now().UTC()
|
|
|
|
|
|
|
|
f("Epoch seconds:", now.Unix())
|
|
|
|
f("Epoch milli:", now.UnixMilli())
|
|
|
|
f("Epoch nano:", now.UnixNano())
|
|
|
|
|
|
|
|
f("Custom time from epoch:", time.Unix(now.Unix(), 0))
|
|
|
|
f("Custom time from epoch:", time.Unix(0, now.UnixNano()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Time() {
|
|
|
|
now := time.Now().UTC()
|
|
|
|
f(now)
|
|
|
|
|
|
|
|
xmasTime := time.Date(2023, 12, 25, 9, 0, 0, 0, time.UTC)
|
|
|
|
f("XMAS =>", xmasTime)
|
|
|
|
|
|
|
|
f("Year:", xmasTime.Year())
|
|
|
|
f("Month:", xmasTime.Month())
|
|
|
|
f("Day:", xmasTime.Day())
|
|
|
|
f("Hour:", xmasTime.Hour())
|
|
|
|
f("Minute:", xmasTime.Minute())
|
|
|
|
f("Second:", xmasTime.Second())
|
|
|
|
f("Nanosecond:", xmasTime.Nanosecond())
|
|
|
|
f("Weekday:", xmasTime.Weekday())
|
|
|
|
|
|
|
|
f("Is Xmas before now?", xmasTime.Before(now))
|
|
|
|
f("Is Xmas after now?", xmasTime.After(now))
|
|
|
|
f("Is Xmas same as now?", xmasTime.Equal(now))
|
|
|
|
|
|
|
|
diff := now.Sub(xmasTime)
|
|
|
|
f(diff)
|
|
|
|
|
|
|
|
f(diff.Hours())
|
|
|
|
f(diff.Minutes())
|
|
|
|
f(diff.Seconds())
|
|
|
|
f(diff.Nanoseconds())
|
|
|
|
|
|
|
|
f(xmasTime.Add(diff))
|
|
|
|
f(now.Add(-diff))
|
|
|
|
|
|
|
|
epoch()
|
2024-05-02 08:36:18 +00:00
|
|
|
timeFormatting()
|
|
|
|
}
|
|
|
|
|
|
|
|
func timeFormatting() {
|
|
|
|
now := time.Now()
|
|
|
|
t1, _ := time.Parse(
|
|
|
|
time.RFC3339,
|
|
|
|
"2023-12-25T10:00:00+05:30",
|
|
|
|
)
|
|
|
|
f(t1)
|
|
|
|
|
|
|
|
// Example string should match the datetime: Monday January 02 2006 15:04:05
|
|
|
|
// (this resembles month:1 day:2 hour:3 minute:4 seconds:5 year:6)
|
|
|
|
f("Custom format:", now.Format("2006/1/2"))
|
|
|
|
f("Custom format:", now.Format("Monday, 02 Jan"))
|
|
|
|
f("Custom format:", t1.Format("Monday, _2 Jan"))
|
|
|
|
f("Custom format:", t1.Format("Monday, 2/01/2006"))
|
2024-05-02 07:56:46 +00:00
|
|
|
}
|