Skip to content

Instantly share code, notes, and snippets.

@jaimelr
Created May 11, 2023 16:55
Show Gist options
  • Save jaimelr/686ce920e651a06597cb09360224109d to your computer and use it in GitHub Desktop.
Save jaimelr/686ce920e651a06597cb09360224109d to your computer and use it in GitHub Desktop.
Duration using decimal hours produces wrong output using toFormat #1070

Describe the bug

Creating a duration using a decimal hour then using toFormat produces an incorrect result

The inputs we've seen wrong so far are 2.4 and 5.3

To Reproduce

luxon.Duration.fromObject({hour: 2.4}).toFormat('hh:mm') returns "02:23"
luxon.Duration.fromObject({hour: 5.3}).toFormat('hh:mm') returns "05:17"

Actual vs Expected behavior

  • 2.4 hours is 02:24
  • 5.3 hours is 05:18

Additional context

Converting to different units from hours produces correct results

luxon.Duration.fromObject({minute: 2.4 * 60}).toFormat('hh:mm') = "02:24"
luxon.Duration.fromObject({second: 2.4 * 60 * 60}).toFormat('hh:mm') = "02:24"
luxon.Duration.fromObject({hour: 2.4}).shiftTo('seconds').toFormat('hh:mm') = "02:24"

Shifting to hours also produces the wrong result

luxon.Duration.fromObject({minute: 2.4 * 60}).shiftTo('hour').toFormat('hh:mm') = "02:23"

The durations still produce the same valueOf

luxon.Duration.fromObject({hour: 2.4}).valueOf() = 8640000
luxon.Duration.fromObject({minute: 2.4 * 60}).valueOf() = 8640000

Instructions

  1. Clone the library repository to your local development environment: https://github.com/moment/luxon
  2. Checkout to tag 2.1.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment