pendulum icon indicating copy to clipboard operation
pendulum copied to clipboard

Add Duration.to_iso8601_string()

Open gnattishness opened this issue 6 years ago • 15 comments

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

Fixes #306

gnattishness avatar Nov 23 '18 08:11 gnattishness

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 avatar Nov 23 '18 09:11 gnattishness

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 avatar Nov 23 '18 09:11 gnattishness

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.

gnattishness avatar Nov 29 '18 02:11 gnattishness

@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: https://github.com/moment/moment/issues/2408 https://github.com/moment/moment/pull/2955

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

gnattishness avatar Nov 29 '18 03:11 gnattishness

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

gnattishness avatar Apr 16 '19 03:04 gnattishness

Have been looking for this. Would be very useful.

jordiju avatar May 03 '19 05:05 jordiju

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

adsultana avatar May 08 '19 14:05 adsultana

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.

martsa1 avatar Jun 04 '19 10:06 martsa1

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

gnattishness avatar Jun 09 '19 02:06 gnattishness

Nice work! Would love to have this feature available!

zeebonk avatar Aug 20 '19 13:08 zeebonk

is this going to be released?

KevinKobi avatar Apr 17 '20 11:04 KevinKobi

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

gnattishness avatar Apr 24 '20 02:04 gnattishness

Any update on this?

jacobg avatar Aug 20 '20 17:08 jacobg

Not from my end :slightly_smiling_face:

gnattishness avatar Aug 25 '20 00:08 gnattishness

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?

hozn avatar Mar 09 '23 12:03 hozn