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

fix: parse time function for nedlib #139

Merged
merged 1 commit into from
May 16, 2024
Merged

fix: parse time function for nedlib #139

merged 1 commit into from
May 16, 2024

Conversation

trym-b
Copy link
Contributor

@trym-b trym-b commented Apr 16, 2024

Motivation

nedlib data might contain a lot of different
time formats. This commit adds a function to parse the time format used in nedlib data.

Future work

Add parsing of custom time formats.

return
}

err = fmt.Errorf("failed to parse string as time.Time: %s: %v", dateString, err)
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
err = fmt.Errorf("failed to parse string as time.Time: %s: %v", dateString, err)
err = fmt.Errorf("failed to parse string as time.Time: %s: %w", dateString, err)

Wrap error to be able to use errors.Is or errors.As.

Copy link
Member

Choose a reason for hiding this comment

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

This function will return the error message from the latest attempt which is a bit misleading considering we are trying different layouts.

Maybe we should return

time.ParseError{
    Message: ": cannot parse as [list of formats?]" 
}

considering:

// Error returns the string representation of a ParseError.
func (e *ParseError) Error() string {
	if e.Message == "" {
		return "parsing time " +
			quote(e.Value) + " as " +
			quote(e.Layout) + ": cannot parse " +
			quote(e.ValueElem) + " as " +
			quote(e.LayoutElem)
	}
	return "parsing time " +
		quote(e.Value) + e.Message
}

ref: https://cs.opensource.google/go/go/+/refs/tags/go1.22.2:src/time/format.go;drc=3ad6393f8676b1b408673bf40b8a876f29561eef;l=885

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The error message is misleading, I agree. We should probably improve it, but right now the current implementation of this PR is better than what exists in main.

I added a fixup to fix the error handling in the invalid time format cases, so now we only check for the type, not the actual error message.


func TestParseInvalidRFC1123(t *testing.T) {
dateString := "Tue, 05 Apr 2024 15:30:00"
expectedError := "failed to parse string as time.Time: Tue, 05 Apr 2024 15:30:00: parsing time \"Tue, 05 Apr 2024 15:30:00\" as \"Mon Jan _2 15:04:05 MST 2006\": cannot parse \", 05 Apr 2024 15:30:00\" as \" \""
Copy link
Member

Choose a reason for hiding this comment

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

Better to compare error against time.ParseError using errors.As or errors.Is.

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 in a fixup

@trym-b
Copy link
Contributor Author

trym-b commented Apr 17, 2024

Ready for next round @maeb

@trym-b
Copy link
Contributor Author

trym-b commented May 3, 2024

After an IRL review with @maeb, we decided to return an additional argument from parseTime that tells the user what time format was parsed.
Also, we decided to rewrite the test into a table instead of individual tests, although we will lose the reasoning behind each of the test cases. Both positive and negative test cases goes into this table.

@trym-b
Copy link
Contributor Author

trym-b commented May 3, 2024

Ready for next round @maeb

nedlibreader/time_test.go Show resolved Hide resolved
# Motivation

`nedlib` data might contain a lot of different
time formats. This commit adds a function to parse
the time format used in `nedlib` data.

# Future work

Add parsing of custom time formats.
@trym-b trym-b merged commit 167042a into main May 16, 2024
8 checks passed
@trym-b trym-b deleted the fix/convert-time branch May 16, 2024 05:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants