Skip to content

Commit c2ca96c

Browse files
authored
Merge pull request #773 from devlights/add-time-format-microsecond
2 parents 99f2563 + 3e2ad83 commit c2ca96c

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

examples/basic/times/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@
2323
| time_format_dateonly.go | time_format_dateonly | Go1.20で追加された time.DateOnly フォーマット書式についてのサンプルです |
2424
| time_format_timeonly.go | time_format_timeonly | Go1.20で追加された time.TimeOnly フォーマット書式についてのサンプルです |
2525
| time_format_millisecond.go | time_format_millisecond | time.Format() にてミリ秒を出力するサンプルです。 |
26+
| time_format_microsecond.go | time_format_microsecond | time.Format() にてマイクロ秒を出力するサンプルす。 |
2627
| time_calc_nextmonth.go | time_calc_nextmonth | 翌月の日付を求めるサンプルです |
2728
| time_daysinmonth.go | time_daysinmonth | 月の日数を求めるサンプルです |

examples/basic/times/examples.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func (r *register) Regist(m mapping.ExampleMapping) {
3333
m["time_format_dateonly"] = FormatDateOnly
3434
m["time_format_timeonly"] = FormatTimeOnly
3535
m["time_format_millisecond"] = FormatMillisecond
36+
m["time_format_microsecond"] = FormatMicrosecond
3637
m["time_calc_nextmonth"] = CalcNextMonth
3738
m["time_daysinmonth"] = DaysInMonth
3839
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package times
2+
3+
import (
4+
"time"
5+
6+
"github.com/devlights/gomy/output"
7+
)
8+
9+
// FormatMicrosecond は、time.Format() にてマイクロ秒を出力するサンプルです。
10+
//
11+
// マイクロ秒をフォーマットするには ".000000" とする。
12+
// ドットを付けないとフォーマットされないので注意。
13+
//
14+
// # REFERENCES
15+
// - https://pkg.go.dev/time@go1.22.1#Time.Format
16+
func FormatMicrosecond() error {
17+
var (
18+
microsec = func() string { return time.Now().Format("05.000000") }
19+
wait100ms = func() { time.Sleep((100 * 1000) * time.Microsecond) }
20+
)
21+
22+
output.Stdoutl("[1]", microsec())
23+
wait100ms()
24+
output.Stdoutl("[2]", microsec())
25+
wait100ms()
26+
output.Stdoutl("[3]", microsec())
27+
28+
for range 5 {
29+
wait100ms()
30+
}
31+
32+
output.Stdoutl("[4]", microsec())
33+
34+
return nil
35+
36+
/*
37+
$ task
38+
task: [build] go build .
39+
task: [run] ./try-golang -onetime
40+
41+
ENTER EXAMPLE NAME: time_format_microsecond
42+
43+
[Name] "time_format_microsecond"
44+
[1] 31.695992
45+
[2] 31.796184
46+
[3] 31.896460
47+
[4] 32.397545
48+
49+
50+
[Elapsed] 701.582286ms
51+
*/
52+
53+
}

0 commit comments

Comments
 (0)