pull/1120/head
sgallese 4 years ago
parent 795ba24395
commit febcbd3da8

@ -19,12 +19,12 @@
package org.isoron.uhabits
import com.nhaarman.mockitokotlin2.spy
import org.isoron.platform.time.LocalDate
import org.isoron.uhabits.core.commands.CommandRunner
import org.isoron.uhabits.core.models.HabitList
import org.isoron.uhabits.core.models.memory.MemoryModelFactory
import org.isoron.uhabits.core.tasks.SingleThreadTaskRunner
import org.isoron.uhabits.core.test.HabitFixtures
import org.isoron.uhabits.core.utils.DateUtils.Companion.setFixedLocalTime
import org.isoron.uhabits.core.utils.DateUtils.Companion.setStartDayOffset
import org.junit.After
import org.junit.Before
@ -43,7 +43,7 @@ open class BaseAndroidJVMTest {
@Before
open fun setUp() {
val fixedLocalTime = 1422172800000L
setFixedLocalTime(fixedLocalTime)
LocalDate.fixedLocalTime = fixedLocalTime
setStartDayOffset(0, 0)
modelFactory = MemoryModelFactory()
habitList = spy(modelFactory.buildHabitList())
@ -54,7 +54,7 @@ open class BaseAndroidJVMTest {
@After
fun tearDown() {
setFixedLocalTime(null)
LocalDate.fixedLocalTime = null
setStartDayOffset(0, 0)
}

@ -31,6 +31,7 @@ kotlin {
implementation(kotlin("stdlib-common"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.3.8")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.2.1")
implementation("io.fluidsonic.locale:fluid-locale:0.10.0")
}
}

@ -0,0 +1,24 @@
/*
* Copyright (C) 2016-2021 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.platform.i18n
import io.fluidsonic.locale.Locale
expect fun Locale.Companion.getDefault(): Locale

@ -19,10 +19,12 @@
package org.isoron.platform.time
import io.fluidsonic.locale.Locale
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlinx.datetime.TimeZone
import kotlinx.datetime.offsetAt
import org.isoron.platform.i18n.getDefault
import kotlin.math.abs
import kotlin.math.ceil
@ -141,6 +143,7 @@ data class LocalDate(val daysSince2000: Int) {
companion object {
var fixedLocalTime: Long? = null
var fixedTimeZone: TimeZone? = null
var fixedLocale: Locale? = null
fun getLocalTime(testTimeInMillis: Long? = null): Long {
if (fixedLocalTime != null) return fixedLocalTime as Long
@ -153,6 +156,10 @@ data class LocalDate(val daysSince2000: Int) {
fun getTimeZone(): TimeZone {
return fixedTimeZone ?: TimeZone.currentSystemDefault()
}
fun getLocale(): Locale {
return fixedLocale ?: Locale.getDefault()
}
}
}

@ -0,0 +1,7 @@
package org.isoron.platform.i18n
import io.fluidsonic.locale.Locale
actual fun Locale.Companion.getDefault() =
forLanguageTagOrNull(java.util.Locale.getDefault().toLanguageTag())
?: forLanguageTag("en-us")

@ -20,6 +20,7 @@ package org.isoron.uhabits.core.utils
import kotlinx.datetime.Instant
import kotlinx.datetime.offsetAt
import org.isoron.platform.time.LocalDate
import org.isoron.platform.time.LocalDate.Companion.getLocalTime
import org.isoron.platform.time.LocalDate.Companion.getTimeZone
import org.isoron.uhabits.core.models.Timestamp
@ -34,7 +35,6 @@ import kotlin.collections.ArrayList
abstract class DateUtils {
companion object {
private var fixedLocale: Locale? = null
private var startDayHourOffset: Int = 0
private var startDayMinuteOffset: Int = 0
@ -229,7 +229,7 @@ abstract class DateUtils {
}
private fun getLocale(): Locale {
return fixedLocale ?: Locale.getDefault()
return Locale.forLanguageTag(LocalDate.getLocale().toLanguageTag().toString())
}
@JvmStatic
@ -308,11 +308,6 @@ abstract class DateUtils {
return applyTimezone(time)
}
@JvmStatic
fun setFixedLocale(newLocale: Locale?) {
this.fixedLocale = newLocale
}
}
enum class TruncateField {

@ -18,6 +18,7 @@
*/
package org.isoron.uhabits.core.utils
import io.fluidsonic.locale.Locale
import junit.framework.Assert.assertEquals
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.core.IsEqual.equalTo
@ -30,14 +31,12 @@ import org.isoron.uhabits.core.utils.DateUtils.Companion.getStartOfDayWithOffset
import org.isoron.uhabits.core.utils.DateUtils.Companion.getTodayWithOffset
import org.isoron.uhabits.core.utils.DateUtils.Companion.millisecondsUntilTomorrowWithOffset
import org.isoron.uhabits.core.utils.DateUtils.Companion.removeTimezone
import org.isoron.uhabits.core.utils.DateUtils.Companion.setFixedLocale
import org.isoron.uhabits.core.utils.DateUtils.Companion.setStartDayOffset
import org.isoron.uhabits.core.utils.DateUtils.Companion.truncate
import org.junit.Before
import org.junit.Test
import java.util.Calendar
import java.util.GregorianCalendar
import java.util.Locale
import java.util.TimeZone
class DateUtilsTest : BaseUnitTest() {
@ -47,7 +46,7 @@ class DateUtilsTest : BaseUnitTest() {
@Throws(Exception::class)
override fun setUp() {
super.setUp()
setFixedLocale(Locale.US)
LocalDate.fixedLocale = Locale.forLanguageTag("en-us")
}
@Test
@ -77,42 +76,42 @@ class DateUtilsTest : BaseUnitTest() {
@Test
fun testGetFirstWeekdayNumberAccordingToLocale_germany() {
setFixedLocale(Locale.GERMANY)
LocalDate.fixedLocale = Locale.forLanguageTag("de-de")
val firstWeekdayNumber = DateUtils.getFirstWeekdayNumberAccordingToLocale()
assertThat(2, equalTo(firstWeekdayNumber))
}
@Test
fun testGetFirstWeekdayNumberAccordingToLocale_us() {
setFixedLocale(Locale.US)
LocalDate.fixedLocale = Locale.forLanguageTag("en-us")
val firstWeekdayNumber = DateUtils.getFirstWeekdayNumberAccordingToLocale()
assertThat(1, equalTo(firstWeekdayNumber))
}
@Test
fun testGetLongWeekdayNames_germany() {
setFixedLocale(Locale.GERMANY)
LocalDate.fixedLocale = Locale.forLanguageTag("de-de")
val longWeekdayNames = DateUtils.getLongWeekdayNames(Calendar.SATURDAY)
assertThat(arrayOf("Samstag", "Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag"), equalTo(longWeekdayNames))
}
@Test
fun testGetLongWeekdayNames_us() {
setFixedLocale(Locale.US)
LocalDate.fixedLocale = Locale.forLanguageTag("en-us")
val longWeekdayNames = DateUtils.getLongWeekdayNames(Calendar.SATURDAY)
assertThat(arrayOf("Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"), equalTo(longWeekdayNames))
}
@Test
fun testGetShortWeekdayNames_germany() {
setFixedLocale(Locale.GERMANY)
LocalDate.fixedLocale = Locale.forLanguageTag("de-de")
val longWeekdayNames = DateUtils.getShortWeekdayNames(Calendar.SATURDAY)
assertThat(arrayOf("Sa.", "So.", "Mo.", "Di.", "Mi.", "Do.", "Fr."), equalTo(longWeekdayNames))
}
@Test
fun testGetShortWeekdayNames_us() {
setFixedLocale(Locale.US)
LocalDate.fixedLocale = Locale.forLanguageTag("en-us")
val longWeekdayNames = DateUtils.getShortWeekdayNames(Calendar.SATURDAY)
assertThat(arrayOf("Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"), equalTo(longWeekdayNames))
}
@ -317,9 +316,9 @@ class DateUtilsTest : BaseUnitTest() {
@Test
fun testGetStartOfTodayCalendar() {
LocalDate.fixedLocalTime = FIXED_LOCAL_TIME
setFixedLocale(Locale.GERMANY)
LocalDate.fixedLocale = Locale.forLanguageTag("de-de")
val expectedStartOfDay = unixTime(2015, Calendar.JANUARY, 25, 0, 0)
val expectedCalendar = GregorianCalendar(TimeZone.getTimeZone("GMT"), Locale.GERMANY)
val expectedCalendar = GregorianCalendar(TimeZone.getTimeZone("GMT"), java.util.Locale.GERMANY)
expectedCalendar.timeInMillis = expectedStartOfDay
val startOfTodayCalendar = DateUtils.getStartOfTodayCalendar()
assertThat(expectedCalendar, equalTo(startOfTodayCalendar))
@ -331,9 +330,9 @@ class DateUtilsTest : BaseUnitTest() {
setStartDayOffset(hourOffset, 0)
val priorToOffset = unixTime(2017, Calendar.JANUARY, 2, hourOffset - 1, 0)
LocalDate.fixedLocalTime = priorToOffset
setFixedLocale(Locale.GERMANY)
LocalDate.fixedLocale = Locale.forLanguageTag("de-de")
val startOfYesterday = unixTime(2017, Calendar.JANUARY, 2, 0, 0)
val expectedCalendar = GregorianCalendar(TimeZone.getTimeZone("GMT"), Locale.GERMANY)
val expectedCalendar = GregorianCalendar(TimeZone.getTimeZone("GMT"), java.util.Locale.GERMANY)
expectedCalendar.timeInMillis = startOfYesterday
val startOfTodayCalendar = DateUtils.getStartOfTodayCalendar()
assertThat(expectedCalendar, equalTo(startOfTodayCalendar))
@ -345,9 +344,9 @@ class DateUtilsTest : BaseUnitTest() {
setStartDayOffset(hourOffset, 0)
val afterOffset = unixTime(2017, Calendar.JANUARY, 1, hourOffset + 1, 0)
LocalDate.fixedLocalTime = afterOffset
setFixedLocale(Locale.GERMANY)
LocalDate.fixedLocale = Locale.forLanguageTag("de-de")
val startOfToday = unixTime(2017, Calendar.JANUARY, 1, 0, 0)
val expectedCalendar = GregorianCalendar(TimeZone.getTimeZone("GMT"), Locale.GERMANY)
val expectedCalendar = GregorianCalendar(TimeZone.getTimeZone("GMT"), java.util.Locale.GERMANY)
expectedCalendar.timeInMillis = startOfToday
val startOfTodayCalendar = DateUtils.getStartOfTodayCalendar()
assertThat(expectedCalendar, equalTo(startOfTodayCalendar))

Loading…
Cancel
Save