ref(time): Adds wrapper for most time stdlib methods

Any method that had a function parameter that was a `Time` or returned a
`Time` is now wrapped so you can use our time wrapper without any weird conventions

Signed-off-by: Taylor Thomas <taylor.thomas@microsoft.com>
pull/6679/head
Taylor Thomas 5 years ago
parent aa429e150a
commit 4d7968f692

@ -22,7 +22,6 @@ import (
"os"
"strings"
"testing"
stdtime "time"
shellwords "github.com/mattn/go-shellwords"
"github.com/spf13/cobra"
@ -37,7 +36,7 @@ import (
"helm.sh/helm/v3/pkg/time"
)
func testTimestamper() time.Time { return time.Time{Time: stdtime.Unix(242085845, 0).UTC()} }
func testTimestamper() time.Time { return time.Unix(242085845, 0).UTC() }
func init() {
action.Timestamper = testTimestamper

@ -18,7 +18,6 @@ package main
import (
"testing"
stdtime "time"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/release"
@ -29,10 +28,10 @@ func TestListCmd(t *testing.T) {
defaultNamespace := "default"
sampleTimeSeconds := int64(1452902400)
timestamp1 := time.Time{Time: stdtime.Unix(sampleTimeSeconds+1, 0).UTC()}
timestamp2 := time.Time{Time: stdtime.Unix(sampleTimeSeconds+2, 0).UTC()}
timestamp3 := time.Time{Time: stdtime.Unix(sampleTimeSeconds+3, 0).UTC()}
timestamp4 := time.Time{Time: stdtime.Unix(sampleTimeSeconds+4, 0).UTC()}
timestamp1 := time.Unix(sampleTimeSeconds+1, 0).UTC()
timestamp2 := time.Unix(sampleTimeSeconds+2, 0).UTC()
timestamp3 := time.Unix(sampleTimeSeconds+3, 0).UTC()
timestamp4 := time.Unix(sampleTimeSeconds+4, 0).UTC()
chartInfo := &chart.Chart{
Metadata: &chart.Metadata{
Name: "chickadee",

@ -27,7 +27,7 @@ import (
func TestStatusCmd(t *testing.T) {
releasesMockWithStatus := func(info *release.Info, hooks ...*release.Hook) []*release.Release {
info.LastDeployed = time.Time{Time: stdtime.Unix(1452902400, 0).UTC()}
info.LastDeployed = time.Unix(1452902400, 0).UTC()
return []*release.Release{{
Name: "flummoxed-chickadee",
Namespace: "default",
@ -105,6 +105,6 @@ func TestStatusCmd(t *testing.T) {
}
func mustParseTime(t string) time.Time {
res, _ := stdtime.Parse(stdtime.RFC3339, t)
return time.Time{Time: res}
res, _ := time.Parse(stdtime.RFC3339, t)
return res
}

@ -18,7 +18,6 @@ package release
import (
"math/rand"
stdtime "time"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/time"
@ -50,7 +49,7 @@ type MockReleaseOptions struct {
// Mock creates a mock release object based on options set by MockReleaseOptions. This function should typically not be used outside of testing.
func Mock(opts *MockReleaseOptions) *Release {
date := time.Time{Time: stdtime.Unix(242085845, 0).UTC()}
date := time.Unix(242085845, 0).UTC()
name := opts.Name
if name == "" {

@ -34,7 +34,7 @@ var releases = []*rspb.Release{
}
func tsRelease(name string, vers int, dur stdtime.Duration, status rspb.Status) *rspb.Release {
info := &rspb.Info{Status: status, LastDeployed: time.Time{Time: stdtime.Now().Add(dur)}}
info := &rspb.Info{Status: status, LastDeployed: time.Now().Add(dur)}
return &rspb.Release{
Name: name,
Version: vers,

@ -60,3 +60,32 @@ func (t *Time) UnmarshalJSON(b []byte) error {
return t.Time.UnmarshalJSON(b)
}
func Parse(layout, value string) (Time, error) {
t, err := time.Parse(layout, value)
return Time{Time: t}, err
}
func ParseInLocation(layout, value string, loc *time.Location) (Time, error) {
t, err := time.ParseInLocation(layout, value, loc)
return Time{Time: t}, err
}
func Date(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) Time {
return Time{Time: time.Date(year, month, day, hour, min, sec, nsec, loc)}
}
func Unix(sec int64, nsec int64) Time { return Time{Time: time.Unix(sec, nsec)} }
func (t Time) Add(d time.Duration) Time { return Time{Time: t.Time.Add(d)} }
func (t Time) AddDate(years int, months int, days int) Time {
return Time{Time: t.Time.AddDate(years, months, days)}
}
func (t Time) After(u Time) bool { return t.Time.After(u.Time) }
func (t Time) Before(u Time) bool { return t.Time.Before(u.Time) }
func (t Time) Equal(u Time) bool { return t.Time.Equal(u.Time) }
func (t Time) In(loc *time.Location) Time { return Time{Time: t.Time.In(loc)} }
func (t Time) Local() Time { return Time{Time: t.Time.Local()} }
func (t Time) Round(d time.Duration) Time { return Time{Time: t.Time.Round(d)} }
func (t Time) Sub(u Time) time.Duration { return t.Time.Sub(u.Time) }
func (t Time) Truncate(d time.Duration) Time { return Time{Time: t.Time.Truncate(d)} }
func (t Time) UTC() Time { return Time{Time: t.Time.UTC()} }

@ -23,13 +23,12 @@ import (
)
var (
testingTime, _ = time.Parse(time.RFC3339, "1977-09-02T22:04:05Z")
testingTime, _ = Parse(time.RFC3339, "1977-09-02T22:04:05Z")
testingTimeString = `"1977-09-02T22:04:05Z"`
)
func TestNonZeroValueMarshal(t *testing.T) {
myTime := Time{testingTime}
res, err := json.Marshal(myTime)
res, err := json.Marshal(testingTime)
if err != nil {
t.Fatal(err)
}

Loading…
Cancel
Save