diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/intents/IntentParser.kt b/uhabits-android/src/main/java/org/isoron/uhabits/intents/IntentParser.kt index 6be35b4c9..e51daa96d 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/intents/IntentParser.kt +++ b/uhabits-android/src/main/java/org/isoron/uhabits/intents/IntentParser.kt @@ -22,6 +22,7 @@ package org.isoron.uhabits.intents import android.content.ContentUris.parseId import android.content.Intent import android.net.Uri +import org.isoron.platform.time.LocalDate.Companion.getStartOfDay import org.isoron.uhabits.core.AppScope import org.isoron.uhabits.core.models.Habit import org.isoron.uhabits.core.models.HabitList @@ -51,7 +52,7 @@ class IntentParser private fun parseTimestamp(intent: Intent): Timestamp { val today = DateUtils.getTodayWithOffset().unixTime var timestamp = intent.getLongExtra("timestamp", today) - timestamp = DateUtils.getStartOfDay(timestamp) + timestamp = getStartOfDay(timestamp) if (timestamp < 0 || timestamp > today) throw IllegalArgumentException("timestamp is not valid") diff --git a/uhabits-core/src/commonMain/kotlin/org/isoron/platform/time/Dates.kt b/uhabits-core/src/commonMain/kotlin/org/isoron/platform/time/Dates.kt index 5c334cabb..8bfed3470 100644 --- a/uhabits-core/src/commonMain/kotlin/org/isoron/platform/time/Dates.kt +++ b/uhabits-core/src/commonMain/kotlin/org/isoron/platform/time/Dates.kt @@ -25,7 +25,6 @@ import kotlinx.datetime.Instant import kotlinx.datetime.TimeZone import kotlinx.datetime.offsetAt import org.isoron.platform.i18n.getDefault -import kotlin.jvm.JvmStatic import kotlin.math.abs import kotlin.math.ceil @@ -191,7 +190,6 @@ data class LocalDate(val daysSince2000: Int) { * This function is supposed to be used to construct a sequence of weekday * number following java.util.Calendar conventions. */ - @JvmStatic fun getWeekdaySequence(firstWeekday: Int): Array { return arrayOf( (firstWeekday - 1) % 7 + 1, @@ -203,6 +201,8 @@ data class LocalDate(val daysSince2000: Int) { (firstWeekday + 5) % 7 + 1, ) } + + fun getStartOfDay(timestamp: Long): Long = (timestamp / DAY_LENGTH) * DAY_LENGTH } } diff --git a/uhabits-core/src/commonTest/kotlin/org/isoron/platform/time/DatesTest.kt b/uhabits-core/src/commonTest/kotlin/org/isoron/platform/time/DatesTest.kt index 38958cfab..e837c6d5f 100644 --- a/uhabits-core/src/commonTest/kotlin/org/isoron/platform/time/DatesTest.kt +++ b/uhabits-core/src/commonTest/kotlin/org/isoron/platform/time/DatesTest.kt @@ -22,6 +22,7 @@ package org.isoron.platform.time import kotlinx.datetime.LocalDateTime import kotlinx.datetime.TimeZone import kotlinx.datetime.toInstant +import org.isoron.platform.time.LocalDate.Companion.getStartOfDay import org.isoron.platform.time.LocalDate.Companion.getWeekdaySequence import kotlin.test.Test import kotlin.test.assertContentEquals @@ -54,4 +55,16 @@ class DatesTest { val weekdaySequence = getWeekdaySequence(3) assertContentEquals(arrayOf(3, 4, 5, 6, 7, 1, 2), weekdaySequence) } + + @Test + fun testGetStartOfDay() { + val expectedStartOfDayUtc = LocalDateTime( + 2017, 1, 1, 0, 0, 0, 0 + ).toInstant(TimeZone.UTC).toEpochMilliseconds() + val laterInTheDayUtc = LocalDateTime( + 2017, 1, 1, 20, 0, 0, 0 + ).toInstant(TimeZone.UTC).toEpochMilliseconds() + val startOfDay = getStartOfDay(laterInTheDayUtc) + assertEquals(expectedStartOfDayUtc, startOfDay) + } } diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/utils/DateUtils.kt b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/utils/DateUtils.kt index 761e77186..6e2aa2b2b 100644 --- a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/utils/DateUtils.kt +++ b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/utils/DateUtils.kt @@ -25,6 +25,7 @@ import org.isoron.platform.time.LocalDate.Companion.DAY_LENGTH import org.isoron.platform.time.LocalDate.Companion.HOUR_LENGTH import org.isoron.platform.time.LocalDate.Companion.MINUTE_LENGTH import org.isoron.platform.time.LocalDate.Companion.getLocalTime +import org.isoron.platform.time.LocalDate.Companion.getStartOfDay import org.isoron.platform.time.LocalDate.Companion.getTimeZone import org.isoron.uhabits.core.models.Timestamp import java.util.Calendar @@ -145,9 +146,6 @@ abstract class DateUtils { @JvmStatic fun getTodayWithOffset(): Timestamp = Timestamp(getStartOfTodayWithOffset()) - @JvmStatic - fun getStartOfDay(timestamp: Long): Long = (timestamp / DAY_LENGTH) * DAY_LENGTH - @JvmStatic fun getStartOfDayWithOffset(timestamp: Long): Long { val offset = startDayHourOffset * HOUR_LENGTH + startDayMinuteOffset * MINUTE_LENGTH diff --git a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/utils/DateUtilsTest.kt b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/utils/DateUtilsTest.kt index a84e1b66d..4ace03328 100644 --- a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/utils/DateUtilsTest.kt +++ b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/utils/DateUtilsTest.kt @@ -120,14 +120,6 @@ class DateUtilsTest : BaseUnitTest() { assertThat(Timestamp(FIXED_LOCAL_TIME), equalTo(today)) } - @Test - fun testGetStartOfDay() { - val expectedStartOfDayUtc = unixTime(2017, Calendar.JANUARY, 1, 0, 0) - val laterInTheDayUtc = unixTime(2017, Calendar.JANUARY, 1, 20, 0) - val startOfDay = DateUtils.getStartOfDay(laterInTheDayUtc) - assertThat(expectedStartOfDayUtc, equalTo(startOfDay)) - } - @Test fun testGetStartOfToday() { val expectedStartOfDayUtc = unixTime(2017, Calendar.JANUARY, 1, 0, 0)