Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions date.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package sprig

import (
"errors"
"strconv"
"time"

"github.com/spf13/cast"
)

// Given a format and a date, format the date string.
Expand Down Expand Up @@ -150,3 +153,47 @@ func mustToDate(fmt, str string) (time.Time, error) {
func unixEpoch(date time.Time) string {
return strconv.FormatInt(date.Unix(), 10)
}

func timeRound(duration interface{}, date interface{}) (time.Time, error) {
var d time.Duration
switch v := duration.(type) {
case time.Duration:
d = v
case *time.Duration:
if v == nil {
return time.Time{}, errors.New("duration cannot be nil")
}
d = *v
case string:
var err error
d, err = time.ParseDuration(v)
if err != nil {
return time.Time{}, err
}
default:
dur, err := cast.ToDurationE(v)
if err != nil {
return time.Time{}, err
}
d = dur
}

var t time.Time
switch v := date.(type) {
case time.Time:
t = v
case *time.Time:
if v == nil {
return time.Time{}, errors.New("date cannot be nil")
}
t = *v
default:
var err error
t, err = cast.ToTimeE(v)
if err != nil {
return time.Time{}, err
}
}

return t.Round(d), nil
}
87 changes: 87 additions & 0 deletions date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package sprig
import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestHtmlDate(t *testing.T) {
Expand Down Expand Up @@ -145,3 +148,87 @@ func TestDateMustModifyReturnsErr(t *testing.T) {
t.Error("expected err, got nil")
}
}

func TestTimeRound(t *testing.T) {
tm1, err := time.Parse("2006-01-02 15:04:05", "2023-05-01 10:07:00")
require.NoError(t, err)

tm2, err := time.Parse("2006-01-02 15:04:05", "2023-05-01 10:08:00")
require.NoError(t, err)

tm3, err := time.Parse("2006-01-02 15:04:05", "2023-05-01 10:35:00")
require.NoError(t, err)

tm4, err := time.Parse("2006-01-02 15:04:05", "2023-05-01 10:20:00")
require.NoError(t, err)

// Direct call tests with string duration and time.Time
res, err := timeRound("15m", tm1)
require.NoError(t, err)
assert.Equal(t, "2023-05-01 10:00:00", res.Format("2006-01-02 15:04:05"))

res, err = timeRound("15m", tm2)
require.NoError(t, err)
assert.Equal(t, "2023-05-01 10:15:00", res.Format("2006-01-02 15:04:05"))

res, err = timeRound("1h", tm3)
require.NoError(t, err)
assert.Equal(t, "2023-05-01 11:00:00", res.Format("2006-01-02 15:04:05"))

res, err = timeRound("1h", tm4)
require.NoError(t, err)
assert.Equal(t, "2023-05-01 10:00:00", res.Format("2006-01-02 15:04:05"))

// Direct call with time.Duration
res, err = timeRound(15*time.Minute, tm1)
require.NoError(t, err)
assert.Equal(t, "2023-05-01 10:00:00", res.Format("2006-01-02 15:04:05"))

dur := 15 * time.Minute
res, err = timeRound(&dur, tm1)
require.NoError(t, err)
assert.Equal(t, "2023-05-01 10:00:00", res.Format("2006-01-02 15:04:05"))

// Direct call with pointer to time.Time
res, err = timeRound("15m", &tm1)
require.NoError(t, err)
assert.Equal(t, "2023-05-01 10:00:00", res.Format("2006-01-02 15:04:05"))

// Direct call with string date (RFC3339)
res, err = timeRound("15m", "2023-05-01T10:07:00Z")
require.NoError(t, err)
assert.Equal(t, "2023-05-01 10:00:00", res.UTC().Format("2006-01-02 15:04:05"))

// Direct call with string date (standard layout)
res, err = timeRound("15m", "2023-05-01 10:08:00")
require.NoError(t, err)
assert.Equal(t, "2023-05-01 10:15:00", res.Format("2006-01-02 15:04:05"))

// Template pipeline execution
assert.NoError(t, runtv(`{{ .Time | timeRound "15m" | date "15:04:05" }}`, "10:00:00", map[string]interface{}{"Time": tm1}))
assert.NoError(t, runtv(`{{ .Time | timeRound "15m" | date "15:04:05" }}`, "10:15:00", map[string]interface{}{"Time": tm2}))
assert.NoError(t, runtv(`{{ timeRound "1h" .Time | date "15:04:05" }}`, "11:00:00", map[string]interface{}{"Time": tm3}))
assert.NoError(t, runtv(`{{ time_round "15m" .Time | date "15:04:05" }}`, "10:00:00", map[string]interface{}{"Time": tm1}))

// Template with string date input
assert.NoError(t, runt(`{{ "2023-05-01T10:07:00Z" | timeRound "15m" | date "15:04:05" }}`, "10:00:00"))
assert.NoError(t, runt(`{{ timeRound "1h" "2023-05-01T10:35:00Z" | date "15:04:05" }}`, "11:00:00"))

// Error handling tests
_, err = timeRound("invalid_duration", tm1)
assert.Error(t, err)

_, err = timeRound("15m", "invalid_date")
assert.Error(t, err)

_, err = timeRound((*time.Duration)(nil), tm1)
assert.Error(t, err)

_, err = timeRound("15m", (*time.Time)(nil))
assert.Error(t, err)

// Template execution error on invalid inputs
assert.Error(t, runtv(`{{ .Time | timeRound "invalid" }}`, "", map[string]interface{}{"Time": tm1}))
assert.Error(t, runt(`{{ "invalid_date" | timeRound "15m" }}`, ""))
}

16 changes: 16 additions & 0 deletions docs/date.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ This returns 3mo
durationRound "2400h10m5s"
```

## timeRound

Rounds a given timestamp to the nearest duration multiple. Duration can be a string (e.g. `"15m"`, `"1h"`) or `time.Duration`, and the date can be a `time.Time` or string.

Round the current time to the nearest 15 minutes:

```
now | timeRound "15m"
```

Round to the nearest hour:

```
timeRound "1h" .CreatedAt
```

## unixEpoch

Returns the seconds since the unix epoch for a `time.Time`.
Expand Down
2 changes: 2 additions & 0 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ var genericMap = map[string]interface{}{
"mustDateModify": mustDateModify,
"mustToDate": mustToDate,
"now": time.Now,
"time_round": timeRound,
"timeRound": timeRound,
"toDate": toDate,
"unixEpoch": unixEpoch,

Expand Down