From 41331c7afd3eb7a18ab06d1ec31fc503c27caae8 Mon Sep 17 00:00:00 2001 From: Tran Hau Date: Mon, 11 May 2020 23:28:10 +0700 Subject: [PATCH] go --- Development/{go.md => go/README.md} | 0 Development/go/time.md | 44 +++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) rename Development/{go.md => go/README.md} (100%) create mode 100644 Development/go/time.md diff --git a/Development/go.md b/Development/go/README.md similarity index 100% rename from Development/go.md rename to Development/go/README.md diff --git a/Development/go/time.md b/Development/go/time.md new file mode 100644 index 0000000..fdcaafd --- /dev/null +++ b/Development/go/time.md @@ -0,0 +1,44 @@ +# [time](https://golang.org/pkg/time/) + +To parse string to time in golang, must define layout first. + +Read default layouts in [Constants](https://golang.org/pkg/time/#pkg-constants). + +The reference time: + +```txt +Mon Jan 2 15:04:05 MST 2006 +``` + +To define your layout, rewrite reference time in your layout. + +Example `Parse`: + +```go +layout := "2006-01-02 15:04:05" + +t, err := time.Parse(layout, "2020-06-09 23:04:02") +if err != nil { + log.Fatal(err) +} + +fmt.Println(t) +``` + +Example `ParseInLocation`: + +```go +layout := "2006-01-02 15:04:05" + +loc, err := time.LoadLocation("Asia/Ho_Chi_Minh") +if err != nil { + log.Fatal(err) +} + +t, err := time.ParseInLocation(layout, "2020-06-09 23:04:02", loc) +if err != nil { + log.Fatal(err) +} + +fmt.Println(t) +```