Skip to content

Commit b1a1f81

Browse files
authored
Merge pull request #649 from devlights:add-time-new-format
Add time_format_datetime.go
2 parents 3347f9b + 416bed1 commit b1a1f81

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

examples/basic/times/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@
1818
|time\_in.go|time\_in|time.In() の使い方のサンプルです|
1919
|time\_truncate.go|time\_truncate|time.Truncate() のサンプルです。n分置き や n時間置き の時間を表現することができます。|
2020
|time\_truncate\_hours.go|time\_truncate\_hours|time.Timeから時刻部分を除去するサンプルです.|
21+
|time\_change\_timezone.go|time\_change\_timezone|time.Timeをいろいろなタイム・ゾーンの値に変換するサンプルです|
22+
|time\_format\_datetime.go|time\_format\_datetime|Go1.20で追加された time.DateTime フォーマット書式についてのサンプルです|

examples/basic/times/examples.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ func (r *register) Regist(m mapping.ExampleMapping) {
2929
m["time_json_custom"] = TimeJsonCustom
3030
m["time_in"] = TimeIn
3131
m["time_change_timezone"] = ChangeTimeZone
32+
m["time_format_datetime"] = FormatDateTime
3233
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package times
2+
3+
import (
4+
"time"
5+
6+
"github.com/devlights/gomy/output"
7+
)
8+
9+
// FormatDateTime は、Go1.20で追加された time.DateTime フォーマット書式についてのサンプルです.
10+
//
11+
// # REFERENCES
12+
// - https://pkg.go.dev/time@go1.20.2#pkg-constants
13+
func FormatDateTime() error {
14+
//
15+
// Go1.20 から、time.DateTime (yyyy-MM-dd HH:mm:ss) というフォーマットが追加された
16+
// これにより、少しだけフォーマットする際に楽になった
17+
//
18+
var (
19+
locJst *time.Location
20+
now time.Time
21+
jst time.Time
22+
err error
23+
)
24+
25+
locJst, err = time.LoadLocation("Asia/Tokyo")
26+
if err != nil {
27+
return err
28+
}
29+
30+
now = time.Now()
31+
jst = now.In(locJst)
32+
33+
output.Stdoutf("[UTC ]", "%v\n", now.UTC())
34+
output.Stdoutf("[JST ]", "%v\n", jst)
35+
output.Stdoutf("[time.DateTime]", "%s\n", time.DateTime)
36+
output.Stdoutf("[time.Format ]", "%s\n", jst.Format(time.DateTime))
37+
38+
return nil
39+
}

0 commit comments

Comments
 (0)