|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 Dhanush Sugganahalli <dhanush41230@gmail.com> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify it under |
| 5 | + * the terms of the GNU General Public License as published by the Free Software |
| 6 | + * Foundation; either version 3 of the License, or (at your option) any later |
| 7 | + * version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY |
| 10 | + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 11 | + * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU General Public License along with |
| 14 | + * this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + */ |
| 16 | +package com.ichi2.anki.common.time |
| 17 | + |
| 18 | +import android.icu.util.Calendar |
| 19 | +import org.junit.Assert.assertEquals |
| 20 | +import org.junit.Test |
| 21 | +import java.util.TimeZone |
| 22 | +import kotlin.time.Duration.Companion.hours |
| 23 | +import kotlin.time.Duration.Companion.milliseconds |
| 24 | +import kotlin.time.Duration.Companion.minutes |
| 25 | +import kotlin.time.Duration.Companion.seconds |
| 26 | + |
| 27 | +class TimeUtilsTest { |
| 28 | + @Test |
| 29 | + fun getTimeStamp_returnsTimeStampAsUTC() { |
| 30 | + TimeZone.setDefault(TimeZone.getTimeZone("UTC")) |
| 31 | + try { |
| 32 | + val time = MockTime(2020, Calendar.AUGUST, 7, 7, 0, 0, 0, 0) |
| 33 | + assertEquals("20200807070000", getTimestamp(time)) |
| 34 | + } finally { |
| 35 | + TimeZone.setDefault(null) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + fun formatAsString_zeroDuration_returnsZeroString() { |
| 41 | + val duration = 0.milliseconds |
| 42 | + assertEquals("00:00.00", duration.formatAsString()) |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + fun formatAsString_oneMinuteThirtySeconds_returnsOneMinuteThirtySecondsString() { |
| 47 | + val duration = 1.minutes + 30.seconds + 500.milliseconds |
| 48 | + assertEquals("01:30.50", duration.formatAsString()) |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun formatAsString_oneHourDuration_returnsOneHourString() { |
| 53 | + val duration = 1.hours |
| 54 | + assertEquals("01:00:00.00", duration.formatAsString()) |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + fun formatAsString_overOneHour_returnsHourMinuteSecondString() { |
| 59 | + val duration = 1.hours + 3.minutes + 4.seconds + 500.milliseconds |
| 60 | + assertEquals("01:03:04.50", duration.formatAsString()) |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + fun getDayStart_BeforeFourAM_returnsDayStart() { |
| 65 | + TimeZone.setDefault(TimeZone.getTimeZone("UTC")) |
| 66 | + try { |
| 67 | + val input = MockTime(2020, Calendar.AUGUST, 7, 3, 0, 0, 0, 0) |
| 68 | + val expected = MockTime(2020, Calendar.AUGUST, 6, 4, 0, 0, 0, 0).calendar().timeInMillis |
| 69 | + assertEquals(expected, getDayStart(input)) |
| 70 | + } finally { |
| 71 | + TimeZone.setDefault(null) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + fun getDayStart_AfterFourAM_returnsDayStart() { |
| 77 | + TimeZone.setDefault(TimeZone.getTimeZone("UTC")) |
| 78 | + try { |
| 79 | + val input = MockTime(2020, Calendar.AUGUST, 7, 5, 0, 0, 0, 0) |
| 80 | + val expected = MockTime(2020, Calendar.AUGUST, 7, 4, 0, 0, 0, 0).calendar().timeInMillis |
| 81 | + assertEquals(expected, getDayStart(input)) |
| 82 | + } finally { |
| 83 | + TimeZone.setDefault(null) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + fun getDayStart_AtFourAM_returnsDayStart() { |
| 89 | + TimeZone.setDefault(TimeZone.getTimeZone("UTC")) |
| 90 | + try { |
| 91 | + val input = MockTime(2020, Calendar.AUGUST, 7, 4, 0, 0, 0, 0) |
| 92 | + val expected = MockTime(2020, Calendar.AUGUST, 7, 4, 0, 0, 0, 0).calendar().timeInMillis |
| 93 | + assertEquals(expected, getDayStart(input)) |
| 94 | + } finally { |
| 95 | + TimeZone.setDefault(null) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + fun getDayStart_AtJanuary1_returnsDayStart() { |
| 101 | + TimeZone.setDefault(TimeZone.getTimeZone("UTC")) |
| 102 | + try { |
| 103 | + val input = MockTime(2021, Calendar.JANUARY, 1, 3, 0, 0, 0, 0) |
| 104 | + val expected = MockTime(2020, Calendar.DECEMBER, 31, 4, 0, 0, 0, 0).calendar().timeInMillis |
| 105 | + assertEquals(expected, getDayStart(input)) |
| 106 | + } finally { |
| 107 | + TimeZone.setDefault(null) |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments