Skip to content

Conversation

@dobon
Copy link
Contributor

@dobon dobon commented Dec 5, 2024

This PR duplicates and updates PR: #748 and issue: #651

I've incorporated the feedback from that review and PR, minus renaming "supress[milli]second" which should be in a different PR if the change is made.

@dobon dobon changed the title update precision pull request Create ISO 8601 Date/Time Values w/o Milliseconds (re-implemented, ready for review) Dec 6, 2024
Copy link
Collaborator

@diesieben07 diesieben07 left a comment

Choose a reason for hiding this comment

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

Should we also support this for calendar units?
2024-05 is a valid ISO date, referring to the month of may 2025.

src/datetime.js Outdated
* @param {boolean} [opts.includeOffset=true] - include the offset, such as 'Z' or '-04:00'
* @param {boolean} [opts.extendedZone=false] - add the time zone format extension
* @param {string} [opts.format='extended'] - choose between the basic and extended format
* @param {string} [opts.precision='milliseconds'] - specify desired time precision: 'hours', 'minutes', 'seconds' or 'milliseconds'
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should mention

  • that it will truncate the output
  • How it interacts with suppressSeconds and suppressMilliseconds

@dobon
Copy link
Contributor Author

dobon commented Apr 11, 2025

Should we also support this for calendar units? 2024-05 is a valid ISO date, referring to the month of may 2025.

There was discussion about this in the prior PR, but no conclusion. I've added another commit to enable precision from Year to Millisecond, and it seems good to me. Do the test cases show the function behaving the way you had in mind @diesieben07 ?

I've implemented the changes you've requested and this PR is ready for review.

@dobon dobon requested a review from diesieben07 April 11, 2025 15:47
fix bugs from tests

appease style rules

fix test

extend precision + suppress tests

replace nested ifs with a fall-through switch case

appease javascript linter

fix logic to pass tests

simplify logic to remove switch case.

enable precision for iso dates too

refactor to minimize code changes

simplify and fix comment typo

update comments per code review.
@dobon dobon force-pushed the to-iso-truncate-time branch from fc7db09 to 4fc8391 Compare April 13, 2025 06:24
src/datetime.js Outdated
}
let showSeconds,
c = "";
switch (precisionUnitIndex) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not a fan of the code duplication as well as the magic index numbers here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback.

Both the loop unraveling(code duplication) and magic index numbers were implemented like that for performance (ie: to minimize comparisons) because toISO can be a hot path.

The loop unravelling was to avoid unnecessary compares on smaller units (eg: if precision is 'days', then don't bother checking if precision is greater than years and months), and the magic index numbers were to facilitate numeric compares instead of string.

That being said, if performance isn't paramount then I agree that the repetitive code reads not-great and I'm happy to re-implement any way you please. As such, I've pushed a commit that passes precision as a string parameter (eliminating the magic numbers), and eliminates the loop unraveling in exchange for a couple more compares. Let me know what you think.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree that toISO should be fast. But we shouldn't make claims about performance without a benchmark :D

@dobon dobon requested a review from diesieben07 April 27, 2025 09:56
@diesieben07 diesieben07 merged commit 4786e51 into moment:master Apr 27, 2025
4 checks passed
@diesieben07
Copy link
Collaborator

Thank you!

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.

3 participants