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

Getting days in Month #184

Open
Tommyten opened this issue Feb 3, 2022 · 6 comments
Open

Getting days in Month #184

Tommyten opened this issue Feb 3, 2022 · 6 comments
Labels
cookbook Useful snippets

Comments

@Tommyten
Copy link

Tommyten commented Feb 3, 2022

Is there currently any way of getting the number of total days in a month?

@dkhalanskyjb
Copy link
Contributor

dkhalanskyjb commented Feb 3, 2022

It's dependent on the year, due to leap days, so something like this is needed:

fun monthDays(year: Int, month: Month): Int {
  val start = LocalDate(year, month, 1)
  val end = start.plus(DateTimeUnit.MONTH)
  return start.until(end, DateTimeUnit.DAY)
}

Literally, "how many days between the start of this month and the next month".

@Tommyten
Copy link
Author

Tommyten commented Feb 4, 2022

Thanks for the suggestion. I think having this method implemented in the library would prove useful to quite a lot of people.

@imashnake0
Copy link

imashnake0 commented Feb 15, 2022

Perhaps if we get that YearMonth from #168, we could do something like YearMonth.of(22, 2).lengthOfMonth() (java.time). @dkhalanskyjb Would you consider a PR if I (or someone) happened to cover most if not all methods from java.time.YearMonth? I think a lot of people would find this helpful.

@dkhalanskyjb
Copy link
Contributor

There's an upside to the solution provided above: it uses the conceptually simple until function that calculates the length of some time segments and can be used in various other ways: it allows one to learn the number of days in a given year, or the number of minutes in a given day (which may vary due to DST transitions), or the number of seconds in a year…

By not providing wrappers around until for these cases, we direct the users of the library to learn to wield the flexibility of until. If getting the number of days in a month is something that's very commonly needed, then, true, it does make sense to provide this convenience wrapper, but it's not yet clear that it is.

@ilya-g
Copy link
Member

ilya-g commented Feb 16, 2022

IMO, getting the length of a month by creating two dates and subtracting them is a more contrived way than to query it directly from some calendar info provider. So, these functions are conceptually not wrappers around until, but on the opposite, until can be considered to use this low level info about the calendar which we have internally but do not expose because we haven't yet found the right API form for that.

@dkhalanskyjb
Copy link
Contributor

If we did have a calendar-info-provider thing, then sure. Currently, however, the until and periodUntil functions are the ways to access this functionality.

My point is, providing just something like fun daysInMonth(month: Month, year: Int): Int would be sacrificing orthogonality and conceptual clarity, as the result is not a property of a year or a month.

Implementing a more general way to query calendar info would be a clear solution for this, I agree. So would be something like YearMonth.lengthInDays if we did have YearMonth.

@dkhalanskyjb dkhalanskyjb added the cookbook Useful snippets label Dec 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cookbook Useful snippets
Projects
None yet
Development

No branches or pull requests

5 participants
@Tommyten @ilya-g @dkhalanskyjb @imashnake0 and others