Migrate getWeekdaySequence

pull/1120/head
sgallese 4 years ago
parent febcbd3da8
commit 150dcefb2c

@ -23,12 +23,12 @@ import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import org.isoron.platform.time.LocalDate.Companion.getWeekdaySequence
import org.isoron.uhabits.R
import org.isoron.uhabits.core.models.Timestamp
import org.isoron.uhabits.core.utils.DateUtils.Companion.getShortWeekdayNames
import org.isoron.uhabits.core.utils.DateUtils.Companion.getStartOfTodayCalendar
import org.isoron.uhabits.core.utils.DateUtils.Companion.getStartOfTodayCalendarWithOffset
import org.isoron.uhabits.core.utils.DateUtils.Companion.getWeekdaySequence
import org.isoron.uhabits.utils.ColorUtils.mixColors
import org.isoron.uhabits.utils.StyledResources
import org.isoron.uhabits.utils.toSimpleDataFormat

@ -25,6 +25,7 @@ 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
@ -160,6 +161,28 @@ data class LocalDate(val daysSince2000: Int) {
fun getLocale(): Locale {
return fixedLocale ?: Locale.getDefault()
}
/**
* Returns a vector of exactly seven integers, where the first integer is
* the provided firstWeekday number, and each subsequent number is the
* previous number plus 1, wrapping back to 1 after 7. For example,
* providing 3 as firstWeekday returns {3,4,5,6,7,1,2}
*
* 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<Int> {
return arrayOf(
(firstWeekday - 1) % 7 + 1,
(firstWeekday) % 7 + 1,
(firstWeekday + 1) % 7 + 1,
(firstWeekday + 2) % 7 + 1,
(firstWeekday + 3) % 7 + 1,
(firstWeekday + 4) % 7 + 1,
(firstWeekday + 5) % 7 + 1,
)
}
}
}

@ -22,7 +22,9 @@ package org.isoron.platform.time
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toInstant
import org.isoron.platform.time.LocalDate.Companion.getWeekdaySequence
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
class DatesTest {
@ -46,4 +48,10 @@ class DatesTest {
val expectedUnixTimeForSydney = utcTestTimeInMillis + expectedUnixTimeOffsetForSydney
assertEquals(expectedUnixTimeForSydney, localTimeInMillis)
}
@Test
fun testGetWeekdaySequence() {
val weekdaySequence = getWeekdaySequence(3)
assertContentEquals(arrayOf(3, 4, 5, 6, 7, 1, 2), weekdaySequence)
}
}

@ -119,28 +119,6 @@ abstract class DateUtils {
return daysNullable.toTypedArray()
}
/**
* Returns a vector of exactly seven integers, where the first integer is
* the provided firstWeekday number, and each subsequent number is the
* previous number plus 1, wrapping back to 1 after 7. For example,
* providing 3 as firstWeekday returns {3,4,5,6,7,1,2}
*
* 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<Int> {
return arrayOf(
(firstWeekday - 1) % 7 + 1,
(firstWeekday) % 7 + 1,
(firstWeekday + 1) % 7 + 1,
(firstWeekday + 2) % 7 + 1,
(firstWeekday + 3) % 7 + 1,
(firstWeekday + 4) % 7 + 1,
(firstWeekday + 5) % 7 + 1,
)
}
/**
* @return An integer representing the first day of the week, according to
* the current locale. Sunday corresponds to 1, Monday to 2, and so on,

@ -68,12 +68,6 @@ class DateUtilsTest : BaseUnitTest() {
assertThat(expectedUnixTimeForSydney, equalTo(localTimeInMillis))
}
@Test
fun testGetWeekdaySequence() {
val weekdaySequence = DateUtils.getWeekdaySequence(3)
assertThat(arrayOf(3, 4, 5, 6, 7, 1, 2), equalTo(weekdaySequence))
}
@Test
fun testGetFirstWeekdayNumberAccordingToLocale_germany() {
LocalDate.fixedLocale = Locale.forLanguageTag("de-de")

Loading…
Cancel
Save