Add time
This commit is contained in:
parent
b1fe04a282
commit
b6550b9032
6
main.go
6
main.go
@ -27,7 +27,8 @@ package main
|
|||||||
// import "git.sangeeth.dev/gobyexample/templates"
|
// import "git.sangeeth.dev/gobyexample/templates"
|
||||||
// import "git.sangeeth.dev/gobyexample/regex"
|
// import "git.sangeeth.dev/gobyexample/regex"
|
||||||
// import "git.sangeeth.dev/gobyexample/json"
|
// import "git.sangeeth.dev/gobyexample/json"
|
||||||
import "git.sangeeth.dev/gobyexample/xml"
|
// import "git.sangeeth.dev/gobyexample/xml"
|
||||||
|
import "git.sangeeth.dev/gobyexample/time"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// runes.Runes()
|
// runes.Runes()
|
||||||
@ -57,5 +58,6 @@ func main() {
|
|||||||
// templates.Templates()
|
// templates.Templates()
|
||||||
// regex.Regex()
|
// regex.Regex()
|
||||||
// json.Json()
|
// json.Json()
|
||||||
xml.Xml()
|
// xml.Xml()
|
||||||
|
time.Time()
|
||||||
}
|
}
|
||||||
|
53
time/time.go
Normal file
53
time/time.go
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
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()
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user