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.

Issue #1508

Change-Id: I1d80aab593a7e8b18dd500d73aef12fb9e5bfa4b
Signed-off-by: Marcel van Lohuizen <[email protected]>
  • Loading branch information
mpvl committed Feb 19, 2022
1 parent 487ed1d commit 2c6b14d
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
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
}
}

46 changes: 46 additions & 0 deletions pkg/time/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
// limitations under the License.

// Package time defines time-related types.
//
// In CUE time values are represented as a string of the format
// time.RFC3339Nano.
package time

import (
Expand Down Expand Up @@ -146,6 +149,17 @@ func Format(value, layout string) (bool, error) {
return timeFormat(value, layout)
}

// FormatString returns a textual representation of the time value.
// The formatted value is 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 +209,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 2c6b14d

Please sign in to comment.