date: period: P7D is normalised to P1W even when normalisation is not set
I’m trying to parse the period P7D and I don’t want any normalization.
p, _ := period.Parse("P7D", false)
However, when I print this I get P1W.
It seems like this happens due to the code in the String() function:
https://github.com/rickb777/date/blob/d2bc4afd7cd27d07a25daa88f0ed92822a1ec19e/period/format.go#L107-L113
To me, this doesn’t seem right. I believe that since normalize is deactivated should the result be P7D.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 2
- Comments: 27 (10 by maintainers)
Fair point. It’s an easy enough change and probably won’t affect people adversely because both forms are equivalent.
What about instead doing:
Drop
NewYMWDandNew2as they are initializers for an illegal format.PS! As far as we are concerned (me and @khtufteland), the only purpose of this issue is to prevent automatic normalization into weeks. Direct week support is not something we care about. We have switched to our own implementation now that fits our exact needs though.
As for everyone else proper direct week support would probably only allow
weeksto be non-zero if you have zero date type and zero time components. E.g. imagine:I agree this looks very strange. Perhaps to support sub-second precision without a nsec parameter @rickb777?
Of topic, but have to mention it.
IsPositiveappear to ignore the case where the periodIsZero. Would it not be easier to return!p.IsNegative()? Then it should also cover theIsZerocase.@rickb777 oh, you’re alive! That’s a good sign 😆