Add Duration.isoformat()#522
Conversation
|
@sdispater looks like the only failures left are on Python2.7 and unrelated to my changes. Any thoughts on this feature? |
| ``` | ||
|
|
||
| Finally, it has an | ||
| [ISO-8601-compliant](https://en.wikipedia.org/wiki/ISO_8601#Durations) `isformat()` |
|
I would really like this feature, but it seems that even the competing PR #314 released almost 2 years prior to this one has no dev comments as well. Hoping one of these two makes it in eventually. I did notice this PR doesn't drop the unnecessary portions of the string for 0 values, which makes the resulting format quite verbose. |
|
Is there some hold up on this? Lack of interest? Given how Pendulum has a formatter for datetime objects it seems strange there's not at least some nod towards providing one for durations. |
The commits in this PR are 4+ years old at this point and significantly out of date with the current state of the codebase. If you are interested, feel free to make a new PR for this feature, superseding this one. |
| periods = [ | ||
| ("Y", self.years), | ||
| ("M", self.months), | ||
| ("D", self.remaining_days), | ||
| ] |
There was a problem hiding this comment.
missing weeks,
| periods = [ | |
| ("Y", self.years), | |
| ("M", self.months), | |
| ("D", self.remaining_days), | |
| ] | |
| periods = [ | |
| ("Y", self.years), | |
| ("M", self.months), | |
| ("W", self.weeks), | |
| ("D", self.remaining_days), | |
| ] |
| ] | ||
| period = "P" | ||
| for sym, val in periods: | ||
| period += "{val}{sym}".format(val=val, sym=sym) |
There was a problem hiding this comment.
if you only keep the components that are set, .isoformat() becomes a proper inverse transformation of str → Duration
assert pendulum.parse("P1W").isoformat() == "P1W"need same change below obviously
Pull Request Check List
This adds a simple method to represent Durations in ISO format. Given that pendulum can parse ISO 8601 strings into Durations, it makes sense that we'd be able to also serialize the other way.