go-openapi / strfmt

openapi toolkit common string formats
Apache License 2.0
102 stars 62 forks source link

Adds IsZero functionality to DateTime #105

Closed bg451 closed 1 year ago

bg451 commented 1 year ago

This PR adds a new function IsZero to the DateTime struct. The goal of this is to satisfy a common interface, IsZeroer, used by go-yaml/yaml marshaller. IsZero already exists on the time.Time object, so this does not introduce any novel implementation.

For reference, here is the documentation note on using IsZero by the (yaml).Marshal function. If you have a struct generated by swagger that use a proto timestamp, a strfmt.DateTime is used. When you try to marshal this to yaml, all DateTime fields are skipped and excluded.

bg451 commented 1 year ago

cc @casualjim

codecov[bot] commented 1 year ago

Codecov Report

Merging #105 (397e96b) into master (a239ff1) will increase coverage by 0.03%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #105      +/-   ##
==========================================
+ Coverage   81.52%   81.56%   +0.03%     
==========================================
  Files          12       12              
  Lines        2019     2023       +4     
==========================================
+ Hits         1646     1650       +4     
  Misses        295      295              
  Partials       78       78              
Impacted Files Coverage Δ
time.go 90.97% <100.00%> (+0.25%) :arrow_up:

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

casualjim commented 1 year ago

This could also use a companion method that considers time.Unix(0,0) to be zero. Go's idea of a mindate is different from other languages.

This seems to be the pattern for it: https://github.com/golang/go/issues/33597#issuecomment-548232450

bg451 commented 1 year ago

This could also use a companion method that considers time.Unix(0,0) to be zero.

@casualjim Hm so turns out this behavior isn't as simple as you'd hope. The equivalency is timezone dependent, e.g. time.Unix(0, 0) != time.Unix(0, 0).UTC() on my laptop. When the locale is UTC, the equivalency check is fine, but this seems pretty prone to cause bugs to due the nuance of it.

casualjim commented 1 year ago

We can do something like a package level method that captures the IsZero predicate. Similar to these vars: https://github.com/go-openapi/strfmt/blob/master/time.go#L83-L88

I think this should be configurable because if you are dealing with javascript or java clients and they send a min date, they should be able to determine what is the Zero date for their deployment

bg451 commented 1 year ago

@casualjim alrighty, updated