Migrate getLocalTime and getTimeZone to common module

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

@ -29,13 +29,13 @@ import androidx.test.uiautomator.UiDevice
import junit.framework.TestCase import junit.framework.TestCase
import org.hamcrest.CoreMatchers.hasItems import org.hamcrest.CoreMatchers.hasItems
import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.MatcherAssert.assertThat
import org.isoron.platform.time.LocalDate
import org.isoron.uhabits.core.models.HabitList import org.isoron.uhabits.core.models.HabitList
import org.isoron.uhabits.core.models.ModelFactory import org.isoron.uhabits.core.models.ModelFactory
import org.isoron.uhabits.core.models.Timestamp import org.isoron.uhabits.core.models.Timestamp
import org.isoron.uhabits.core.preferences.Preferences import org.isoron.uhabits.core.preferences.Preferences
import org.isoron.uhabits.core.tasks.TaskRunner import org.isoron.uhabits.core.tasks.TaskRunner
import org.isoron.uhabits.core.utils.DateUtils.Companion.getToday import org.isoron.uhabits.core.utils.DateUtils.Companion.getToday
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.isoron.uhabits.inject.ActivityContextModule import org.isoron.uhabits.inject.ActivityContextModule
import org.isoron.uhabits.inject.AppContextModule import org.isoron.uhabits.inject.AppContextModule
@ -75,7 +75,7 @@ abstract class BaseAndroidTest : TestCase() {
public override fun setUp() { public override fun setUp() {
if (Looper.myLooper() == null) Looper.prepare() if (Looper.myLooper() == null) Looper.prepare()
device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
setFixedLocalTime(FIXED_LOCAL_TIME) LocalDate.Companion.fixedLocalTime = FIXED_LOCAL_TIME
setStartDayOffset(0, 0) setStartDayOffset(0, 0)
setResolution(2.0f) setResolution(2.0f)
setTheme(R.style.AppBaseTheme) setTheme(R.style.AppBaseTheme)

@ -19,6 +19,7 @@
package org.isoron.uhabits.database package org.isoron.uhabits.database
import org.isoron.platform.time.LocalDate
import org.isoron.uhabits.AndroidDirFinder import org.isoron.uhabits.AndroidDirFinder
import org.isoron.uhabits.BaseAndroidTest import org.isoron.uhabits.BaseAndroidTest
import org.isoron.uhabits.core.utils.DateUtils import org.isoron.uhabits.core.utils.DateUtils
@ -29,7 +30,7 @@ import java.io.FileOutputStream
class AutoBackupTest : BaseAndroidTest() { class AutoBackupTest : BaseAndroidTest() {
@Test @Test
fun testRun() { fun testRun() {
DateUtils.setFixedLocalTime(40 * DateUtils.DAY_LENGTH) LocalDate.fixedLocalTime = 40 * DateUtils.DAY_LENGTH
val basedir = AndroidDirFinder(targetContext).getFilesDir("Backups")!! val basedir = AndroidDirFinder(targetContext).getFilesDir("Backups")!!
createTestFiles(basedir, 30) createTestFiles(basedir, 30)

@ -21,6 +21,7 @@ package org.isoron.uhabits.database
import android.content.Context import android.content.Context
import android.util.Log import android.util.Log
import org.isoron.platform.time.LocalDate
import org.isoron.uhabits.AndroidDirFinder import org.isoron.uhabits.AndroidDirFinder
import org.isoron.uhabits.core.utils.DateUtils import org.isoron.uhabits.core.utils.DateUtils
import org.isoron.uhabits.utils.DatabaseUtils import org.isoron.uhabits.utils.DatabaseUtils
@ -37,7 +38,7 @@ class AutoBackup(private val context: Context) {
if (files.isNotEmpty()) { if (files.isNotEmpty()) {
newestTimestamp = files.last().lastModified() newestTimestamp = files.last().lastModified()
} }
val now = DateUtils.getLocalTime() val now = LocalDate.getLocalTime()
removeOldest(files, keep) removeOldest(files, keep)
if (now - newestTimestamp > DateUtils.DAY_LENGTH) { if (now - newestTimestamp > DateUtils.DAY_LENGTH) {
DatabaseUtils.saveDatabaseCopy(context, basedir) DatabaseUtils.saveDatabaseCopy(context, basedir)

@ -21,12 +21,12 @@ package org.isoron.uhabits.utils
import android.content.Context import android.content.Context
import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteDatabase
import android.util.Log import android.util.Log
import org.isoron.platform.time.LocalDate.Companion.getLocalTime
import org.isoron.uhabits.HabitsApplication.Companion.isTestMode import org.isoron.uhabits.HabitsApplication.Companion.isTestMode
import org.isoron.uhabits.HabitsDatabaseOpener import org.isoron.uhabits.HabitsDatabaseOpener
import org.isoron.uhabits.core.DATABASE_FILENAME import org.isoron.uhabits.core.DATABASE_FILENAME
import org.isoron.uhabits.core.DATABASE_VERSION import org.isoron.uhabits.core.DATABASE_VERSION
import org.isoron.uhabits.core.utils.DateFormats.Companion.getBackupDateFormat import org.isoron.uhabits.core.utils.DateFormats.Companion.getBackupDateFormat
import org.isoron.uhabits.core.utils.DateUtils.Companion.getLocalTime
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
import java.text.SimpleDateFormat import java.text.SimpleDateFormat

@ -30,6 +30,7 @@ kotlin {
dependencies { dependencies {
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")
} }
} }

@ -19,6 +19,10 @@
package org.isoron.platform.time package org.isoron.platform.time
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlinx.datetime.TimeZone
import kotlinx.datetime.offsetAt
import kotlin.math.abs import kotlin.math.abs
import kotlin.math.ceil import kotlin.math.ceil
@ -133,6 +137,23 @@ data class LocalDate(val daysSince2000: Int) {
override fun toString(): String { override fun toString(): String {
return "LocalDate($year-$month-$day)" return "LocalDate($year-$month-$day)"
} }
companion object {
var fixedLocalTime: Long? = null
var fixedTimeZone: TimeZone? = null
fun getLocalTime(testTimeInMillis: Long? = null): Long {
if (fixedLocalTime != null) return fixedLocalTime as Long
val tz = getTimeZone()
val now = testTimeInMillis ?: Clock.System.now().toEpochMilliseconds()
return now + (tz.offsetAt(Instant.fromEpochMilliseconds(now)).totalSeconds * 1000)
}
fun getTimeZone(): TimeZone {
return fixedTimeZone ?: TimeZone.currentSystemDefault()
}
}
} }
interface LocalDateFormatter { interface LocalDateFormatter {

@ -0,0 +1,49 @@
/*
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
*
* 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.time
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toInstant
import kotlin.test.Test
import kotlin.test.assertEquals
class DatesTest {
@Test
fun testDatesBefore2000() {
val date = LocalDate(-1)
assertEquals(31, date.day)
assertEquals(12, date.month)
assertEquals(1999, date.year)
}
@Test
fun testGetLocalTime() {
LocalDate.fixedLocalTime = null
LocalDate.fixedTimeZone = TimeZone.of("Australia/Sydney")
val utcTestTimeInMillis = LocalDateTime(
2015, 1, 11, 0, 0, 0, 0
).toInstant(TimeZone.UTC).toEpochMilliseconds()
val localTimeInMillis = LocalDate.getLocalTime(utcTestTimeInMillis)
val expectedUnixTimeOffsetForSydney = 11 * 60 * 60 * 1000
val expectedUnixTimeForSydney = utcTestTimeInMillis + expectedUnixTimeOffsetForSydney
assertEquals(expectedUnixTimeForSydney, localTimeInMillis)
}
}

@ -18,6 +18,7 @@
*/ */
package org.isoron.uhabits.core.reminders package org.isoron.uhabits.core.reminders
import org.isoron.platform.time.LocalDate.Companion.getLocalTime
import org.isoron.uhabits.core.AppScope import org.isoron.uhabits.core.AppScope
import org.isoron.uhabits.core.commands.ChangeHabitColorCommand import org.isoron.uhabits.core.commands.ChangeHabitColorCommand
import org.isoron.uhabits.core.commands.Command import org.isoron.uhabits.core.commands.Command
@ -28,7 +29,6 @@ import org.isoron.uhabits.core.models.HabitList
import org.isoron.uhabits.core.models.HabitMatcher import org.isoron.uhabits.core.models.HabitMatcher
import org.isoron.uhabits.core.preferences.WidgetPreferences import org.isoron.uhabits.core.preferences.WidgetPreferences
import org.isoron.uhabits.core.utils.DateUtils.Companion.applyTimezone import org.isoron.uhabits.core.utils.DateUtils.Companion.applyTimezone
import org.isoron.uhabits.core.utils.DateUtils.Companion.getLocalTime
import org.isoron.uhabits.core.utils.DateUtils.Companion.getStartOfDayWithOffset import org.isoron.uhabits.core.utils.DateUtils.Companion.getStartOfDayWithOffset
import org.isoron.uhabits.core.utils.DateUtils.Companion.removeTimezone import org.isoron.uhabits.core.utils.DateUtils.Companion.removeTimezone
import java.util.Locale import java.util.Locale

@ -18,20 +18,22 @@
*/ */
package org.isoron.uhabits.core.utils package org.isoron.uhabits.core.utils
import kotlinx.datetime.Instant
import kotlinx.datetime.offsetAt
import org.isoron.platform.time.LocalDate.Companion.getLocalTime
import org.isoron.platform.time.LocalDate.Companion.getTimeZone
import org.isoron.uhabits.core.models.Timestamp import org.isoron.uhabits.core.models.Timestamp
import java.util.Calendar import java.util.Calendar
import java.util.Calendar.DAY_OF_MONTH import java.util.Calendar.DAY_OF_MONTH
import java.util.Calendar.DAY_OF_WEEK import java.util.Calendar.DAY_OF_WEEK
import java.util.Calendar.SHORT import java.util.Calendar.SHORT
import java.util.Date
import java.util.GregorianCalendar import java.util.GregorianCalendar
import java.util.Locale import java.util.Locale
import java.util.TimeZone import java.util.TimeZone
import kotlin.collections.ArrayList
abstract class DateUtils { abstract class DateUtils {
companion object { companion object {
private var fixedLocalTime: Long? = null
private var fixedTimeZone: TimeZone? = null
private var fixedLocale: Locale? = null 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
@ -58,8 +60,15 @@ abstract class DateUtils {
@JvmStatic @JvmStatic
fun applyTimezone(localTimestamp: Long): Long { fun applyTimezone(localTimestamp: Long): Long {
val tz: TimeZone = getTimeZone() val tz = getTimeZone()
return localTimestamp - tz.getOffset(localTimestamp - tz.getOffset(localTimestamp)) val offset = tz.offsetAt(
Instant.fromEpochMilliseconds(localTimestamp)
).totalSeconds * 1000
val difference = localTimestamp - offset
val offsetDifference = tz.offsetAt(
Instant.fromEpochMilliseconds(difference)
).totalSeconds * 1000
return localTimestamp - offsetDifference
} }
@JvmStatic @JvmStatic
@ -76,15 +85,6 @@ abstract class DateUtils {
return day return day
} }
@JvmStatic
fun getLocalTime(testTimeInMillis: Long? = null): Long {
if (fixedLocalTime != null) return fixedLocalTime as Long
val tz = getTimeZone()
val now = testTimeInMillis ?: Date().time
return now + tz.getOffset(now)
}
/** /**
* Returns an array of strings with the names for each day of the week, * Returns an array of strings with the names for each day of the week,
* in either SHORT or LONG format. The first entry corresponds to the * in either SHORT or LONG format. The first entry corresponds to the
@ -206,22 +206,20 @@ abstract class DateUtils {
fun getStartOfTodayWithOffset(): Long = getStartOfDayWithOffset(getLocalTime()) fun getStartOfTodayWithOffset(): Long = getStartOfDayWithOffset(getLocalTime())
@JvmStatic @JvmStatic
fun millisecondsUntilTomorrowWithOffset(): Long = getStartOfTomorrowWithOffset() - getLocalTime() fun millisecondsUntilTomorrowWithOffset(): Long =
getStartOfTomorrowWithOffset() - getLocalTime()
@JvmStatic @JvmStatic
fun getStartOfTodayCalendar(): GregorianCalendar = getCalendar(getStartOfToday()) fun getStartOfTodayCalendar(): GregorianCalendar = getCalendar(getStartOfToday())
@JvmStatic @JvmStatic
fun getStartOfTodayCalendarWithOffset(): GregorianCalendar = getCalendar(getStartOfTodayWithOffset()) fun getStartOfTodayCalendarWithOffset(): GregorianCalendar =
getCalendar(getStartOfTodayWithOffset())
private fun getTimeZone(): TimeZone {
return fixedTimeZone ?: TimeZone.getDefault()
}
@JvmStatic @JvmStatic
fun removeTimezone(timestamp: Long): Long { fun removeTimezone(timestamp: Long): Long {
val tz = getTimeZone() val tz = getTimeZone()
return timestamp + tz.getOffset(timestamp) return timestamp + (tz.offsetAt(Instant.fromEpochMilliseconds(timestamp)).totalSeconds * 1000)
} }
@JvmStatic @JvmStatic
@ -259,7 +257,9 @@ abstract class DateUtils {
return when (field) { return when (field) {
TruncateField.DAY -> { cal.timeInMillis } TruncateField.DAY -> {
cal.timeInMillis
}
TruncateField.MONTH -> { TruncateField.MONTH -> {
cal.set(DAY_OF_MONTH, 1) cal.set(DAY_OF_MONTH, 1)
@ -269,7 +269,9 @@ abstract class DateUtils {
TruncateField.WEEK_NUMBER -> { TruncateField.WEEK_NUMBER -> {
val weekDay = cal.get(DAY_OF_WEEK) val weekDay = cal.get(DAY_OF_WEEK)
var delta = weekDay - firstWeekday var delta = weekDay - firstWeekday
if (delta < 0) { delta += 7 } if (delta < 0) {
delta += 7
}
cal.add(Calendar.DAY_OF_YEAR, -delta) cal.add(Calendar.DAY_OF_YEAR, -delta)
cal.timeInMillis cal.timeInMillis
} }
@ -307,16 +309,6 @@ abstract class DateUtils {
return applyTimezone(time) return applyTimezone(time)
} }
@JvmStatic
fun setFixedLocalTime(newFixedLocalTime: Long?) {
this.fixedLocalTime = newFixedLocalTime
}
@JvmStatic
fun setFixedTimeZone(newTimeZone: TimeZone?) {
this.fixedTimeZone = newTimeZone
}
@JvmStatic @JvmStatic
fun setFixedLocale(newLocale: Locale?) { fun setFixedLocale(newLocale: Locale?) {
this.fixedLocale = newLocale this.fixedLocale = newLocale

@ -1,34 +0,0 @@
/*
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
*
* 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.gui
import org.isoron.platform.time.LocalDate
import org.junit.Assert.assertEquals
import org.junit.Test
class DatesTest {
@Test
fun testDatesBefore2000() {
val date = LocalDate(-1)
assertEquals(date.day, 31)
assertEquals(date.month, 12)
assertEquals(date.year, 1999)
}
}

@ -21,6 +21,7 @@ package org.isoron.uhabits.core
import com.nhaarman.mockitokotlin2.spy import com.nhaarman.mockitokotlin2.spy
import com.nhaarman.mockitokotlin2.validateMockitoUsage import com.nhaarman.mockitokotlin2.validateMockitoUsage
import org.apache.commons.io.IOUtils import org.apache.commons.io.IOUtils
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.database.Database import org.isoron.uhabits.core.database.Database
import org.isoron.uhabits.core.database.DatabaseOpener import org.isoron.uhabits.core.database.DatabaseOpener
@ -33,7 +34,6 @@ 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.getStartOfTodayCalendar import org.isoron.uhabits.core.utils.DateUtils.Companion.getStartOfTodayCalendar
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
@ -76,7 +76,7 @@ open class BaseUnitTest {
@Before @Before
@Throws(Exception::class) @Throws(Exception::class)
open fun setUp() { open fun setUp() {
setFixedLocalTime(FIXED_LOCAL_TIME) LocalDate.fixedLocalTime = FIXED_LOCAL_TIME
setStartDayOffset(0, 0) setStartDayOffset(0, 0)
val memoryModelFactory = MemoryModelFactory() val memoryModelFactory = MemoryModelFactory()
habitList = spy(memoryModelFactory.buildHabitList()) habitList = spy(memoryModelFactory.buildHabitList())
@ -90,7 +90,7 @@ open class BaseUnitTest {
@Throws(Exception::class) @Throws(Exception::class)
open fun tearDown() { open fun tearDown() {
validateMockitoUsage() validateMockitoUsage()
setFixedLocalTime(null) LocalDate.fixedLocalTime = null
setStartDayOffset(0, 0) setStartDayOffset(0, 0)
} }

@ -22,13 +22,13 @@ import junit.framework.Assert.assertFalse
import junit.framework.Assert.assertTrue import junit.framework.Assert.assertTrue
import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.core.IsEqual.equalTo import org.hamcrest.core.IsEqual.equalTo
import org.isoron.platform.time.LocalDate
import org.isoron.uhabits.core.BaseUnitTest import org.isoron.uhabits.core.BaseUnitTest
import org.isoron.uhabits.core.models.Entry import org.isoron.uhabits.core.models.Entry
import org.isoron.uhabits.core.models.Frequency import org.isoron.uhabits.core.models.Frequency
import org.isoron.uhabits.core.models.Habit import org.isoron.uhabits.core.models.Habit
import org.isoron.uhabits.core.models.Timestamp import org.isoron.uhabits.core.models.Timestamp
import org.isoron.uhabits.core.utils.DateUtils.Companion.getStartOfTodayCalendar import org.isoron.uhabits.core.utils.DateUtils.Companion.getStartOfTodayCalendar
import org.isoron.uhabits.core.utils.DateUtils.Companion.setFixedLocalTime
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
import java.io.File import java.io.File
@ -39,7 +39,7 @@ class ImportTest : BaseUnitTest() {
@Throws(Exception::class) @Throws(Exception::class)
override fun setUp() { override fun setUp() {
super.setUp() super.setUp()
setFixedLocalTime(null) LocalDate.fixedLocalTime = null
} }
@Test @Test

@ -22,6 +22,7 @@ import com.nhaarman.mockitokotlin2.eq
import com.nhaarman.mockitokotlin2.mock import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.verify import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever import com.nhaarman.mockitokotlin2.whenever
import org.isoron.platform.time.LocalDate
import org.isoron.uhabits.core.BaseUnitTest import org.isoron.uhabits.core.BaseUnitTest
import org.isoron.uhabits.core.models.Habit import org.isoron.uhabits.core.models.Habit
import org.isoron.uhabits.core.models.Reminder import org.isoron.uhabits.core.models.Reminder
@ -30,8 +31,6 @@ import org.isoron.uhabits.core.preferences.WidgetPreferences
import org.isoron.uhabits.core.utils.DateUtils.Companion.applyTimezone import org.isoron.uhabits.core.utils.DateUtils.Companion.applyTimezone
import org.isoron.uhabits.core.utils.DateUtils.Companion.getStartOfTodayCalendar import org.isoron.uhabits.core.utils.DateUtils.Companion.getStartOfTodayCalendar
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.setFixedLocalTime
import org.isoron.uhabits.core.utils.DateUtils.Companion.setFixedTimeZone
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
@ -57,13 +56,13 @@ class ReminderSchedulerTest : BaseUnitTest() {
habit.id = habitId habit.id = habitId
reminderScheduler = reminderScheduler =
ReminderScheduler(commandRunner, habitList, sys, widgetPreferences) ReminderScheduler(commandRunner, habitList, sys, widgetPreferences)
setFixedTimeZone(TimeZone.getTimeZone("GMT-4")) LocalDate.Companion.fixedTimeZone = kotlinx.datetime.TimeZone.of("GMT-4")
} }
@Test @Test
fun testScheduleAll() { fun testScheduleAll() {
val now = unixTime(2015, 1, 26, 13, 0) val now = unixTime(2015, 1, 26, 13, 0)
setFixedLocalTime(now) LocalDate.Companion.fixedLocalTime = now
val h1 = fixtures.createEmptyHabit() val h1 = fixtures.createEmptyHabit()
val h2 = fixtures.createEmptyHabit() val h2 = fixtures.createEmptyHabit()
val h3 = fixtures.createEmptyHabit() val h3 = fixtures.createEmptyHabit()
@ -97,7 +96,7 @@ class ReminderSchedulerTest : BaseUnitTest() {
@Test @Test
fun testSchedule_withSnooze() { fun testSchedule_withSnooze() {
val now = removeTimezone(unixTime(2015, 1, 1, 15, 0)) val now = removeTimezone(unixTime(2015, 1, 1, 15, 0))
setFixedLocalTime(now) LocalDate.fixedLocalTime = now
val snoozeTimeInFuture = unixTime(2015, 1, 1, 21, 0) val snoozeTimeInFuture = unixTime(2015, 1, 1, 21, 0)
val snoozeTimeInPast = unixTime(2015, 1, 1, 7, 0) val snoozeTimeInPast = unixTime(2015, 1, 1, 7, 0)
val regularReminderTime = applyTimezone(unixTime(2015, 1, 2, 8, 30)) val regularReminderTime = applyTimezone(unixTime(2015, 1, 2, 8, 30))
@ -116,7 +115,7 @@ class ReminderSchedulerTest : BaseUnitTest() {
@Test @Test
fun testSchedule_laterToday() { fun testSchedule_laterToday() {
val now = unixTime(2015, 1, 26, 6, 30) val now = unixTime(2015, 1, 26, 6, 30)
setFixedLocalTime(now) LocalDate.fixedLocalTime = now
val expectedCheckmarkTime = unixTime(2015, 1, 26, 0, 0) val expectedCheckmarkTime = unixTime(2015, 1, 26, 0, 0)
val expectedReminderTime = unixTime(2015, 1, 26, 12, 30) val expectedReminderTime = unixTime(2015, 1, 26, 12, 30)
habit.reminder = Reminder(8, 30, WeekdayList.EVERY_DAY) habit.reminder = Reminder(8, 30, WeekdayList.EVERY_DAY)
@ -126,7 +125,7 @@ class ReminderSchedulerTest : BaseUnitTest() {
@Test @Test
fun testSchedule_tomorrow() { fun testSchedule_tomorrow() {
val now = unixTime(2015, 1, 26, 13, 0) val now = unixTime(2015, 1, 26, 13, 0)
setFixedLocalTime(now) LocalDate.fixedLocalTime = now
val expectedCheckmarkTime = unixTime(2015, 1, 27, 0, 0) val expectedCheckmarkTime = unixTime(2015, 1, 27, 0, 0)
val expectedReminderTime = unixTime(2015, 1, 27, 12, 30) val expectedReminderTime = unixTime(2015, 1, 27, 12, 30)
habit.reminder = Reminder(8, 30, WeekdayList.EVERY_DAY) habit.reminder = Reminder(8, 30, WeekdayList.EVERY_DAY)

@ -21,6 +21,7 @@ package org.isoron.uhabits.core.utils
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
import org.isoron.platform.time.LocalDate
import org.isoron.uhabits.core.BaseUnitTest import org.isoron.uhabits.core.BaseUnitTest
import org.isoron.uhabits.core.models.Timestamp import org.isoron.uhabits.core.models.Timestamp
import org.isoron.uhabits.core.utils.DateUtils.Companion.applyTimezone import org.isoron.uhabits.core.utils.DateUtils.Companion.applyTimezone
@ -29,9 +30,7 @@ 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.setFixedLocalTime
import org.isoron.uhabits.core.utils.DateUtils.Companion.setFixedLocale import org.isoron.uhabits.core.utils.DateUtils.Companion.setFixedLocale
import org.isoron.uhabits.core.utils.DateUtils.Companion.setFixedTimeZone
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
@ -61,10 +60,10 @@ class DateUtilsTest : BaseUnitTest() {
@Test @Test
fun testGetLocalTime() { fun testGetLocalTime() {
setFixedLocalTime(null) LocalDate.fixedLocalTime = null
setFixedTimeZone(TimeZone.getTimeZone("Australia/Sydney")) LocalDate.fixedTimeZone = kotlinx.datetime.TimeZone.of("Australia/Sydney")
val utcTestTimeInMillis = unixTime(2015, Calendar.JANUARY, 11) val utcTestTimeInMillis = unixTime(2015, Calendar.JANUARY, 11)
val localTimeInMillis = DateUtils.getLocalTime(utcTestTimeInMillis) val localTimeInMillis = LocalDate.getLocalTime(utcTestTimeInMillis)
val expectedUnixTimeOffsetForSydney = 11 * 60 * 60 * 1000 val expectedUnixTimeOffsetForSydney = 11 * 60 * 60 * 1000
val expectedUnixTimeForSydney = utcTestTimeInMillis + expectedUnixTimeOffsetForSydney val expectedUnixTimeForSydney = utcTestTimeInMillis + expectedUnixTimeOffsetForSydney
assertThat(expectedUnixTimeForSydney, equalTo(localTimeInMillis)) assertThat(expectedUnixTimeForSydney, equalTo(localTimeInMillis))
@ -120,7 +119,7 @@ class DateUtilsTest : BaseUnitTest() {
@Test @Test
fun testGetToday() { fun testGetToday() {
setFixedLocalTime(FIXED_LOCAL_TIME) LocalDate.fixedLocalTime = FIXED_LOCAL_TIME
val today = DateUtils.getToday() val today = DateUtils.getToday()
assertThat(Timestamp(FIXED_LOCAL_TIME), equalTo(today)) assertThat(Timestamp(FIXED_LOCAL_TIME), equalTo(today))
} }
@ -137,7 +136,7 @@ class DateUtilsTest : BaseUnitTest() {
fun testGetStartOfToday() { fun testGetStartOfToday() {
val expectedStartOfDayUtc = unixTime(2017, Calendar.JANUARY, 1, 0, 0) val expectedStartOfDayUtc = unixTime(2017, Calendar.JANUARY, 1, 0, 0)
val laterInTheDayUtc = unixTime(2017, Calendar.JANUARY, 1, 20, 0) val laterInTheDayUtc = unixTime(2017, Calendar.JANUARY, 1, 20, 0)
setFixedLocalTime(laterInTheDayUtc) LocalDate.fixedLocalTime = laterInTheDayUtc
val startOfToday = DateUtils.getStartOfToday() val startOfToday = DateUtils.getStartOfToday()
assertThat(expectedStartOfDayUtc, equalTo(startOfToday)) assertThat(expectedStartOfDayUtc, equalTo(startOfToday))
} }
@ -146,10 +145,10 @@ class DateUtilsTest : BaseUnitTest() {
fun testGetStartOfTomorrowWithOffset_priorToOffset() { fun testGetStartOfTomorrowWithOffset_priorToOffset() {
val hourOffset = 3 val hourOffset = 3
setStartDayOffset(hourOffset, 0) setStartDayOffset(hourOffset, 0)
setFixedTimeZone(TimeZone.getTimeZone("GMT")) LocalDate.fixedTimeZone = kotlinx.datetime.TimeZone.UTC
val startOfTomorrowWithOffset = unixTime(2017, Calendar.JANUARY, 1, hourOffset, 0) val startOfTomorrowWithOffset = unixTime(2017, Calendar.JANUARY, 1, hourOffset, 0)
val priorToOffset = unixTime(2017, Calendar.JANUARY, 1, hourOffset - 1, 0) val priorToOffset = unixTime(2017, Calendar.JANUARY, 1, hourOffset - 1, 0)
setFixedLocalTime(priorToOffset) LocalDate.fixedLocalTime = priorToOffset
val startOfTomorrow = DateUtils.getStartOfTomorrowWithOffset() val startOfTomorrow = DateUtils.getStartOfTomorrowWithOffset()
assertThat(startOfTomorrowWithOffset, equalTo(startOfTomorrow)) assertThat(startOfTomorrowWithOffset, equalTo(startOfTomorrow))
} }
@ -158,10 +157,10 @@ class DateUtilsTest : BaseUnitTest() {
fun testGetStartOfTomorrowWithOffset_afterOffset() { fun testGetStartOfTomorrowWithOffset_afterOffset() {
val hourOffset = 3 val hourOffset = 3
setStartDayOffset(hourOffset, 0) setStartDayOffset(hourOffset, 0)
setFixedTimeZone(TimeZone.getTimeZone("GMT")) LocalDate.fixedTimeZone = kotlinx.datetime.TimeZone.UTC
val startOfTomorrowWithOffset = unixTime(2017, Calendar.JANUARY, 2, hourOffset, 0) val startOfTomorrowWithOffset = unixTime(2017, Calendar.JANUARY, 2, hourOffset, 0)
val afterOffset = unixTime(2017, Calendar.JANUARY, 1, hourOffset + 1, 0) val afterOffset = unixTime(2017, Calendar.JANUARY, 1, hourOffset + 1, 0)
setFixedLocalTime(afterOffset) LocalDate.fixedLocalTime = afterOffset
val startOfTomorrow = DateUtils.getStartOfTomorrowWithOffset() val startOfTomorrow = DateUtils.getStartOfTomorrowWithOffset()
assertThat(startOfTomorrowWithOffset, equalTo(startOfTomorrow)) assertThat(startOfTomorrowWithOffset, equalTo(startOfTomorrow))
} }
@ -170,10 +169,10 @@ class DateUtilsTest : BaseUnitTest() {
fun testGetStartOfTodayWithOffset_priorToOffset() { fun testGetStartOfTodayWithOffset_priorToOffset() {
val hourOffset = 3 val hourOffset = 3
setStartDayOffset(hourOffset, 0) setStartDayOffset(hourOffset, 0)
setFixedTimeZone(TimeZone.getTimeZone("GMT")) LocalDate.fixedTimeZone = kotlinx.datetime.TimeZone.UTC
val startOfYesterday = unixTime(2017, Calendar.JANUARY, 1, 0, 0) val startOfYesterday = unixTime(2017, Calendar.JANUARY, 1, 0, 0)
val priorToOffset = unixTime(2017, Calendar.JANUARY, 2, hourOffset - 1, 0) val priorToOffset = unixTime(2017, Calendar.JANUARY, 2, hourOffset - 1, 0)
setFixedLocalTime(priorToOffset) LocalDate.fixedLocalTime = priorToOffset
val startOfTodayWithOffset = DateUtils.getStartOfTodayWithOffset() val startOfTodayWithOffset = DateUtils.getStartOfTodayWithOffset()
assertThat(startOfYesterday, equalTo(startOfTodayWithOffset)) assertThat(startOfYesterday, equalTo(startOfTodayWithOffset))
} }
@ -182,10 +181,10 @@ class DateUtilsTest : BaseUnitTest() {
fun testGetStartOfTodayWithOffset_afterOffset() { fun testGetStartOfTodayWithOffset_afterOffset() {
val hourOffset = 3 val hourOffset = 3
setStartDayOffset(hourOffset, 0) setStartDayOffset(hourOffset, 0)
setFixedTimeZone(TimeZone.getTimeZone("GMT")) LocalDate.fixedTimeZone = kotlinx.datetime.TimeZone.UTC
val startOfToday = unixTime(2017, Calendar.JANUARY, 1, 0, 0) val startOfToday = unixTime(2017, Calendar.JANUARY, 1, 0, 0)
val afterOffset = unixTime(2017, Calendar.JANUARY, 1, hourOffset + 1, 0) val afterOffset = unixTime(2017, Calendar.JANUARY, 1, hourOffset + 1, 0)
setFixedLocalTime(afterOffset) LocalDate.fixedLocalTime = afterOffset
val startOfTodayWithOffset = DateUtils.getStartOfTodayWithOffset() val startOfTodayWithOffset = DateUtils.getStartOfTodayWithOffset()
assertThat(startOfToday, equalTo(startOfTodayWithOffset)) assertThat(startOfToday, equalTo(startOfTodayWithOffset))
} }
@ -284,8 +283,8 @@ class DateUtilsTest : BaseUnitTest() {
@Test @Test
fun testGetUpcomingTimeInMillis() { fun testGetUpcomingTimeInMillis() {
setFixedLocalTime(FIXED_LOCAL_TIME) LocalDate.fixedLocalTime = FIXED_LOCAL_TIME
setFixedTimeZone(TimeZone.getTimeZone("GMT")) LocalDate.fixedTimeZone = kotlinx.datetime.TimeZone.UTC
val expected = unixTime(2015, Calendar.JANUARY, 25, 10, 1) val expected = unixTime(2015, Calendar.JANUARY, 25, 10, 1)
val upcomingTimeMillis = DateUtils.getUpcomingTimeInMillis(10, 1) val upcomingTimeMillis = DateUtils.getUpcomingTimeInMillis(10, 1)
assertThat(expected, equalTo(upcomingTimeMillis)) assertThat(expected, equalTo(upcomingTimeMillis))
@ -294,21 +293,21 @@ class DateUtilsTest : BaseUnitTest() {
@Test @Test
@Throws(Exception::class) @Throws(Exception::class)
fun testMillisecondsUntilTomorrow() { fun testMillisecondsUntilTomorrow() {
setFixedTimeZone(TimeZone.getTimeZone("GMT")) LocalDate.fixedTimeZone = kotlinx.datetime.TimeZone.UTC
setFixedLocalTime(unixTime(2017, Calendar.JANUARY, 1, 23, 59)) LocalDate.fixedLocalTime = unixTime(2017, Calendar.JANUARY, 1, 23, 59)
assertThat(millisecondsUntilTomorrowWithOffset(), equalTo(DateUtils.MINUTE_LENGTH)) assertThat(millisecondsUntilTomorrowWithOffset(), equalTo(DateUtils.MINUTE_LENGTH))
setFixedLocalTime(unixTime(2017, Calendar.JANUARY, 1, 20, 0)) LocalDate.fixedLocalTime = unixTime(2017, Calendar.JANUARY, 1, 20, 0)
assertThat( assertThat(
millisecondsUntilTomorrowWithOffset(), millisecondsUntilTomorrowWithOffset(),
equalTo(4 * DateUtils.HOUR_LENGTH) equalTo(4 * DateUtils.HOUR_LENGTH)
) )
setStartDayOffset(3, 30) setStartDayOffset(3, 30)
setFixedLocalTime(unixTime(2017, Calendar.JANUARY, 1, 23, 59)) LocalDate.fixedLocalTime = unixTime(2017, Calendar.JANUARY, 1, 23, 59)
assertThat( assertThat(
millisecondsUntilTomorrowWithOffset(), millisecondsUntilTomorrowWithOffset(),
equalTo(3 * DateUtils.HOUR_LENGTH + 31 * DateUtils.MINUTE_LENGTH) equalTo(3 * DateUtils.HOUR_LENGTH + 31 * DateUtils.MINUTE_LENGTH)
) )
setFixedLocalTime(unixTime(2017, Calendar.JANUARY, 2, 1, 0)) LocalDate.fixedLocalTime = unixTime(2017, Calendar.JANUARY, 2, 1, 0)
assertThat( assertThat(
millisecondsUntilTomorrowWithOffset(), millisecondsUntilTomorrowWithOffset(),
equalTo(2 * DateUtils.HOUR_LENGTH + 30 * DateUtils.MINUTE_LENGTH) equalTo(2 * DateUtils.HOUR_LENGTH + 30 * DateUtils.MINUTE_LENGTH)
@ -317,7 +316,7 @@ class DateUtilsTest : BaseUnitTest() {
@Test @Test
fun testGetStartOfTodayCalendar() { fun testGetStartOfTodayCalendar() {
setFixedLocalTime(FIXED_LOCAL_TIME) LocalDate.fixedLocalTime = FIXED_LOCAL_TIME
setFixedLocale(Locale.GERMANY) setFixedLocale(Locale.GERMANY)
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"), Locale.GERMANY)
@ -331,7 +330,7 @@ class DateUtilsTest : BaseUnitTest() {
val hourOffset = 3 val hourOffset = 3
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)
setFixedLocalTime(priorToOffset) LocalDate.fixedLocalTime = priorToOffset
setFixedLocale(Locale.GERMANY) setFixedLocale(Locale.GERMANY)
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"), Locale.GERMANY)
@ -345,7 +344,7 @@ class DateUtilsTest : BaseUnitTest() {
val hourOffset = 3 val hourOffset = 3
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)
setFixedLocalTime(afterOffset) LocalDate.fixedLocalTime = afterOffset
setFixedLocale(Locale.GERMANY) setFixedLocale(Locale.GERMANY)
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"), Locale.GERMANY)
@ -382,7 +381,7 @@ class DateUtilsTest : BaseUnitTest() {
@Test @Test
fun test_applyTimezone() { fun test_applyTimezone() {
setFixedTimeZone(TimeZone.getTimeZone("Australia/Sydney")) LocalDate.fixedTimeZone = kotlinx.datetime.TimeZone.of("Australia/Sydney")
assertEquals( assertEquals(
applyTimezone(unixTime(2017, Calendar.JULY, 30, 18, 0)), applyTimezone(unixTime(2017, Calendar.JULY, 30, 18, 0)),
unixTime(2017, Calendar.JULY, 30, 8, 0) unixTime(2017, Calendar.JULY, 30, 8, 0)
@ -521,7 +520,7 @@ class DateUtilsTest : BaseUnitTest() {
@Test @Test
fun test_removeTimezone() { fun test_removeTimezone() {
setFixedTimeZone(TimeZone.getTimeZone("Australia/Sydney")) LocalDate.fixedTimeZone = kotlinx.datetime.TimeZone.of("Australia/Sydney")
assertEquals( assertEquals(
removeTimezone(unixTime(2017, Calendar.JULY, 30, 8, 0)), removeTimezone(unixTime(2017, Calendar.JULY, 30, 8, 0)),
unixTime(2017, Calendar.JULY, 30, 18, 0) unixTime(2017, Calendar.JULY, 30, 18, 0)

@ -3,6 +3,7 @@ package org.isoron.uhabits.core.utils
import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.isoron.platform.time.LocalDate
import org.isoron.uhabits.core.BaseUnitTest import org.isoron.uhabits.core.BaseUnitTest
import org.junit.Test import org.junit.Test
import java.util.Calendar import java.util.Calendar
@ -21,8 +22,8 @@ class MidnightTimerTest : BaseUnitTest() {
val dispatcher = executor.asCoroutineDispatcher() val dispatcher = executor.asCoroutineDispatcher()
withContext(dispatcher) { withContext(dispatcher) {
DateUtils.setFixedTimeZone(TimeZone.getTimeZone("GMT")) LocalDate.fixedTimeZone = kotlinx.datetime.TimeZone.UTC
DateUtils.setFixedLocalTime( LocalDate.fixedLocalTime =
unixTime( unixTime(
2017, 2017,
Calendar.JANUARY, Calendar.JANUARY,
@ -31,7 +32,6 @@ class MidnightTimerTest : BaseUnitTest() {
59, 59,
DateUtils.MINUTE_LENGTH - 1 DateUtils.MINUTE_LENGTH - 1
) )
)
val suspendedListener = suspendCoroutine<Boolean> { continuation -> val suspendedListener = suspendCoroutine<Boolean> { continuation ->
MidnightTimer().apply { MidnightTimer().apply {

Loading…
Cancel
Save