From b6550b90329b28fca3037c83f1bad96cd74745e1 Mon Sep 17 00:00:00 2001 From: Sangeeth Sudheer Date: Thu, 2 May 2024 13:26:46 +0530 Subject: [PATCH] Add time --- main.go | 6 ++++-- time/time.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 time/time.go diff --git a/main.go b/main.go index fccc610..499c791 100644 --- a/main.go +++ b/main.go @@ -27,7 +27,8 @@ package main // import "git.sangeeth.dev/gobyexample/templates" // import "git.sangeeth.dev/gobyexample/regex" // 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() { // runes.Runes() @@ -57,5 +58,6 @@ func main() { // templates.Templates() // regex.Regex() // json.Json() - xml.Xml() + // xml.Xml() + time.Time() } diff --git a/time/time.go b/time/time.go new file mode 100644 index 0000000..e94ba5a --- /dev/null +++ b/time/time.go @@ -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() +}