-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
add ISO year / ISO week utilities #48507
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
Conversation
Since the first week is defined in terms of Thursday according to ISO, perhaps this should be named |
I find both For example, 2019-12-30 and 2021-01-01 are two dates that have ISO year 2020: https://www.epochconverter.com/weeks/2020. So with julia> date = Date(2021) # A day of 2021 that has ISO year 2020
2021-01-01 what should The current implementation in this PR gives julia> firstmondayofyear(date) # date = 2021-01-01
2021-01-04 Is this expected? If the function is supposed to help computations with ISO weeks I would expect here to get 2019-12-30, because that is the first Monday in the ISO year that contains the date 2021-01-01. |
Here are some ideas of other functions/names to help working with ISO week dates:
Maybe most useful would be Examples: julia> date = Date(2021) # 2021-01-01 falls in ISO year 2020 which has first Monday 2019-12-30
2021-01-01
julia> firstmondayofisoyear(date)
2019-12-30
julia> isoyear(date)
2020
julia> Date(Year(2021), Week(1))
2019-12-30
julia> weekdates(Year(2020), Week(1))
Date("2019-12-30"):Date("2020-01-05") |
The current behavior is intentional, but you are right about the ambiguity. Initially I was going to only let iso year as an input, but then I thought it would make it easier to let users input arbitrary Date which is technically wrong. I could keep the functions implementations mostly the same, but the input will be iso year (integer). The names will change to reflect iso year. How about this for a start? Next, for the extra functionality I will follow the examples you have given. Do you know where could I find test data? |
I think it makes sense to let users pass an arbitrary Date: this is how the existing In the case of julia> firstmondayofyear(Date(1901))
1900-12-31 while I think it should give But anyway for working with ISO weeks, aren't the functions I proposed easier and clearer? For example to find 2023-W05-4, you could call For the test data maybe the easiest is to make a CSV with Excel (or with Julia and the |
I actually thought and decided against Though, the current implementation might not be too fast, but it feels to be more robust. What do you think? If this looks to be satisfactory I will proceed to implement inverse conversion and |
For |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the update, the proposed API looks good to me except for the argument type for weeksperyear
: I think that function should accept a Year
instead of a DateTime
. With a DateTime
it's not clear if the function will give an answer for the actual year of dt
, or for the ISO year of dt
.
Also it would be good to add one or two examples (jldoctests) in each of the docstrings.
Co-authored-by: Jeremie Knuesel <[email protected]>
Co-authored-by: Jeremie Knuesel <[email protected]>
Co-authored-by: Jeremie Knuesel <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to add some tests in: https://github.com/JuliaLang/julia/blob/master/stdlib/Dates/test/accessors.jl
Yeah, I'll get on to that, sorry for the delay. My editor keeps formatting files on save according to juliaformatter (thereby chaning like 50% of the lines), i have not committed these changes but do you guys try to format the julia stdlib codebase? Should I commit the stylistic changes? |
We do want to format the whole codebase.
Better not, Only format your newly written code. |
Yeah i have no idea why tests are failing on apple ... |
was used to generate the test cases |
The error is not related to Dates, just ignore it. |
doctest failed. |
I dont understand, where/what should I fix? |
Co-authored-by: woclass <[email protected]>
This plugs a functionality hole in Dates, ISO week is the standard for week numbering. And it's a simple addition, just implementing "missing" methods and some tests. |
Is this PR still needed? cc: @tecosaur (who previously added the FWIW, I still think it might be easier to just add this to a separate package, unless there's a specific need for it to be in the stdlib (e.g. some other stdlib functionality would utilize this PR). |
It's 2 functions total more or less that patches an obviously missing functionality in Dates that is present in python's standard library. ISO year is a little niche but sometimes you need this for 'week' based calendars (like schools starting on first monday of the year, or similar). Being 3rd party isn't necessary because it is just a single function (10 lines). This kind of functionality isn't likely to change in the future being a ISO standard |
@DilumAluthge I don't think "required by other stdlib functionality" is the best measure in this case. Much of the Dates module is about calendars, weeks, months, etc. The small addition in this PR fits perfectly in that, and plugs a hole that's not trivial to cover by oneself, and it would be annoying having to rely on a third party package just for that. Maybe Julia shouldn't come with a Dates module if the stdlib doesn't need it, but as long as we ship one, I think it should include the functionality in this PR. |
my opinion is that the fact that the probably does need compat note in docs though (now will be 1.13) |
@DilumAluthge im not sure if im supposed to add docs somewhere now? Please tell me where and how and I will do it. |
just a compat note; something like
at the end of each of the docstrings of the 3 functions |
@adienes is this right? |
yes, thanks! I'm just merging master to restart CI |
Nice took like 2.5years only. |
By the way, should I also edit the |
sorry that it took so long, and thank you for the contribution! sure, a NEWS entry could be a good thing to add. I forgot about that |
Should I update news file in a new PR or somehow it can be merged again from this PR |
Please make a new PR, you can't add to one that's merged already. |
Based on some discussion in https://discourse.julialang.org/t/determine-date-range-given-year-and-week-number/93878
and the relevant issue #48490
I created a couple of utilities to help handle 2023-W05-4 (YYYY-WWW-D) dateformats (https://en.wikipedia.org/wiki/ISO_week_date)
Currently there is no functionality of ISO Week with weekday | 2023-W05-4 in Julia, but this could be good start and could help a lot of people who require such dateformat.
Notably, with
firstmondayofyear
it is trivial to convert from week format 2023-W05-4 to usual date format 2023-02-02 as:This was otherwise impossible within std lib