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

Week number of the Year #102

Open
codingedgar opened this issue Oct 2, 2022 · 5 comments
Open

Week number of the Year #102

codingedgar opened this issue Oct 2, 2022 · 5 comments

Comments

@codingedgar
Copy link

Hi, thank you for such a great lib.

I'm trying to figure out a way to get the week of the year.

Is there a way to accomplish this with the library?

@JordanMartinez
Copy link
Contributor

Probably, but it depends on what you mean by 'week number of the year'. See https://www.wikiwand.com/en/ISO_week_date

@codingedgar
Copy link
Author

Oh wow, it seems there can be multiple elements affecting what the week date is.

Depending if the start of the week is set Monday or Sunday, and if the week day is containing vs ISO.

I honestly cannot think of an easy way to get a any of the possible "valid" week dates 😅. Judging by the amount of code/config from date-fns/moment that have the most complete set (Locale Week Date, full config and ISO Week Date) it doesn't seem trivial.

Thank you so much for the read, if you already know of any quick way to get a week date I'd be amazed to know, but apart from that please feel free to close this issue as I assume it is out of scope for this library.

@garyb
Copy link
Member

garyb commented Oct 3, 2022

An extremely rough estimate would be something like:

floor ((current date in ms - start of current date year ms) / 604800000)

604800000 being the length of a week in milliseconds... but I don't know if that's at all suitable for what you're trying to do. 😄

I think it would be reasonable to add some more stuff to this library, including a function (or more likely multiple functions that allow choice of which variety of week number is wanted) to do this kind of operation, but without making it fully TZ and locale aware. Maybe... it's perhaps not that useful without TZ being taken into account.

I was very reluctant to do a TZ/locale aware library originally due to the maintenance burden that would come with, but I think there is room for it now - browsers have the Intl API now that I think means we wouldn't necessarily need to keep the library up to date with the IANA Time Zone database and could instead make use of the API to handle those kind of computations. But I would say it should be done separately from this library, as ideally this library should be completely pure and backend agnostic (it isn't right now, but not for any good reason), and is only meant to represent dates and times in a basic form, essentially UTC.

@codingedgar
Copy link
Author

Thank you so much for the example 🙏!

Haha I typed all of those reasons and then deleted it to be brief 🤣 It seems clear the goal is to have a pure/agnostic/basic date/datetime/duration/time library to work with PS from the get go.

And it seems like TZ-unaware week date might be not very usable as the result might be significant on the edges.

My intent is to display a calendar UI as Google Calendar, no calculations so far for the weeks, so precision is not crucial atm, but precisely should be something configurable/localised.

The lib also seems too beautiful and "core" for such functions, agree a separate lib might be better.

For the time being I'm gonna FFI date-fns, it really seems tricky to get a full solution right in PS way for my skill level atm 😅

@JordanMartinez
Copy link
Contributor

At work, I implemented a generator based on this code, so that we have the dateFns interface but it's using DateTime behind the scenes. So, it's doable, but not always safe. That's actually how I came to learn about the various week number definitions.

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

3 participants