Skip to content

add String API for time #130360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
8 changes: 8 additions & 0 deletions staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ func (t *Time) Before(u *Time) bool {
return false
}

// String returns the time formatted using the format string
func (t *Time) String() string {
if t == nil {
return ""
}
return t.Time.String()
}

// Equal reports whether the time instant t is equal to u.
func (t *Time) Equal(u *Time) bool {
if t == nil && u == nil {
Expand Down
20 changes: 20 additions & 0 deletions staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"fmt"
"reflect"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -258,6 +259,25 @@ func TestTimeEqual(t *testing.T) {
}
}

func TestTimeString(t *testing.T) {
cases := []struct {
name string
input *Time
result string
}{
{"nil", nil, ""},
{"empty", &Time{}, "0001-01-01 00:00:00 +0000 UTC"},
{"time", &Time{Time: time.Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC)}, "1998-05-05 05:05:05 +0000 UTC"},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
if !strings.EqualFold(c.input.String(), c.result) {
t.Errorf("Failed equality: input '%v': expected %+v, got %+v", c.input, c.result, c.input.String())
}
})
}
}

func TestTimeBefore(t *testing.T) {
t1 := NewTime(time.Now())
cases := []struct {
Expand Down