Skip to content

Commit 1e85322

Browse files
committed
Add time_format_dataonly.go
1 parent b1a1f81 commit 1e85322

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

examples/basic/times/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@
2020
|time\_truncate\_hours.go|time\_truncate\_hours|time.Timeから時刻部分を除去するサンプルです.|
2121
|time\_change\_timezone.go|time\_change\_timezone|time.Timeをいろいろなタイム・ゾーンの値に変換するサンプルです|
2222
|time\_format\_datetime.go|time\_format\_datetime|Go1.20で追加された time.DateTime フォーマット書式についてのサンプルです|
23+
|time\_format\_dateonly.go|time\_format\_dateonly|Go1.20で追加された time.DateOnly フォーマット書式についてのサンプルです|

examples/basic/times/examples.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ func (r *register) Regist(m mapping.ExampleMapping) {
3030
m["time_in"] = TimeIn
3131
m["time_change_timezone"] = ChangeTimeZone
3232
m["time_format_datetime"] = FormatDateTime
33+
m["time_format_dateonly"] = FormatDateOnly
3334
}
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+
// FormatDateOnly は、Go1.20で追加された time.DateOnly フォーマット書式についてのサンプルです.
10+
//
11+
// # REFERENCES
12+
// - https://pkg.go.dev/time@go1.20.2#pkg-constants
13+
func FormatDateOnly() error {
14+
//
15+
// Go1.20 から、time.DateOnly (yyyy-MM-dd) というフォーマットが追加された
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.DateOnly]", "%s\n", time.DateOnly)
36+
output.Stdoutf("[time.Format ]", "%s\n", jst.Format(time.DateOnly))
37+
38+
return nil
39+
}

0 commit comments

Comments
 (0)