Skip to content
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

Merged
merged 6 commits into from
Jul 3, 2024
Merged

Improve time functions #539

merged 6 commits into from
Jul 3, 2024

Conversation

maraino
Copy link
Contributor

@maraino maraino commented Jul 2, 2024

Description

This PR improves the functions added in #534 with some usability changes.

  • toTime 1719970524 returns a time.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 and mustParseTime:
    • parseTime: returns the current time.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" returns 2006-01-02T15:04:05Z07:00
    • toTimeLayout "time.UnixDate" returns Mon Jan _2 15:04:05 MST 2006

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.
@maraino maraino requested a review from hslatman July 2, 2024 03:29
internal/templates/funcmap.go Outdated Show resolved Hide resolved
internal/templates/funcmap.go Outdated Show resolved Hide resolved
@maraino maraino force-pushed the mariano/timeFunctions branch 2 times, most recently from 735b6bf to edbd11e Compare July 3, 2024 02:09
@maraino maraino force-pushed the mariano/timeFunctions branch from edbd11e to ca6ad2a Compare July 3, 2024 02:11
@maraino maraino marked this pull request as ready for review July 3, 2024 02:16
@maraino maraino requested review from hslatman and tashian July 3, 2024 02:16
m["toTime"] = toTime
m["parseTime"] = parseTime
m["mustParseTime"] = mustParseTime
m["toLayout"] = toLayout
Copy link
Member

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)?

Copy link

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"?

Copy link
Contributor Author

@maraino maraino Jul 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed with a23cb16 and a87b577

Comment on lines 14 to 15
// functions "formatTime", "toTime", "mustToTime", and changes the function
// "fail".
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed with a23cb16

// 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed with a23cb16

@maraino maraino requested a review from hslatman July 3, 2024 18:04
Copy link
Member

@hslatman hslatman left a 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 😄

return time.RFC3339
case "RFC3339Nano":
return time.RFC3339Nano
// From the ones bellow, only time.DateTime will parse a complete date.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// From the ones bellow, only time.DateTime will parse a complete date.
// From the ones below, only time.DateTime will parse a complete date.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed with 329b3f1

Comment on lines 116 to 117
switch strings.TrimPrefix(fmt, "time.") {
case "Layout":
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed with 329b3f1

@maraino maraino requested a review from hslatman July 3, 2024 20:40
@maraino maraino merged commit 446f4e2 into master Jul 3, 2024
13 checks passed
@maraino maraino deleted the mariano/timeFunctions branch July 3, 2024 20:47
maraino added a commit to smallstep/certificates that referenced this pull request Jul 3, 2024
This commit upgrades go.step.sm/crypto that includes the template
functions added with smallstep/crypto#539
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants