Skip to content
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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

seandstewart
Copy link

Pull Request Check List

  • Added tests for changed code.
  • Updated documentation for changed code.

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.

@seandstewart
Copy link
Author

@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()`
Copy link

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()

@IceN9ne
Copy link

IceN9ne commented Jan 17, 2021

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.

@Evan0000000000
Copy link

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.

@Secrus
Copy link
Collaborator

Secrus commented Jan 3, 2025

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.

Comment on lines +419 to +423
periods = [
("Y", self.years),
("M", self.months),
("D", self.remaining_days),
]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing weeks,

Suggested change
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)

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants