purescript-datetime icon indicating copy to clipboard operation
purescript-datetime copied to clipboard

Week number of the Year

Open codingedgar opened this issue 1 year ago • 5 comments

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?

codingedgar avatar Oct 02 '22 01:10 codingedgar

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

JordanMartinez avatar Oct 03 '22 08:10 JordanMartinez

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.

codingedgar avatar Oct 03 '22 11:10 codingedgar

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. :smile:

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.

garyb avatar Oct 03 '22 12:10 garyb

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 😅

codingedgar avatar Oct 03 '22 14:10 codingedgar

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.

JordanMartinez avatar Oct 03 '22 15:10 JordanMartinez