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 .
66package clock
77
88import (
@@ -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.
7273func 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.
8283func 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.
8788func (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.
153161func (c Clock ) TruncateMillisecond () Clock {
154162 return (c / Millisecond ) * Millisecond
155163}
0 commit comments