Skip to content

Add Duration.to_iso8601_string() #314

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gnattishness
Copy link

Add a Duration.to_iso8601_string() method to output a standalone duration as an ISO8601 string.

Fixes #306

@gnattishness
Copy link
Author

Still some work in progress:

  • Need to handle negative duration values
  • Add tests for negative durations

I have yet to find any info on whether the standard allows for durations like P10M-1D (which seems like it could be useful).

Haven't purchased the standard, so not sure if present in the full standard.
This has a BNF, but doesn't define week, hour, month in it so no help there.
https://www.loc.gov/standards/datetime/iso-tc154-wg5_n0039_iso_wd_8601-2_2016-02-16.pdf

@gnattishness
Copy link
Author

Not too sure what's happening with the failing tests. I'll have a look at it in a bit, but am not too familiar with pypy or python 2.7, so would appreciate any thoughts.

@gnattishness
Copy link
Author

Turns out the bug was caused by round returning a float instead of an int in python2, so duration.__init__() was saving microseconds as a float.

Add relevant tests.

Skip test that can't succed with current duration implementation.

Fixed bug caused by microseconds being stored as floats on python2.

Update CHANGELOG.
@gnattishness
Copy link
Author

@sdispater I'm still not sure how/if negative durations should be handled, but this should otherwise be good to go.

I.e. Would "P1M-1D" or "-P5D" be valid ISO8601 duration strings?

I don't have the full standard, but what I've seen doesn't mention whether negative durations or duration components are possible.

Looks like Moment.js has encountered similar:
moment/moment#2408
moment/moment#2955

How should I proceed? Happy to add support for negative durations if you'd like.

@gnattishness
Copy link
Author

Hi @sdispater, I would appreciate your thoughts if you have a chance.

@jordiju
Copy link

jordiju commented May 3, 2019

Have been looking for this. Would be very useful.

@adsultana
Copy link

I've also stumbled upon this as a nice-to-have feature

@martsa1
Copy link

martsa1 commented Jun 4, 2019

I just bumped into this, would really appreciate it getting merged soon!

If there's any help I can add, testing/debugging etc. please shout.

@gnattishness
Copy link
Author

gnattishness commented Jun 9, 2019

Thanks from the bumps, just waiting on some input from @sdispater or another authority on how negative durations should be handled.
There's a few other PRs pending, so happy to fix any conflicts if you'd like to accept other PRs first

@zeebonk
Copy link

zeebonk commented Aug 20, 2019

Nice work! Would love to have this feature available!

@KevinKobi
Copy link

is this going to be released?

@gnattishness
Copy link
Author

I'm still waiting on a response and some input from @sdispater
Happy to rebase and fix conflicts after that.

@jacobg
Copy link

jacobg commented Aug 20, 2020

Any update on this?

@gnattishness
Copy link
Author

Not from my end 🙂

@IceN9ne IceN9ne mentioned this pull request Jan 17, 2021
2 tasks
@hozn
Copy link

hozn commented Mar 9, 2023

Would also love to see this. It sounds like there isn't work needed other than decision on negative timestamps? @sdispater is this under consideration?

@jgogstad
Copy link

this fails

assert pendulum.parse(pendulum.parse("P1Y").to_iso8601_string()) == pendulum.parse("1Y")

it gives

pendulum.parsing.exceptions.ParserError: Unable to parse string [P1Y-365D]

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.

Format duration to ISO 8601 string
9 participants