-
-
Notifications
You must be signed in to change notification settings - Fork 396
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
Add Duration.isoformat() #522
base: master
Are you sure you want to change the base?
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? |
@@ -175,3 +175,22 @@ It also has a handy `in_words()` method, which determines the duration represent | |||
>>> it.in_words(locale='de') | |||
'168 Wochen 1 Tag 2 Stunden 1 Minute 24 Sekunden' | |||
``` | |||
|
|||
Finally, it has an | |||
[ISO-8601-compliant](https://en.wikipedia.org/wiki/ISO_8601#Durations) `isformat()` |
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 you meant isoformat()
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.