-
-
Notifications
You must be signed in to change notification settings - Fork 33
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: Add versatile duration formatting and parsing function #66
base: main
Are you sure you want to change the base?
Conversation
- Introduced `formatOrParseDuration` function to format and parse durations. - Supports various formats including `hh:mm:ss`, `mm:ss`, `DD:hh:mm:ss`, `hh:mm:ss,SSS`. - Updated related types and interfaces to accommodate the new function. - Added comprehensive tests for different duration formats and parsing.
@SeanLuis is attempting to deploy a commit to the Formkit Team on Vercel. A member of the Team first needs to authorize it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your work on this PR. it looks well put together and I appreciate the effort. What I like about this:
- Simple to implement with today’s browsers
- You include tests
- Bi-directional — this follows the tempo mantra that anything we can format we should be able to parse.
Those are great positives, and in that regard I would consider merging this. However, I think it may warrant further discussion as for how we should actually handle things like duration formats.
Unlike the rest of Tempo, this feature does not take into account locales. The colon, for example, is not a global standard. Some countries use a dot .
some use h
, some use a comma ,
. This is why it would be so great to have names like short
or long
.
Now, of course, the rub is how could we take these things into account since the Intl.DurationFormat()
is barely supported anywhere at this point. The DurationFormat is a stage 3 proposal, so perhaps it will be more broadly available in a year or two and this API could be more complete...
I’m not going to immediately merge this or close this — I’d love to hear more feedback from other users.
Again — thanks so much for the effort, great work.
Thank you, Justin, for your detailed feedback and kind words. I appreciate your points regarding the implementation and the thoughtful consideration about locale-specific formats. I completely agree that having options like I'll explore how we can incorporate locale variations and ensure the feature aligns better with global standards. As for the I'll also await further feedback from the community to gather more insights. Thank you again for your guidance and support. |
…ocess Co-authored-by: Justin Schroeder <[email protected]>
- Extended DateTimeFormatPartTypes to include "millisecond". - Added support for parsing and formatting milliseconds in `parse.ts`. - Updated `format.ts` to handle new format tokens for milliseconds. - Enhanced `parts.ts` with millisecond support in guessPattern and createPart functions. - Modified `date.ts` to correctly normalize and handle milliseconds. - Updated `offset.ts` to apply offsets including milliseconds. - Improved ISO 8601 validation in `iso8601.ts` to support milliseconds. - Updated common utilities in `common.ts` to handle `SSS` format pattern and ensure millisecond support. - Added new test cases for milliseconds in `sameMillisecond.test.ts`. - Expanded ISO 8601 validation tests in `iso8601.test.ts` to include milliseconds. These changes enhance the precision of date handling functions, ensuring compliance with ISO 8601 and extending support to milliseconds.
I took a quick glance over the work @SeanLuis — it looks really promising. I’ll try to carve out some time to fully review it and test it soon! |
Add Millisecond Support and Improve Date Handling FunctionsDescriptionThis commit 4e94ac2 introduces support for milliseconds across various date handling functions within the codebase. The following changes have been made: Detailed Changes1. Types and Interfaces Update (
|
@SeanLuis & @justin-schroeder maybe best to split the I would split it up into the following (names are only suggestions) type DurationObj = {years: number, months: number .........}
function formatDuration(duration: DurationObj, options: {format: string, style: string, .....}): string
function parseDuration(durationString: string, options: { style: string, locale: string, ...}): DurationObj
function msToDurationObj(ms: number): DurationObj
function durationObjToMs(duration: DurationObj): number Also to add to this, I'm also planning to create a |
It seems good to me, it is in line with the structure of the package. As for the names of the functions, they seem fine to me. 👌🏼 |
hey @SeanLuis i think we could add more formats like
|
This is really great work @SeanLuis — Ive got some stuff to ship beginning of the week and then ill turn my attention to this PR. thanks for keeping the quality high. |
Intl.DurationFormat is supported by more and more browsers. It makes sense to use it as the main tool for working with duration and foulback in case |
Overview
This pull request introduces a new feature: the
formatOrParseDuration
function. This function provides the ability to both format durations given in milliseconds to a specified string format and parse duration strings back into milliseconds. This enhancement aims to offer a robust and scalable solution for duration handling within the library.Features
hh:mm:ss
mm:ss
DD:hh:mm:ss
hh:mm:ss,SSS
hh:mm:ss
mm:ss
DD:hh:mm:ss
hh:mm:ss,SSS
Tasks Completed
formatOrParseDuration
functionhh:mm:ss,SSS
)hh:mm:ss
,mm:ss
,DD:hh:mm:ss
,hh:mm:ss,SSS
)hh:mm:ss
,mm:ss
,DD:hh:mm:ss
,hh:mm:ss,SSS
)Implementation Details
Function:
formatOrParseDuration
Parameters:
input
(number | string): Duration in milliseconds to format or a string to parse.options
(DurationOptions): Options to define formatting or parsing behavior, including:format
: Desired format for the duration string or parsing pattern.parse
: Boolean indicating whether to parse a string or format a duration.locale
: Locale string for future compatibility.Behavior:
parse
istrue
, it parses the provided duration string into milliseconds based on the specified format.parse
isfalse
, it formats the given duration in milliseconds to a string based on the specified format.Tests
Comprehensive tests have been added to cover various scenarios for both formatting and parsing:
hh:mm:ss
,mm:ss
,DD:hh:mm:ss
,hh:mm:ss,SSS
.Example Usage