diff --git a/cmd/helm/helm_test.go b/cmd/helm/helm_test.go index 392400a72..934f14f86 100644 --- a/cmd/helm/helm_test.go +++ b/cmd/helm/helm_test.go @@ -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 diff --git a/cmd/helm/list_test.go b/cmd/helm/list_test.go index 80be98b6c..b5833fd72 100644 --- a/cmd/helm/list_test.go +++ b/cmd/helm/list_test.go @@ -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", diff --git a/cmd/helm/status_test.go b/cmd/helm/status_test.go index 4e3cab40f..b5eff833d 100644 --- a/cmd/helm/status_test.go +++ b/cmd/helm/status_test.go @@ -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 } diff --git a/pkg/release/mock.go b/pkg/release/mock.go index 575f2d65a..3abbd574b 100644 --- a/pkg/release/mock.go +++ b/pkg/release/mock.go @@ -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 == "" { diff --git a/pkg/releaseutil/sorter_test.go b/pkg/releaseutil/sorter_test.go index 85d6753e3..5db8bb1d7 100644 --- a/pkg/releaseutil/sorter_test.go +++ b/pkg/releaseutil/sorter_test.go @@ -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, diff --git a/pkg/time/time.go b/pkg/time/time.go index a372e61e9..44f3fedfb 100644 --- a/pkg/time/time.go +++ b/pkg/time/time.go @@ -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()} } diff --git a/pkg/time/time_test.go b/pkg/time/time_test.go index 32a3e685b..20f0f8e29 100644 --- a/pkg/time/time_test.go +++ b/pkg/time/time_test.go @@ -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) }