-
Notifications
You must be signed in to change notification settings - Fork 25
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
Improve time functions #539
Conversation
This PR changes name of the toTime template function to formatTime and adds toTime and mustToTime, which parse a string and convert it to a time, and toLayout that converts strings like time.RFC3339 to the actual layout string.
735b6bf
to
edbd11e
Compare
edbd11e
to
ca6ad2a
Compare
internal/templates/funcmap.go
Outdated
m["toTime"] = toTime | ||
m["parseTime"] = parseTime | ||
m["mustParseTime"] = mustParseTime | ||
m["toLayout"] = toLayout |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The functionality of the function is specific to times/dates, and the other new functions have this in their name. Maybe it should be called toTimeLayout / toDateLayout (or support both)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be toTimeLayout "RFC8222"
instead of toLayout "time.RFC8222"
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
internal/templates/funcmap.go
Outdated
// functions "formatTime", "toTime", "mustToTime", and changes the function | ||
// "fail". |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want to add toLayout
(or its updated name(s)) here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed with a23cb16
internal/templates/funcmap.go
Outdated
// time using RFC3339. The functions "parseTime" and "mustParseTime" parse a | ||
// string and return the time.Time it represents. The "toLayout" function | ||
// converts strings like "time.RFC3339" or "time.UnixDate" to the actual layout | ||
// represented by the Go constant with the same nameß. The "fail" function sets |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// represented by the Go constant with the same nameß. The "fail" function sets | |
// represented by the Go constant with the same name. The "fail" function sets |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed with a23cb16
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor typo; potential change. But otherwise good 😄
internal/templates/funcmap.go
Outdated
return time.RFC3339 | ||
case "RFC3339Nano": | ||
return time.RFC3339Nano | ||
// From the ones bellow, only time.DateTime will parse a complete date. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// From the ones bellow, only time.DateTime will parse a complete date. | |
// From the ones below, only time.DateTime will parse a complete date. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed with 329b3f1
internal/templates/funcmap.go
Outdated
switch strings.TrimPrefix(fmt, "time.") { | ||
case "Layout": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for convenience for the user, we could allow any case, but up to you
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed with 329b3f1
This commit upgrades go.step.sm/crypto that includes the template functions added with smallstep/crypto#539
Description
This PR improves the functions added in #534 with some usability changes.
toTime 1719970524
returns atime.Time
in UTC from a time or a Unix epoch.formatTime (now)
: from a time or Unix epoch returns a time string using RFC3339 format UTC.parseTime
andmustParseTime
:parseTime
: returns the currenttime.Time
in UTC.parseTime "2024-07-03T01:30:39Z"
: parses the given time using RFC3339.parseTime "time.UnixDate" "Tue Jul 2 18:31:04 PDT 2024"
: parses a time using a given format.parseTime "time.UnixDate" "Tue Jul 2 18:31:04 PDT 2024" "America/Los_Angeles"
: parses a time with the given format and time zone.toTimeLayout
returns a layout format from the given name.toTimeLayout "RFC3339"
returns2006-01-02T15:04:05Z07:00
toTimeLayout "time.UnixDate"
returnsMon Jan _2 15:04:05 MST 2006