go.mway.dev/chrono@v0.6.1-0.20240126030049-189c5aef20d2/clock/options.go (about) 1 // Copyright (c) 2023 Matt Way 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to 5 // deal in the Software without restriction, including without limitation the 6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 // sell copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 // IN THE THE SOFTWARE. 20 21 package clock 22 23 import ( 24 "time" 25 26 "go.mway.dev/chrono" 27 ) 28 29 // A TimeFunc is a function that returns time as a [time.Time] object. 30 type TimeFunc = func() time.Time 31 32 // DefaultTimeFunc returns a new [TimeFunc] that uses [time.Now] to tell time. 33 func DefaultTimeFunc() TimeFunc { 34 return time.Now 35 } 36 37 // A NanotimeFunc is a function that returns time as integer nanoseconds. 38 type NanotimeFunc = func() int64 39 40 // DefaultNanotimeFunc returns a new [NanotimeFunc] that uses [chrono.Nanotime] 41 // to tell time. 42 func DefaultNanotimeFunc() NanotimeFunc { 43 return chrono.Nanotime 44 } 45 46 // Options configure a [Clock]. 47 type Options struct { 48 // TimeFunc configures the [TimeFunc] for a [Clock]. 49 // If both TimeFunc and NanotimeFunc are provided, NanotimeFunc is used. 50 TimeFunc TimeFunc 51 // NanotimeFunc configures the [NanotimeFunc] for a [Clock]. 52 // If both TimeFunc and NanotimeFunc are provided, NanotimeFunc is used. 53 NanotimeFunc NanotimeFunc 54 } 55 56 // DefaultOptions returns a new [Options] with sane defaults. 57 func DefaultOptions() Options { 58 return Options{ 59 TimeFunc: DefaultTimeFunc(), 60 } 61 } 62 63 func (o Options) apply(opts *Options) { 64 if o.TimeFunc != nil { 65 opts.TimeFunc = o.TimeFunc 66 } 67 68 if o.NanotimeFunc != nil { 69 opts.NanotimeFunc = o.NanotimeFunc 70 } 71 } 72 73 // An Option configures a Clock. 74 type Option interface { 75 apply(*Options) 76 } 77 78 type optionFunc func(*Options) 79 80 func (f optionFunc) apply(o *Options) { 81 f(o) 82 } 83 84 // WithNanotimeFunc returns an [Option] that configures a [Clock] to use f as 85 // its time function. 86 func WithNanotimeFunc(f NanotimeFunc) Option { 87 return optionFunc(func(o *Options) { 88 o.NanotimeFunc = f 89 o.TimeFunc = nil 90 }) 91 } 92 93 // WithTimeFunc returns an [Option] that configures a [Clock] to use f as its 94 // time function. 95 func WithTimeFunc(f TimeFunc) Option { 96 return optionFunc(func(o *Options) { 97 o.TimeFunc = f 98 o.NanotimeFunc = nil 99 }) 100 }