question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calculations of years and months wrong due to leap years

See original GitHub issue

The constants used for milliseconds in a year and milliseconds in a month are incorrect because they are computed from the slightly naïve understanding of leap days in the Gregorian calendar.

A leap day occurs every four years. However, because that’s actually slightly too often, if the year is divisible by 100 then it is not a leap year. However, because that would be too infrequent, if the year is divisible by 400 they it is a leap year.

Thus the correct decimal representation of the average length of a year is 365.2425 days. Likewise, since the year is 12 exact months, an average length of a month is 30.436875 days.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
EvanHahncommented, Jun 7, 2020

Did some more thinking about this, finally.

I think there are a few distinct questions at hand:

  1. Is this a breaking change?
  2. What should the default durations for months and years be, if at all?
  3. Which units should be included by default?

The answer to the first question is easy: if we change anything, then it’s a breaking change. (I started a branch for the next major version if you want to follow along.)

The other two questions are trickier.

Wikipedia provides a list of time units. Milliseconds, seconds, minutes, hours, days, and weeks are constant. Months and years, however, have a range. In other words, it’s easy to answer “how long is an hour, in milliseconds?” and impossible to answer “how long is a month, in milliseconds?”

To inform what we should do, I looked at what other folks do.

Many others don’t deal with this problem at all:

My rough takeaway from the above: if your library deals with exact durations (e.g., “1.7 minutes” instead of “about 2 minutes”), then you either (1) don’t deal with months and years (2) treat them carefully.

I’d like to continue to support months and years, because I think they’re useful.

Given that, I think HumanizeDuration.js should:

  1. Remove mo and y from the default list. For example, by default, humanizing 400 days should humanize to 57 weeks, 1 day, not something like 1 year, 1 month, ....
  2. Allow users to opt-in to mo and y, and allow them to set the unit duration as they see fit. This is already supported with the unitMeasures option.
  3. Consider updating the default durations of mo and y, as is done in #157.
  4. Document the above clearly.

If that sounds good, I’ll do this in the next major version.

1reaction
EvanHahncommented, Jul 13, 2019

Ah dates, always more complicated than I think.

I think your change in #157 is worthy but suffers from a similar problem as the issue reported in #152—these are arguably breaking changes. And because this library is in maintenance mode, I’m hesitant. However, they are also arguably bug fixes, in which case they would be in scope.

What do you think?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Excel incorrectly assumes that the year 1900 is a leap year
NOTE: Microsoft Excel correctly handles all other leap years, including century years that are not leap years (for example, 2100). Only the  ......
Read more >
Calendar Calculations - NASA
So to correct (approximately), we add 1 day every four years (leap year). Thus, three calendar years are 365 days long; the fourth...
Read more >
Leap year problem - Wikipedia
The leap year problem is a problem for both digital (computer-related) and non-digital ... results from errors in the calculation of which years...
Read more >
The leap year and the law | Legal Blog
Every four years is a leap year, a year with an additional day in the month of February. How does this additional day...
Read more >
The math behind leap years.
So after four years the calendar is behind by a day. The Earth has spun one extra time over those four years, and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found