mirror of https://github.com/go-co-op/gocron.git
parse time.Time from AtTime (#806)
This commit is contained in:
parent
edb147514f
commit
c180381e94
8
job.go
8
job.go
|
|
@ -413,6 +413,14 @@ func (a atTime) time(location *time.Location) time.Time {
|
|||
return time.Date(0, 0, 0, int(a.hours), int(a.minutes), int(a.seconds), 0, location)
|
||||
}
|
||||
|
||||
// TimeFromAtTime is a helper function to allow converting AtTime into a time.Time value
|
||||
// Note: the time.Time value will have zero values for all Time fields except Hours, Minutes, Seconds.
|
||||
//
|
||||
// For example: time.Date(0, 0, 0, 1, 1, 1, 0, time.UTC)
|
||||
func TimeFromAtTime(at AtTime, loc *time.Location) time.Time {
|
||||
return at().time(loc)
|
||||
}
|
||||
|
||||
// AtTime defines a function that returns the internal atTime
|
||||
type AtTime func() atTime
|
||||
|
||||
|
|
|
|||
48
job_test.go
48
job_test.go
|
|
@ -676,3 +676,51 @@ func TestJob_PanicOccurred(t *testing.T) {
|
|||
close(gotCh)
|
||||
close(errCh)
|
||||
}
|
||||
|
||||
func TestTimeFromAtTime(t *testing.T) {
|
||||
testTimeUTC := time.Date(0, 0, 0, 1, 1, 1, 0, time.UTC)
|
||||
cst, err := time.LoadLocation("America/Chicago")
|
||||
require.NoError(t, err)
|
||||
testTimeCST := time.Date(0, 0, 0, 1, 1, 1, 0, cst)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
at AtTime
|
||||
loc *time.Location
|
||||
expectedTime time.Time
|
||||
expectedStr string
|
||||
}{
|
||||
{
|
||||
"UTC",
|
||||
NewAtTime(
|
||||
uint(testTimeUTC.Hour()),
|
||||
uint(testTimeUTC.Minute()),
|
||||
uint(testTimeUTC.Second()),
|
||||
),
|
||||
time.UTC,
|
||||
testTimeUTC,
|
||||
"01:01:01",
|
||||
},
|
||||
{
|
||||
"CST",
|
||||
NewAtTime(
|
||||
uint(testTimeCST.Hour()),
|
||||
uint(testTimeCST.Minute()),
|
||||
uint(testTimeCST.Second()),
|
||||
),
|
||||
cst,
|
||||
testTimeCST,
|
||||
"01:01:01",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := TimeFromAtTime(tt.at, tt.loc)
|
||||
assert.Equal(t, tt.expectedTime, result)
|
||||
|
||||
resultFmt := result.Format("15:04:05")
|
||||
assert.Equal(t, tt.expectedStr, resultFmt)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue