Skip to content

Commit c180381

Browse files
authored
parse time.Time from AtTime (#806)
1 parent edb1475 commit c180381

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

job.go

+8
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,14 @@ func (a atTime) time(location *time.Location) time.Time {
413413
return time.Date(0, 0, 0, int(a.hours), int(a.minutes), int(a.seconds), 0, location)
414414
}
415415

416+
// TimeFromAtTime is a helper function to allow converting AtTime into a time.Time value
417+
// Note: the time.Time value will have zero values for all Time fields except Hours, Minutes, Seconds.
418+
//
419+
// For example: time.Date(0, 0, 0, 1, 1, 1, 0, time.UTC)
420+
func TimeFromAtTime(at AtTime, loc *time.Location) time.Time {
421+
return at().time(loc)
422+
}
423+
416424
// AtTime defines a function that returns the internal atTime
417425
type AtTime func() atTime
418426

job_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -676,3 +676,51 @@ func TestJob_PanicOccurred(t *testing.T) {
676676
close(gotCh)
677677
close(errCh)
678678
}
679+
680+
func TestTimeFromAtTime(t *testing.T) {
681+
testTimeUTC := time.Date(0, 0, 0, 1, 1, 1, 0, time.UTC)
682+
cst, err := time.LoadLocation("America/Chicago")
683+
require.NoError(t, err)
684+
testTimeCST := time.Date(0, 0, 0, 1, 1, 1, 0, cst)
685+
686+
tests := []struct {
687+
name string
688+
at AtTime
689+
loc *time.Location
690+
expectedTime time.Time
691+
expectedStr string
692+
}{
693+
{
694+
"UTC",
695+
NewAtTime(
696+
uint(testTimeUTC.Hour()),
697+
uint(testTimeUTC.Minute()),
698+
uint(testTimeUTC.Second()),
699+
),
700+
time.UTC,
701+
testTimeUTC,
702+
"01:01:01",
703+
},
704+
{
705+
"CST",
706+
NewAtTime(
707+
uint(testTimeCST.Hour()),
708+
uint(testTimeCST.Minute()),
709+
uint(testTimeCST.Second()),
710+
),
711+
cst,
712+
testTimeCST,
713+
"01:01:01",
714+
},
715+
}
716+
717+
for _, tt := range tests {
718+
t.Run(tt.name, func(t *testing.T) {
719+
result := TimeFromAtTime(tt.at, tt.loc)
720+
assert.Equal(t, tt.expectedTime, result)
721+
722+
resultFmt := result.Format("15:04:05")
723+
assert.Equal(t, tt.expectedStr, resultFmt)
724+
})
725+
}
726+
}

0 commit comments

Comments
 (0)