Skip to content

Commit 16786d5

Browse files
committed
New Clock.ToTime method added
1 parent 9522185 commit 16786d5

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

clock/clock.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// Package clock specifies a time of day with resolution to the nearest millisecond.
5+
// Package clock specifies a time of day with resolution to the nearest nanosecond.
66
package clock
77

88
import (
@@ -69,6 +69,7 @@ func New(hour, minute, second, millisec int) Clock {
6969
}
7070

7171
// NewAt returns a new [Clock] with specified hour, minute, seconds (to nanosecond resolution).
72+
// The date and timezone information is discarded.
7273
func NewAt(t time.Time) Clock {
7374
hour, minute, second := t.Clock()
7475
hx := Clock(hour) * Hour
@@ -78,16 +79,23 @@ func NewAt(t time.Time) Clock {
7879
return hx + mx + sx + ns
7980
}
8081

81-
// SinceMidnight returns a new [Clock] based on a duration since some arbitrary midnight.
82+
// SinceMidnight returns a new [Clock] based on a duration since midnight.
8283
func SinceMidnight(d time.Duration) Clock {
8384
return Clock(d)
8485
}
8586

86-
// DurationSinceMidnight convert a [Clock] to a [time.Duration] since some arbitrary midnight.
87+
// DurationSinceMidnight convert a [Clock] to a [time.Duration] since midnight.
8788
func (c Clock) DurationSinceMidnight() time.Duration {
8889
return time.Duration(c)
8990
}
9091

92+
// ToTime converts the clock and a given date to a [time.Time].
93+
// For example, given a date.Date d, use c.ToTime(d.Date()).
94+
func (c Clock) ToTime(year int, month time.Month, day int) time.Time {
95+
return time.Date(year, month, day,
96+
c.Hour(), c.Minute(), c.Second(), c.Nanosecond(), time.UTC)
97+
}
98+
9199
// Add returns a new [Clock] offset from this clock specified hour, minute, second and millisecond.
92100
// The parameters can be negative.
93101
//
@@ -149,7 +157,7 @@ func (c Clock) IsMidnight() bool {
149157

150158
// TruncateMillisecond discards any fractional digits within the millisecond represented by c.
151159
// For example, for 10:20:30.456111222 this will return 10:20:30.456.
152-
// This method will force the [Clock.String] method to limit its output to three decimal places.
160+
// This method will also force the [Clock.String] method to limit its output to three decimal places.
153161
func (c Clock) TruncateMillisecond() Clock {
154162
return (c / Millisecond) * Millisecond
155163
}

clock/clock_test.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
package clock
5+
package clock_test
66

77
import (
88
"fmt"
99
"testing"
1010
time "time"
1111

1212
"github.com/govalues/decimal"
13+
"github.com/rickb777/date/v2"
14+
. "github.com/rickb777/date/v2/clock"
1315
"github.com/rickb777/period"
1416
)
1517

@@ -66,6 +68,28 @@ func TestClockSinceMidnight(t *testing.T) {
6668
}
6769
}
6870

71+
func TestClockToTime(t *testing.T) {
72+
cases := []struct {
73+
c Clock
74+
in date.Date
75+
exp time.Time
76+
}{
77+
{
78+
c: New(4, 25, 6, 999),
79+
in: date.New(2001, 2, 3),
80+
exp: time.Date(2001, 2, 3, 4, 25, 6, 999_000_000, time.UTC),
81+
},
82+
}
83+
for i, x := range cases {
84+
t.Run(fmt.Sprintf("%d %s", i, x.c), func(t *testing.T) {
85+
a := x.c.ToTime(x.in.Date())
86+
if !a.Equal(x.exp) {
87+
t.Errorf("%d: got %v, want %v (%d)", i, a, x.exp, x.c)
88+
}
89+
})
90+
}
91+
}
92+
6993
func TestClockIsInOneDay(t *testing.T) {
7094
cases := []struct {
7195
in Clock

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ toolchain go1.24.2
77
require (
88
github.com/govalues/decimal v0.1.36
99
github.com/rickb777/period v1.0.21
10-
golang.org/x/text v0.31.0
10+
golang.org/x/text v0.32.0
1111
)
1212

1313
require github.com/rickb777/plural v1.4.7 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ github.com/rickb777/plural v1.4.7 h1:rBRAxp9aTFYzWTLWIE/UTwKcaqSSAV2ml7aOUFYpAGo
1212
github.com/rickb777/plural v1.4.7/go.mod h1:DB19dtrplGS5s6VJVHn7tvmFYPoE83p1xqio3oVnNRM=
1313
golang.org/x/text v0.31.0 h1:aC8ghyu4JhP8VojJ2lEHBnochRno1sgL6nEi9WGFGMM=
1414
golang.org/x/text v0.31.0/go.mod h1:tKRAlv61yKIjGGHX/4tP1LTbc13YSec1pxVEWXzfoeM=
15+
golang.org/x/text v0.32.0 h1:ZD01bjUt1FQ9WJ0ClOL5vxgxOI/sVCNgX1YtKwcY0mU=
16+
golang.org/x/text v0.32.0/go.mod h1:o/rUWzghvpD5TXrTIBuJU77MTaN0ljMWE47kxGJQ7jY=

0 commit comments

Comments
 (0)