gobyexample/time/time.go

54 lines
1.0 KiB
Go

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()
}