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

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

@ -31,6 +31,7 @@ kotlin {
implementation(kotlin("stdlib-common")) implementation(kotlin("stdlib-common"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.3.8") implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.3.8")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.2.1") 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 package org.isoron.platform.time
import io.fluidsonic.locale.Locale
import kotlinx.datetime.Clock import kotlinx.datetime.Clock
import kotlinx.datetime.Instant import kotlinx.datetime.Instant
import kotlinx.datetime.TimeZone import kotlinx.datetime.TimeZone
import kotlinx.datetime.offsetAt import kotlinx.datetime.offsetAt
import org.isoron.platform.i18n.getDefault
import kotlin.math.abs import kotlin.math.abs
import kotlin.math.ceil import kotlin.math.ceil
@ -141,6 +143,7 @@ data class LocalDate(val daysSince2000: Int) {
companion object { companion object {
var fixedLocalTime: Long? = null var fixedLocalTime: Long? = null
var fixedTimeZone: TimeZone? = null var fixedTimeZone: TimeZone? = null
var fixedLocale: Locale? = null
fun getLocalTime(testTimeInMillis: Long? = null): Long { fun getLocalTime(testTimeInMillis: Long? = null): Long {
if (fixedLocalTime != null) return fixedLocalTime as Long if (fixedLocalTime != null) return fixedLocalTime as Long
@ -153,6 +156,10 @@ data class LocalDate(val daysSince2000: Int) {
fun getTimeZone(): TimeZone { fun getTimeZone(): TimeZone {
return fixedTimeZone ?: TimeZone.currentSystemDefault() 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.Instant
import kotlinx.datetime.offsetAt 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.getLocalTime
import org.isoron.platform.time.LocalDate.Companion.getTimeZone import org.isoron.platform.time.LocalDate.Companion.getTimeZone
import org.isoron.uhabits.core.models.Timestamp import org.isoron.uhabits.core.models.Timestamp
@ -34,7 +35,6 @@ import kotlin.collections.ArrayList
abstract class DateUtils { abstract class DateUtils {
companion object { companion object {
private var fixedLocale: Locale? = null
private var startDayHourOffset: Int = 0 private var startDayHourOffset: Int = 0
private var startDayMinuteOffset: Int = 0 private var startDayMinuteOffset: Int = 0
@ -229,7 +229,7 @@ abstract class DateUtils {
} }
private fun getLocale(): Locale { private fun getLocale(): Locale {
return fixedLocale ?: Locale.getDefault() return Locale.forLanguageTag(LocalDate.getLocale().toLanguageTag().toString())
} }
@JvmStatic @JvmStatic
@ -308,11 +308,6 @@ abstract class DateUtils {
return applyTimezone(time) return applyTimezone(time)
} }
@JvmStatic
fun setFixedLocale(newLocale: Locale?) {
this.fixedLocale = newLocale
}
} }
enum class TruncateField { enum class TruncateField {

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

Loading…
Cancel
Save