parse time.Time from AtTime (#806)

This commit is contained in:
John Roesler 2024-12-17 11:38:16 -06:00 committed by GitHub
parent edb147514f
commit c180381e94
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 56 additions and 0 deletions

8
job.go
View File

@ -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

View File

@ -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)
})
}
}