Skip to content
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

#651: Create ISO 8601 Date/Time Values w/o Milliseconds #748

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

nbcarey
Copy link

@nbcarey nbcarey commented Jul 29, 2020

This addresses Issue #651 (Feature request - function to quickly create ISO8601 stamp without milliseconds).

It adds a precision option, the domain of which is hours, minutes, seconds, milliseconds, defaulting to milliseconds. Time components of higher precision than that specified are simply omitted: no attempt is made at rounding.

If precision is specified in addition to suppressSeconds or suppressMilliseconds, the suppress* option wins:

DateTime.utc({2020,7,25,12,34,0,0}).toISO({ precision: 'millisecond',  suppressSeconds: true })

returns 2020-07-25T12:34Z.

Millisecond precision was requested, with the proviso that seconds and milliseconds won't be rendered if they are both zero.

Note: It might be a nice TO DO to add an auto precision that would set the precision level to omit trailing components whose value is zero, so

Datetime.utc({2020, 7, 25, 12, 34, 0, 0}).toISO({ precision: "auto" })

would return 12:34Z.

@jsf-clabot
Copy link

jsf-clabot commented Jul 29, 2020

CLA assistant check
All committers have signed the CLA.

@nbcarey nbcarey changed the title To iso truncate time #651: Create ISO 8601 Date/Time Values w/o Milliseconds Jul 29, 2020
Copy link
Contributor

@gdebunne gdebunne left a comment

Choose a reason for hiding this comment

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

I created a dedicated PR for npm install

* @example DateTime.utc().set({ hour: 7, minute: 34 }).toISOTime() //=> '07:34:19.361Z'
* @example DateTime.utc().set({ hour: 7, minute: 34, seconds: 0, milliseconds: 0 }).toISOTime({ suppressSeconds: true }) //=> '07:34Z'
* @example DateTime.utc().set({ hour: 7, minute: 34 }).toISOTime({ format: 'basic' }) //=> '073419.361Z'
* @example DateTime.utc().toISOTime({ precision: 'minute' }) //=> '12:34Z'
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe add a .set({ hour: 7, minute: 34, second: 56 }) for consistency and to show that no rounding is performed

@@ -1559,22 +1604,26 @@ export default class DateTime {
* @param {boolean} [opts.suppressSeconds=false] - exclude seconds from the format if they're 0
* @param {boolean} [opts.includeOffset=true] - include the offset, such as 'Z' or '-04:00'
* @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
Contributor

Choose a reason for hiding this comment

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

Add quotes around the possible values to outline strings, like above

test("DateTime#toISOTime({precision}) throws when the precision is invalid", () => {
expect(() => dt.toISOTime({ precision: "ms" })).toThrow(InvalidUnitError);
expect(() => dt.toISOTime({ precision: "xxx" })).toThrow(InvalidUnitError);
expect(() => dt.toISOTime({ precision: null })).toThrow(InvalidUnitError);
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe add days, months and years to the list

expect(dt.toISOTime({ precision: "minute" })).toBe("09:23Z");
expect(dt.toISOTime({ precision: "hours" })).toBe("09Z");
});

Copy link
Contributor

Choose a reason for hiding this comment

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

Add a test using both precision and suppress[milli]seconds

// Note that suppressSeconds'/'suppressMilliseconds' take precedence over
// 'precision'. If you ask for millisecond precison, but have
// 'suppressSeconds' set to true, You'll get back a string with only
// hours and minutes when seconds and milliseconds are both zero.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think precision should take precedence instead. It feels more constraining, and suppress* is more cosmetic.

Yes, it would mean that in { precision : 'minutes', suppressSeconds: true } the suppress part is useless, but that feels more intuitive than to display seconds when explicitly asking for a minutes:

12:34:56.789 { precision : 'minutes', suppressSeconds: true } -> 12:34
feels more natural than
12:34:56.789 { precision : 'minutes', suppressSeconds: true } -> 12:34:56

However,
12:34:00 { precision : 'seconds' } -> 12:34:00
12:34:00 { precision : 'seconds', suppressSeconds: true } -> 12:34

Copy link
Author

Choose a reason for hiding this comment

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

I'll try to get to that this weekend, along with adding days, months and years.

Copy link
Member

Choose a reason for hiding this comment

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

I agree with the above, but it also reminds me that supressSeconds was never a great name. suppressSecondsIfTheyAreZero is too long. Any ideas?

Copy link
Collaborator

Choose a reason for hiding this comment

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

hideZeroSeconds / suppressZeroSeconds ?

Copy link
Member

Choose a reason for hiding this comment

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

hideZeroSeconds sounds good to me

@loynoir
Copy link

loynoir commented Mar 4, 2022

Hi, developers.

Will this useful pull be merged?

Maybe stripMilliseconds?

  • Less than 0.5, 11:22:33.444 -> 11:22:33
  • Larger than 0.5, 11:22:33.666 -> 11:22:33

@ahmetzambak
Copy link

Still missing that feature..

@icambron
Copy link
Member

icambron commented Jan 4, 2023

To anyone finding this: this PR just needs a little work, as detailed in the comments. If you do that work, I'll merge it! In the meantime, asking for it more isn't going to help.

fmt += ".SSS";
}
// validate the precision and throw if invalid
let desiredPrecision = Precision[Duration.normalizeUnit(precision)];
Copy link

Choose a reason for hiding this comment

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

Wondering if this needs to call the static normalizeUnit on Duration vs local to datetime.js?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants