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

#1709 Generate C# TimeSpan as a DayJs duration #1714

Closed
wants to merge 1 commit into from
Closed

#1709 Generate C# TimeSpan as a DayJs duration #1714

wants to merge 1 commit into from

Conversation

FreeFrags
Copy link
Contributor

Attempt to generate a Dayjs duration for a C# TimeSpan.

See #1709

Im not familiar with the code however I wanted to attempt to help out.

DayJS duration takes a ISO 8601 string or an object as a constructor.

I chose to use a function to convert the 'D.HH:mm:ss.SSS' string to the object.

Im not sure if that is a good idea or if converting it to a ISO 8601 and passing that to the constructor would be more reusable.

Copilot suggests the following conversion function:

function convertToISO8601(input: string): string {
  const [days, rest] = input.split('.');
  const [hours, minutes, seconds, milliseconds] = rest.split(/[:.]/);

  // Constructing the ISO 8601 duration string
  let isoDuration = 'P';
  if (parseInt(days) > 0) isoDuration += `${parseInt(days)}D`;
  if (hours !== '00' || minutes !== '00' || seconds !== '00' || milliseconds !== '000') {
    isoDuration += 'T';
    if (parseInt(hours) > 0) isoDuration += `${parseInt(hours)}H`;
    if (parseInt(minutes) > 0) isoDuration += `${parseInt(minutes)}M`;
    if (parseInt(seconds) > 0 || parseInt(milliseconds) > 0) {
      let totalSeconds = parseInt(seconds);
      if (parseInt(milliseconds) > 0) {
        totalSeconds += parseInt(milliseconds) / 1000;
      }
      isoDuration += `${totalSeconds}S`;
    }
  }

  return isoDuration;
}

It would be best if the conversion function would be added as a function to the file as a utility which can then be called in the generated classes instead of inlined like i did here.

But im not sure how to do this and i hope someone can pick this up and polish it up.

@FreeFrags
Copy link
Contributor Author

Please dont merge this. I will make a new pull request.

I realized that the dayjs duration can also be constructed with a ISO 8601.

@FreeFrags FreeFrags closed this Jul 5, 2024
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.

1 participant