Skip to content

Implement YearMonth #457

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft

Implement YearMonth #457

wants to merge 3 commits into from

Conversation

dkhalanskyjb
Copy link
Collaborator

@dkhalanskyjb dkhalanskyjb commented Nov 13, 2024

Fixes #168
Fixes #184

Related PRs:

@dalewking
Copy link

About time we get this added. You need to resolve merge conflicts

@dkhalanskyjb
Copy link
Collaborator Author

You are right! Thanks for the advice!

@dkhalanskyjb dkhalanskyjb added this to the 0.7.0 milestone Mar 18, 2025
*
* @sample kotlinx.datetime.test.samples.YearMonthSamples.days
*/
// val days: LocalDateRange get() = firstDay..lastDay // no ranges yet

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for commenting this out? Seems useful to me.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason is that LocalDateRange is not in master. See the description of the pull request.

*
* @sample kotlinx.datetime.test.samples.YearMonthSamples.days
*/
// val days: LocalDateRange get() = firstDay..lastDay // no ranges yet

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another useful range would be an open end range of LocalDateTime that is midnight on the first day of the given month until midnight on the first day of the next month

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no ranges of LocalDateTime semantically, because due to DST transitions, sometimes, "earlier" local date-times follow the "later" ones, and some local date-times don't exist at all.

* @throws DateTimeArithmeticException if the result exceeds the boundaries of [YearMonth].
* @sample kotlinx.datetime.test.samples.YearMonthSamples.minusMonth
*/
public fun YearMonth.minusMonth(): YearMonth = minus(1, DateTimeUnit.MONTH)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 4 functions should be plural and accept an int parameter that defaults to 1 as in:

public fun YearMonth.minusMonths(months: Int = 1): YearMonth = minus(months, DateTimeUnit.MONTH)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In our research, we haven't found cases where the value was anything other than 1, so the extra flexibility is pointless. You have a more general plus for this case if 1 doesn't suit your needs.

*
* @sample kotlinx.datetime.test.samples.YearMonthSamples.firstAndLastDay
*/
public val lastDay: LocalDate get() = onDay(numberOfDays)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add contains methods for LocalDate and LocalDateTime as in:

    public fun contains(date: LocalDate): Boolean = date in firstDay..lastDay

Also helpful would be intersects functions for ranges to determine if this year month has any overlap with another range. This should include the cases of ClosedRange<LocalDate> and OpenEndRange<LocalDateTime>.

    public fun intersects(dateRange: ClosedRange<LocalDate>): Boolean = ...
    public fun intersects(dateTimeRange: OpenEndRange<LocalDateTime>): Boolean = ...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, we've decided that to interpret the YearMonth as a range, you'd use the days function (currently commented out).

@dkhalanskyjb dkhalanskyjb force-pushed the year-month branch 2 times, most recently from 48e41f2 to 8929852 Compare March 20, 2025 13:03
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

Successfully merging this pull request may close these issues.

Getting days in Month Substitute for YearMonth?
2 participants