Skip to content

Commit cbcf5e1

Browse files
committed
test:add tests for TimeUtils.kt
1 parent 1bffa80 commit cbcf5e1

2 files changed

Lines changed: 111 additions & 5 deletions

File tree

common/src/main/java/com/ichi2/anki/common/time/TimeUtils.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package com.ichi2.anki.common.time
1818

19-
import com.ichi2.anki.common.annotations.NeedsTest
2019
import java.text.SimpleDateFormat
2120
import java.util.Calendar
2221
import java.util.Locale
@@ -33,11 +32,9 @@ const val TIME_DAY = 24.0 * TIME_HOUR
3332
// private const val TIME_MONTH = 30.0 * TIME_DAY
3433
// private const val TIME_YEAR = 12.0 * TIME_MONTH
3534

36-
@NeedsTest("untested")
3735
fun getTimestamp(time: Time): String = SimpleDateFormat("yyyyMMddHHmmss", Locale.US).format(time.currentDate)
3836

3937
/** Formats the time as '00:00.00' (m:s:ms), OR 00:00:00.00 (h:m:s.ms) */
40-
@NeedsTest("untested")
4138
fun Duration.formatAsString(): String {
4239
val milliseconds = this.inWholeMilliseconds
4340
val ms = milliseconds % 1000
@@ -51,11 +48,10 @@ fun Duration.formatAsString(): String {
5148
}
5249
}
5350

54-
@NeedsTest("untested")
5551
fun getDayStart(time: Time): Long {
5652
val cal = time.calendar()
5753
if (cal[Calendar.HOUR_OF_DAY] < 4) {
58-
cal.roll(Calendar.DAY_OF_YEAR, -1)
54+
cal.add(Calendar.DAY_OF_YEAR, -1)
5955
}
6056
cal[Calendar.HOUR_OF_DAY] = 4
6157
cal[Calendar.MINUTE] = 0
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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

Comments
 (0)