-
Notifications
You must be signed in to change notification settings - Fork 109
Add an equivalent of TemporalAdjusters #235
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
Comments
Thanks! This looks like an interesting proposal, and we received several requests in this vein already, though we didn't yet manage to find a good API shape for this. A question about your use case though: let's say an alarm is enabled on Monday, Tuesday, and Thursday. On Tuesday, the alarm goes off, and your code needs to schedule an alarm for Thursday (not Monday or another Tuesday). How would it know that it needs Thursday and not something else? Could you write the full code if we pretend that we have a function like EDIT: actually, let's not even pretend, here's the function: public fun LocalDate.nextDateWithWeekDay(newDayOfWeek: DayOfWeek): LocalDate =
plus((newDayOfWeek.isoDayNumber - dayOfWeek.isoDayNumber).mod(7), DateTimeUnit.DAY) |
Hello, here are the functions I'm using right now. These are essentially copy-pasted from the java.time library source code: fun LocalDateTime.withNextDayOfWeek(
dayOfWeek: DayOfWeek,
zone: TimeZone,
): LocalDateTime {
val daysDiff = this.dayOfWeek.value - dayOfWeek.value
val daysToAdd = if (daysDiff >= 0) 7 - daysDiff else -daysDiff
return plusDays(daysToAdd, zone)
}
fun LocalDateTime.previousOrSameDayOfWeek(
dayOfWeek: DayOfWeek,
zone: TimeZone,
): LocalDateTime {
val daysDiff = dayOfWeek.value - this.dayOfWeek.value
if (daysDiff == 0) return this
val daysToAdd = if (daysDiff >= 0) 7 - daysDiff else -daysDiff
return minusDays(daysToAdd, zone)
} (I know you won't like my api of these, it's my personal choice) |
Please reread my last message. I already provided a function similar to your |
Okay, I'm sorry I misunderstood you. fun Ritual.getNextAlarmTime(now: LocalDateTime, zone: TimeZone): LocalDateTime? {
if (!shouldBeNotified) return null
val days = repeat.toSet()
val withTime = now.withTime(time!!.copy(second = 0), 0) //adjusts LocalDateTime to specified Time
return if (now.dayOfWeek in days && withTime > now) {
// alarm can go off today, and the time has not passed yet
// (alarms for less than 1 min do not count)
withTime
} else {
// if no day is next in the week, take closest to the start of the week
val dayOfWeek = days.firstOrNull { it > now.dayOfWeek } ?: days.first()
withTime.withNextDayOfWeek(dayOfWeek, zone)
}
}
fun LocalDateTime.asStartOfWeek(zone: TimeZone) =
withPreviousOrSameDayOfWeek(DayOfWeek.MONDAY, zone).asMidnight() P.S. There is no support for Sunday as the first day of week yet, but it will take minor adjustments |
Great, thank you! This is clearly a solid use case. An unrelated note: assuming
val zone = TimeZone.of("Europe/Berlin")
val wednesday = LocalDateTime(2023, Month.MARCH, 22, 2, 30).toInstant(zone)
val sunday = wednesday.plus(4, DateTimeUnit.DAY, zone)
val anotherWednesday = sunday.plus(3, DateTimeUnit.DAY, zone)
println(anotherWednesday.toLocalDateTime(zone)) // 2023-03-29T03:30 Instead, you should explicitly store the |
Do I understand you correctly that the bug happens because the LocalDateTime is reused? Here's how my system works:
I think that this behavior should work correctly, although unit-testing it would be a pain so I haven't... |
Yep.
The timezone itself doesn't change in my example, it's still
Then I don't understand how |
No, |
Ah, ok! Everything should be good then. |
Closing in favor of #325 |
TemporalAdjusters
are a useful family of classes in thejava.time
libraryThey allow to change a specific field value of a datetime according to a specific pattern commonly used by humans, avoiding the complications of the computation of such a value.
Common use cases (basically th list of adjusters from java.time):
My personal use case is this:
I want to set an alarm that fires on given days of week each week, and each time an alarm is fired, I schedule an alarm for the next day of week that alarm is enabled on. I want to get the next date for my specific day of week to trigger the next alarm.
I propose implementing these as extension functions on
LocalDate
/LocalDateTime
The text was updated successfully, but these errors were encountered: