Skip to content

Commit 71e9d03

Browse files
committed
pkg/time: add FormatString and Split
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]> Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/533448 Unity-Result: CUEcueckoo <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent e32817a commit 71e9d03

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

Diff for: pkg/time/pkg.go

+25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: pkg/time/testdata/gen.txtar

+14
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ parse: {
2121
t3: time.Parse(_layout, _layout)
2222
}
2323

24+
split: {
25+
t1: time.Split("2017-07-14T02:40:00.000123456Z")
26+
}
2427
-- out/time --
2528
Errors:
2629
t2: invalid value "no time" (does not satisfy time.Time): error in call to time.Time: invalid time "no time":
@@ -36,4 +39,15 @@ parse: {
3639
t2: "2021-02-19T00:00:00Z"
3740
t3: "2006-01-02T22:04:05Z"
3841
}
42+
split: {
43+
t1: {
44+
year: 2017
45+
month: 7
46+
day: 14
47+
hour: 2
48+
minute: 40
49+
second: 0
50+
nanosecond: 123456
51+
}
52+
}
3953

Diff for: pkg/time/time.go

+46
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
// limitations under the License.
1414

1515
// Package time defines time-related types.
16+
//
17+
// In CUE time values are represented as a string of the format
18+
// time.RFC3339Nano.
1619
package time
1720

1821
import (
@@ -146,6 +149,17 @@ func Format(value, layout string) (bool, error) {
146149
return timeFormat(value, layout)
147150
}
148151

152+
// FormatString returns a textual representation of the time value.
153+
// The formatted value is formatted according to the layout defined by the
154+
// argument. See Parse for more information on the layout string.
155+
func FormatString(layout, value string) (string, error) {
156+
t, err := time.Parse(time.RFC3339Nano, value)
157+
if err != nil {
158+
return "", err
159+
}
160+
return t.Format(layout), nil
161+
}
162+
149163
// Parse parses a formatted string and returns the time value it represents.
150164
// The layout defines the format by showing how the reference time,
151165
// defined to be
@@ -196,3 +210,35 @@ func Unix(sec int64, nsec int64) string {
196210
t := time.Unix(sec, nsec)
197211
return t.UTC().Format(time.RFC3339Nano)
198212
}
213+
214+
// Parts holds individual parts of a parsed time stamp.
215+
type Parts struct {
216+
Year int `json:"year"`
217+
Month int `json:"month"`
218+
Day int `json:"day"`
219+
Hour int `json:"hour"`
220+
Minute int `json:"minute"`
221+
222+
// Second is equal to div(Nanosecond, 1_000_000_000)
223+
Second int `json:"second"`
224+
Nanosecond int `json:"nanosecond"`
225+
}
226+
227+
// Split parses a time string into its individual parts.
228+
func Split(t string) (*Parts, error) {
229+
st, err := time.Parse(time.RFC3339Nano, t)
230+
if err != nil {
231+
return nil, err
232+
}
233+
year, month, day := st.Date()
234+
return &Parts{
235+
Year: year,
236+
Month: int(month),
237+
Day: day,
238+
Hour: st.Hour(),
239+
Minute: st.Minute(),
240+
241+
Second: st.Second(),
242+
Nanosecond: st.Nanosecond(),
243+
}, nil
244+
}

0 commit comments

Comments
 (0)