Skip to content

Commit

Permalink
pkg/time: add FormatString and Split
Browse files Browse the repository at this point in the history
Down the line, FormatString should replace Format
with new semantics. It is added now as FormatString
to aid in the transition.

Closes #1508

Change-Id: I1d80aab593a7e8b18dd500d73aef12fb9e5bfa4b
Signed-off-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Feb 18, 2022
1 parent afbbbb2 commit 6f21790
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
25 changes: 25 additions & 0 deletions pkg/time/pkg.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions pkg/time/testdata/gen.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ parse: {
t2: time.Parse(_layout, _layout)
}

split: {
t1: time.Split("2017-07-14T02:40:00.000123456Z")
}
-- out/time --
Errors:
t2: invalid value "no time" (does not satisfy time.Time): error in call to time.Time: invalid time "no time":
Expand All @@ -32,4 +35,15 @@ parse: {
t1: "2021-07-01T17:54:00Z"
t2: "2006-01-02T22:04:05Z"
}
split: {
t1: {
year: 2017
month: 7
day: 14
hour: 2
minute: 40
second: 0
nanosecond: 123456
}
}

45 changes: 44 additions & 1 deletion pkg/time/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,24 @@ func timeFormat(value, layout string) (bool, error) {
return true, nil
}

// Format defines a type string that must adhere to a certain layout.
// FormatString defines a type string that must adhere to a certain layout.
//
// See Parse for a description on layout strings.
func Format(value, layout string) (bool, error) {
return timeFormat(value, layout)
}

// FormatString returns a textual representation of the time value formatted
// according to the layout defined by the argument. See Parse for more
// information on the layout string.
func FormatString(layout, value string) (string, error) {
t, err := time.Parse(time.RFC3339Nano, value)
if err != nil {
return "", err
}
return t.Format(layout), nil
}

// Parse parses a formatted string and returns the time value it represents.
// The layout defines the format by showing how the reference time,
// defined to be
Expand Down Expand Up @@ -195,3 +206,35 @@ func Unix(sec int64, nsec int64) string {
t := time.Unix(sec, nsec)
return t.UTC().Format(time.RFC3339Nano)
}

// Parts holds individual parts of a parsed time stamp.
type Parts struct {
Year int `json:"year"`
Month int `json:"month"`
Day int `json:"day"`
Hour int `json:"hour"`
Minute int `json:"minute"`

// Second is equal to div(Nanosecond, 1_000_000_000)
Second int `json:"second"`
Nanosecond int `json:"nanosecond"`
}

// Split parses a time string into its individual parts.
func Split(t string) (*Parts, error) {
st, err := time.Parse(time.RFC3339Nano, t)
if err != nil {
return nil, err
}
year, month, day := st.Date()
return &Parts{
Year: year,
Month: int(month),
Day: day,
Hour: st.Hour(),
Minute: st.Minute(),

Second: st.Second(),
Nanosecond: st.Nanosecond(),
}, nil
}

0 comments on commit 6f21790

Please sign in to comment.