-
Notifications
You must be signed in to change notification settings - Fork 183
Add dyn.Time to box a timestamp with its original string value
#1732
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package dyn | ||
|
|
||
| import "time" | ||
|
|
||
| // Time represents a time-like primitive value. | ||
| // | ||
| // It represents a timestamp and includes the original string value | ||
| // that was parsed to create the timestamp. This makes it possible | ||
| // to coalesce a value that YAML interprets as a timestamp back into | ||
| // a string without losing information. | ||
| type Time struct { | ||
| t time.Time | ||
| s string | ||
| } | ||
|
|
||
| // NewTime creates a new Time from the given string. | ||
| func NewTime(str string) (Time, bool) { | ||
| // Try a couple of layouts | ||
| for _, layout := range []string{ | ||
| "2006-1-2T15:4:5.999999999Z07:00", // RCF3339Nano with short date fields. | ||
| "2006-1-2t15:4:5.999999999Z07:00", // RFC3339Nano with short date fields and lower-case "t". | ||
| "2006-1-2 15:4:5.999999999", // space separated with no time zone | ||
| "2006-1-2", // date only | ||
| } { | ||
| t, terr := time.Parse(layout, str) | ||
| if terr == nil { | ||
| return Time{t: t, s: str}, true | ||
| } | ||
| } | ||
|
|
||
| return Time{}, false | ||
| } | ||
|
|
||
| // MustTime creates a new Time from the given string. | ||
| // It panics if the string cannot be parsed. | ||
| func MustTime(str string) Time { | ||
| t, ok := NewTime(str) | ||
| if !ok { | ||
| panic("invalid time") | ||
| } | ||
| return t | ||
| } | ||
|
|
||
| // FromTime creates a new Time from the given time.Time. | ||
| // It uses the RFC3339Nano format for its string representation. | ||
| // This guarantees that it can roundtrip into a string without losing information. | ||
| func FromTime(t time.Time) Time { | ||
| return Time{t: t, s: t.Format(time.RFC3339Nano)} | ||
| } | ||
|
|
||
| // Time returns the time.Time value. | ||
| func (t Time) Time() time.Time { | ||
| return t.t | ||
| } | ||
|
|
||
| // String returns the original string value that was parsed to create the timestamp. | ||
| func (t Time) String() string { | ||
| return t.s | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package dyn_test | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/databricks/cli/libs/dyn" | ||
| assert "github.com/databricks/cli/libs/dyn/dynassert" | ||
| ) | ||
|
|
||
| func TestTimeValid(t *testing.T) { | ||
| for _, tc := range []string{ | ||
| "2024-08-29", | ||
| "2024-01-15T12:34:56.789012345Z", | ||
| } { | ||
| tm, ok := dyn.NewTime(tc) | ||
| if assert.True(t, ok) { | ||
| assert.NotEqual(t, time.Time{}, tm.Time()) | ||
| assert.Equal(t, tc, tm.String()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestTimeInvalid(t *testing.T) { | ||
| tm, ok := dyn.NewTime("invalid") | ||
| assert.False(t, ok) | ||
| assert.Equal(t, dyn.Time{}, tm) | ||
| } | ||
|
|
||
| func TestTimeFromTime(t *testing.T) { | ||
| tref := time.Now() | ||
| t1 := dyn.FromTime(tref) | ||
|
|
||
| // Verify that the underlying value is the same. | ||
| assert.Equal(t, tref, t1.Time()) | ||
|
|
||
| // Verify that the string representation can be used to construct the same. | ||
| t2, ok := dyn.NewTime(t1.String()) | ||
| assert.True(t, ok) | ||
| assert.True(t, t1.Time().Equal(t2.Time())) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can we log a message here saying that format that was used is not supported?
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.
Thanks, done. It now returns an
errorthat says as much.