Skip to content

Commit 19cc446

Browse files
authored
Merge pull request #705 from devlights:add-time-daysinmonth-example
Add time_daysinmonth.go
2 parents ede4fbe + 2a38b02 commit 19cc446

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

examples/basic/times/examples.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ func (r *register) Regist(m mapping.ExampleMapping) {
3333
m["time_format_dateonly"] = FormatDateOnly
3434
m["time_format_timeonly"] = FormatTimeOnly
3535
m["time_calc_nextmonth"] = CalcNextMonth
36+
m["time_daysinmonth"] = DaysInMonth
3637
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package times
2+
3+
import (
4+
"time"
5+
6+
"github.com/devlights/gomy/output"
7+
)
8+
9+
// DaysInMonth は、月の日数を求めるサンプルです.
10+
//
11+
// # REFERENCES
12+
// - https://cs.opensource.google/go/go/+/refs/tags/go1.21.4:src/time/time.go;l=1467
13+
func DaysInMonth() error {
14+
//
15+
// time.Date() で日の値を 0 で指定すると内部で正規化されて
16+
// -1日する動きとなる。なので、月を1加算して日を0にすると
17+
// その月の末日となる。
18+
//
19+
20+
var (
21+
year = time.Now().Year()
22+
months = [...]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
23+
)
24+
25+
for _, m := range months {
26+
var (
27+
daysInMonth = time.Date(year, time.Month(m+1), 0, 0, 0, 0, 0, time.UTC).Day()
28+
)
29+
30+
output.Stdoutf("[日数]", "%02d月の日数: %d\n", m, daysInMonth)
31+
}
32+
33+
return nil
34+
}

0 commit comments

Comments
 (0)