Skip to content

Commit ce915e2

Browse files
authored
Merge pull request #651 from devlights:add-time-format-timeonly
Add time_format_timeonly.go
2 parents df451ad + a1cefd8 commit ce915e2

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

examples/basic/times/README.md

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

examples/basic/times/examples.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ func (r *register) Regist(m mapping.ExampleMapping) {
3131
m["time_change_timezone"] = ChangeTimeZone
3232
m["time_format_datetime"] = FormatDateTime
3333
m["time_format_dateonly"] = FormatDateOnly
34+
m["time_format_timeonly"] = FormatTimeOnly
3435
}

examples/basic/times/time_format_datetime.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func FormatDateTime() error {
3232

3333
output.Stdoutf("[UTC ]", "%v\n", now.UTC())
3434
output.Stdoutf("[JST ]", "%v\n", jst)
35-
output.Stdoutf("[time.DateTime]", "%s\n", time.DateTime)
35+
output.Stdoutf("[time.DateOnly]", "%s\n", time.DateTime)
3636
output.Stdoutf("[time.Format ]", "%s\n", jst.Format(time.DateTime))
3737

3838
return nil
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.TimeOnly フォーマット書式についてのサンプルです.
10+
//
11+
// # REFERENCES
12+
// - https://pkg.go.dev/time@go1.20.2#pkg-constants
13+
func FormatTimeOnly() 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.TimeOnly]", "%s\n", time.TimeOnly)
36+
output.Stdoutf("[time.Format ]", "%s\n", jst.Format(time.TimeOnly))
37+
38+
return nil
39+
}

0 commit comments

Comments
 (0)