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

getSeconds sometimes returns different values #2

Closed
SarcevicAntonio opened this issue Dec 3, 2022 · 1 comment
Closed

getSeconds sometimes returns different values #2

SarcevicAntonio opened this issue Dec 3, 2022 · 1 comment

Comments

@SarcevicAntonio
Copy link

SarcevicAntonio commented Dec 3, 2022

I noticed some weird behavior when trying out getSeconds in an Advent of Vue challenge (that I did in Svelte) and had to replace it sadly.

Sometimes getSeconds('1 day') and the others return weird values.

Screenshot 2022-12-04 at 00 53 00

Here is a minimal repro that showcases it with a flickering number and in the console when using chrome 107.0.5304.121: https://svelte.dev/repl/31907b32e50042d8b8f068148de739a3?version=3.53.1

EDIT: same on firefox 107.0 (lol they share version numbers atm?) and safari 16.1

@kwhitley
Copy link
Owner

kwhitley commented Dec 4, 2022

Should be resolved in 0.2.1 :)

The Problem

Internally, itty-time was using the build-in Date functions for manipulating time from a fixed date. This meant using things like setHours and setMonth and such. getSeconds was actually a derivative function of datePlus, which needed those bits. Regardless, some of these functions return inconsistent results, which is especially irritating when you're asking for a very easily calculation like seconds, days, etc.

The Solution

I've reworked itty-time to make getSeconds the first-class citizen, and default to using basic time math for all "obvious" units (seconds, minutes, hours, days, weeks), and only use the built-ins for months and years (where you don't want to add a fixed number of seconds, you typically want to move the month/year). The new solution should be faster in most cases (simple multiplication is faster than getters/setters), and only added 11 bytes total, as much of the specific unit translation to use all the setters could be removed. Furthermore, with getSeconds now not dependent on the other functions, the import for that alone should actually be smaller!

Notes

I've verified it in your demo code (that was super helpful btw!), and confirmed the value remains 86400 now :)

@kwhitley kwhitley closed this as completed Dec 4, 2022
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

No branches or pull requests

2 participants