Skip to content

Commit

Permalink
parse time.Time from AtTime (#806)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnRoesler authored Dec 17, 2024
1 parent edb1475 commit c180381
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
8 changes: 8 additions & 0 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
48 changes: 48 additions & 0 deletions job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}

0 comments on commit c180381

Please sign in to comment.