-
Notifications
You must be signed in to change notification settings - Fork 6
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
Allow empty location as no location info #12
Conversation
@nasermirzaei89 Do you feel equipped to add a unit test for this case? (I'm fine with the change but want to make sure we have a test that FAILS without this change but passes with it.) |
OK, I'll add the test. |
convert.go
Outdated
@@ -115,11 +115,13 @@ func FromString(s string) (Date, error) { | |||
// FromTime validates that a `time.Time{}` contains a date and converts it to a | |||
// `Date{}`. | |||
func FromTime(t time.Time) (Date, error) { | |||
hasLocationInfo := !(t.Location() == time.UTC || t.Location().String() == "") |
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.
Given the naming, wouldn't it make sense for this to be
hasLocationInfo := t.Location() == time.UTC || t.Location().String() == ""
and then use !hasLocationInfo
on line 124?
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.
and then the naming doesn't make sense.
I can make it inline and remove the variable. Shall I do that?
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.
Oh I was thinking about it wrong, I get it now!
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.
I think the failing test showcases an issue with this approach
-timestamp contains more than just date information; 2022-01-31T00:00:00-05:00
+<nil>
In particular, the test case ends up resulting in a time.FixedZone("", -5*60*60)
which isn't a "zero offset" even though it has a name equal to ""
. Note that time.Location{}.String()
just returns the name and does not mention the offset.
Do you have an example value deserialized by lib/pq
? Could you fmt.Printf("%#v\n", tz)
the value and peek at the internal fields?
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.
Actually t.Zone()
may do the trick for us here
@nasermirzaei89 Testing this is quirkier than I realized! I starting experimenting with I'm sending a PR with test-only changes, then your feature will be easier to test and add tests for. |
@nasermirzaei89 #13 will merge shortly and then you can merge it in / rebase on top of it |
@dhermes as the input for the function is time.Time, this field shouldn't be time.Time rather than string? |
@nasermirzaei89 The input is intentionally But makes it impossible to handle named timezones, so I added that in #13. |
@nasermirzaei89 So sorry, I wasn't intending for you to refactor the existing tests! Definitely not the direction I want to take that test in. Sorry we didn't have a chance to discuss before you put that time in. As for your code itself, you'll need to tweak it to using |
13d5b4b
to
f089e0e
Compare
The issue is that it's a timezone with no name, so the |
I've updated the code to use zone and offset. However, since |
All good, I can sort out how to test from here! |
Package github.com/lib/pq converts Postgres type "Date" to
time.Time
with zero value location, notnil
.So, scanning it by
date.Date
returns error.However I believe this is an issue on
github.com/lib/pq
, we can add this patch todate.Date
.Regarding the nil location, golang itself already handles this and returns UTC.