Skip to content

Add Duration.to_iso8601_string()#314

Open
gnattishness wants to merge 1 commit into
python-pendulum:masterfrom
gnattishness:duration_to_iso
Open

Add Duration.to_iso8601_string()#314
gnattishness wants to merge 1 commit into
python-pendulum:masterfrom
gnattishness:duration_to_iso

Conversation

@gnattishness

Copy link
Copy Markdown

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

Fixes #306

@gnattishness

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Author

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

@jordiju

jordiju commented May 3, 2019

Copy link
Copy Markdown

Have been looking for this. Would be very useful.

@adsultana

Copy link
Copy Markdown

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

@martsa1

martsa1 commented Jun 4, 2019

Copy link
Copy Markdown

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

gnattishness commented Jun 9, 2019

Copy link
Copy Markdown
Author

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

zeebonk commented Aug 20, 2019

Copy link
Copy Markdown

Nice work! Would love to have this feature available!

@KevinKobi

Copy link
Copy Markdown

is this going to be released?

@gnattishness

Copy link
Copy Markdown
Author

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

@jacobg

jacobg commented Aug 20, 2020

Copy link
Copy Markdown

Any update on this?

@gnattishness

Copy link
Copy Markdown
Author

Not from my end 🙂

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

hozn commented Mar 9, 2023

Copy link
Copy Markdown

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
Copy Markdown

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