tolu / ISO8601-duration

Node/Js-module for parsing and making sense of ISO8601-durations
92 stars 10 forks source link

Incorrect toSeconds value #30

Closed winston0410 closed 1 year ago

winston0410 commented 1 year ago

I am not sure if this is a bug or a feature that I don't understand. I get the duration of a year in seconds like this:

import { parse, toSeconds } from "iso8601-duration";

console.log(toSeconds(parse('P1Y'))) //31536000

And when I get 2 years in seconds, the value seems to be wrong:

import { parse, toSeconds } from "iso8601-duration";

console.log(toSeconds(parse('P2Y'))) //It returns 63158400, instead of 63072000

Why is that?

tolu commented 1 year ago

hi there, sorry for the late reply 🙏

the reason is that any duration has to have a starting point (an epoch if you will), that being defaulted to Date.now() if nothing is provided. And so getting the duration for 2 years only means that the subsequent year might be a leap year or something like that - so it is all calendar related which makes it seem a bit weird, but correct 😊

winston0410 commented 1 year ago

Yes Ieap year was the issue. Thanks for your reply