é¢é£è¨äº
æ¦è¦
以ä¸ãèªåç¨ã®ã¡ã¢ã§ããå¿ããªããã¡ã«ã¡ã¢ã¡ã¢ããã
Go1.20ãã time ããã±ã¼ã¸ã« DateTime, DateOnly, TimeOnly å®æ°ã追å ããã¾ããã
æ¥ä»ã®ãã©ã¼ããããå°ã楽ã«ãªãã¾ãããã
試ãã¦ã¿ã
time.DateTime
package times import ( "time" "github.com/devlights/gomy/output" ) // FormatDateTime ã¯ãGo1.20ã§è¿½å ããã time.DateTime ãã©ã¼ãããæ¸å¼ã«ã¤ãã¦ã®ãµã³ãã«ã§ã. // // # REFERENCES // - https://pkg.go.dev/[email protected]#pkg-constants func FormatDateTime() error { // // Go1.20 ãããtime.DateTime (yyyy-MM-dd HH:mm:ss) ã¨ãããã©ã¼ãããã追å ããã // ããã«ãããå°ãã ããã©ã¼ãããããéã«æ¥½ã«ãªã£ã // var ( locJst *time.Location now time.Time jst time.Time err error ) locJst, err = time.LoadLocation("Asia/Tokyo") if err != nil { return err } now = time.Now() jst = now.In(locJst) output.Stdoutf("[UTC ]", "%v\n", now.UTC()) output.Stdoutf("[JST ]", "%v\n", jst) output.Stdoutf("[time.DateOnly]", "%s\n", time.DateTime) output.Stdoutf("[time.Format ]", "%s\n", jst.Format(time.DateTime)) return nil }
å®è¡ããã¨ä»¥ä¸ã®ããã«ãªãã¾ãã
gitpod /workspace/try-golang (master) $ task task: [run] go run . -onetime ENTER EXAMPLE NAME: time_format_datetime [Name] "time_format_datetime" [UTC ] 2023-04-03 08:15:53.769488165 +0000 UTC [JST ] 2023-04-03 17:15:53.769488165 +0900 JST [time.DateOnly] 2006-01-02 15:04:05 [time.Format ] 2023-04-03 17:15:53 [Elapsed] 412.28µs
time.DateOnly
package times import ( "time" "github.com/devlights/gomy/output" ) // FormatDateOnly ã¯ãGo1.20ã§è¿½å ããã time.DateOnly ãã©ã¼ãããæ¸å¼ã«ã¤ãã¦ã®ãµã³ãã«ã§ã. // // # REFERENCES // - https://pkg.go.dev/[email protected]#pkg-constants func FormatDateOnly() error { // // Go1.20 ãããtime.DateOnly (yyyy-MM-dd) ã¨ãããã©ã¼ãããã追å ããã // ããã«ãããå°ãã ããã©ã¼ãããããéã«æ¥½ã«ãªã£ã // var ( locJst *time.Location now time.Time jst time.Time err error ) locJst, err = time.LoadLocation("Asia/Tokyo") if err != nil { return err } now = time.Now() jst = now.In(locJst) output.Stdoutf("[UTC ]", "%v\n", now.UTC()) output.Stdoutf("[JST ]", "%v\n", jst) output.Stdoutf("[time.DateOnly]", "%s\n", time.DateOnly) output.Stdoutf("[time.Format ]", "%s\n", jst.Format(time.DateOnly)) return nil }
å®è¡ããã¨ä»¥ä¸ã®ããã«ãªãã¾ãã
gitpod /workspace/try-golang (master) $ task task: [run] go run . -onetime ENTER EXAMPLE NAME: time_format_dateonly [Name] "time_format_dateonly" [UTC ] 2023-04-03 08:16:39.763871276 +0000 UTC [JST ] 2023-04-03 17:16:39.763871276 +0900 JST [time.DateOnly] 2006-01-02 [time.Format ] 2023-04-03 [Elapsed] 102.66µs
time.TimeOnly
package times import ( "time" "github.com/devlights/gomy/output" ) // FormatDateTime ã¯ãGo1.20ã§è¿½å ããã time.TimeOnly ãã©ã¼ãããæ¸å¼ã«ã¤ãã¦ã®ãµã³ãã«ã§ã. // // # REFERENCES // - https://pkg.go.dev/[email protected]#pkg-constants func FormatTimeOnly() error { // // Go1.20 ãããtime.DateTime (yyyy-MM-dd HH:mm:ss) ã¨ãããã©ã¼ãããã追å ããã // ããã«ãããå°ãã ããã©ã¼ãããããéã«æ¥½ã«ãªã£ã // var ( locJst *time.Location now time.Time jst time.Time err error ) locJst, err = time.LoadLocation("Asia/Tokyo") if err != nil { return err } now = time.Now() jst = now.In(locJst) output.Stdoutf("[UTC ]", "%v\n", now.UTC()) output.Stdoutf("[JST ]", "%v\n", jst) output.Stdoutf("[time.TimeOnly]", "%s\n", time.TimeOnly) output.Stdoutf("[time.Format ]", "%s\n", jst.Format(time.TimeOnly)) return nil }
å®è¡ããã¨ä»¥ä¸ã®ããã«ãªãã¾ãã
gitpod /workspace/try-golang (master) $ task task: [run] go run . -onetime ENTER EXAMPLE NAME: time_format_timeonly [Name] "time_format_timeonly" [UTC ] 2023-04-03 08:17:15.413025001 +0000 UTC [JST ] 2023-04-03 17:17:15.413025001 +0900 JST [time.TimeOnly] 15:04:05 [time.Format ] 17:17:15 [Elapsed] 139.98µs
åèæ å ±
https://pkg.go.dev/[email protected]#pkg-constants
Goã®ããããæ¸ç±
éå»ã®è¨äºã«ã¤ãã¦ã¯ã以ä¸ã®ãã¼ã¸ãããåç §ä¸ããã
ãµã³ãã«ã³ã¼ãã¯ã以ä¸ã®å ´æã§å ¬éãã¦ãã¾ãã