-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
feat(date): add durationToString
function to stringify durations as ISO 8061 strings
#7788
base: main
Are you sure you want to change the base?
Conversation
…ngify durations as ISO 8061 strings Additionally, this commit makes `parseDuration` accept decimal values for all time scale components rather than just ones after the time designator. This is consistent with the ISO 8601-1:2019 specification and should improve interoperability.
I'm unsure about the check nagging me about the PR title; it seems the regex is quite a bit restrictive and not fond of the slash, which imho is correct here 😅 |
durationToString
function to stringify durations as ISO 8061 stringsdurationToString
function to stringify durations as ISO 8061 strings
the pr title linting is a work in progress and nothing is hooked up to the name category yet, I have changed it to just 'date' as that is more than fine for what we use it for also, we are mid release cycle right now, so apologies if there is a bit of a delay for looking at this pr |
Thanks for the PR. We had a chance to discuss this today as a team. We'd like to propose a couple of changes. Instead of this function on its own, we'd like for The class should match |
That's understandable, although I wonder if that wouldn't incur breaking changes 🤔 Besides, sounds great, would make manipulating durations a lot easier (immutably that is); just worried a bit by the breaking nature of using a class compared to a plain object. |
So long as the class implements the same properties as the object, we should be fine. The types should still match as well. Was there another worry for breaking changes there? |
Immutability isn't something that is currently required to deal with, it's possible to mutate plain objects currently but would require using appropriate methods using a class. Also, Temporal objects implement |
I think the change to Immutability is a little more concerning to me since that one can pop up in unexpected places. We could consider that parseDuration take a new arg which has it return the Temporal aligned class instance so that existing usage doesn't change. I'll bring it up with the team. |
The biggest problem is that toJSON is automatically called by |
This PR introduces a new helper called
durationToString
to stringifyDateTimeDuration
objects to an ISO 8061 duration string.Additionally, this PR makes
parseDuration
accept decimal values for all time scale components rather than just ones after the time designator. This is consistent with the ISO 8601-1:2019 specification and should improve interoperability. Tests that were incorrectly checking failure of parsing have been updated.Side note: currently the
parseDuration
function wrongly accepts strings containing the week time scale component mixed with other components. My interpretation of the standard is that this is incorrect; a duration string can either be composed of year, month, day, hours, minutes, seconds (optionally omitting zero values), orPxW
. This isn't a major issue imho, but I've made sure that the library doesn't produce such invalid strings.✅ Pull Request Checklist:
📝 Test Instructions:
Simply experiment with the
durationToString
function. Tests have been added to the test suite, and documentation have been updated, which contain examples of how to use the function.