Skip to content

Commit d3bdb91

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

2 files changed

Lines changed: 110 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: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 org.junit.Assert.assertEquals
19+
import org.junit.Test
20+
import java.util.TimeZone
21+
import kotlin.time.Duration.Companion.hours
22+
import kotlin.time.Duration.Companion.milliseconds
23+
import kotlin.time.Duration.Companion.minutes
24+
import kotlin.time.Duration.Companion.seconds
25+
26+
class TimeUtilsTest {
27+
@Test
28+
fun getTimeStamp_returnsTimeStampAsUTC() {
29+
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
30+
try {
31+
val time = MockTime(2020, 7, 7, 7, 0, 0, 0, 0)
32+
assertEquals("20200807070000", getTimestamp(time))
33+
} finally {
34+
TimeZone.setDefault(null)
35+
}
36+
}
37+
38+
@Test
39+
fun formatAsString_zeroDuration_returnsZeroString() {
40+
val duration = 0.milliseconds
41+
assertEquals("00:00.00", duration.formatAsString())
42+
}
43+
44+
@Test
45+
fun formatAsString_oneMinuteThirtySeconds_returnsOneMinuteThirtySecondsString() {
46+
val duration = 1.minutes + 30.seconds + 500.milliseconds
47+
assertEquals("01:30.50", duration.formatAsString())
48+
}
49+
50+
@Test
51+
fun formatAsString_oneHourDuration_returnsOneHourString() {
52+
val duration = 1.hours
53+
assertEquals("01:00:00.00", duration.formatAsString())
54+
}
55+
56+
@Test
57+
fun formatAsString_overOneHour_returnsHourMinuteSecondString() {
58+
val duration = 1.hours + 3.minutes + 4.seconds + 500.milliseconds
59+
assertEquals("01:03:04.50", duration.formatAsString())
60+
}
61+
62+
@Test
63+
fun getDayStart_BeforeFourAM_returnsDayStart() {
64+
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
65+
try {
66+
val input = MockTime(2020, 7, 7, 3, 0, 0, 0, 0)
67+
val expected = MockTime(2020, 7, 6, 4, 0, 0, 0, 0).calendar().timeInMillis
68+
assertEquals(expected, getDayStart(input))
69+
} finally {
70+
TimeZone.setDefault(null)
71+
}
72+
}
73+
74+
@Test
75+
fun getDayStart_AfterFourAM_returnsDayStart() {
76+
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
77+
try {
78+
val input = MockTime(2020, 7, 7, 5, 0, 0, 0, 0)
79+
val expected = MockTime(2020, 7, 7, 4, 0, 0, 0, 0).calendar().timeInMillis
80+
assertEquals(expected, getDayStart(input))
81+
} finally {
82+
TimeZone.setDefault(null)
83+
}
84+
}
85+
86+
@Test
87+
fun getDayStart_AtFourAM_returnsDayStart() {
88+
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
89+
try {
90+
val input = MockTime(2020, 7, 7, 4, 0, 0, 0, 0)
91+
val expected = MockTime(2020, 7, 7, 4, 0, 0, 0, 0).calendar().timeInMillis
92+
assertEquals(expected, getDayStart(input))
93+
} finally {
94+
TimeZone.setDefault(null)
95+
}
96+
}
97+
98+
@Test
99+
fun getDayStart_AtJanuary1_returnsDayStart() {
100+
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
101+
try {
102+
val input = MockTime(2021, 0, 1, 3, 0, 0, 0, 0)
103+
val expected = MockTime(2020, 11, 31, 4, 0, 0, 0, 0).calendar().timeInMillis
104+
assertEquals(expected, getDayStart(input))
105+
} finally {
106+
TimeZone.setDefault(null)
107+
}
108+
}
109+
}

0 commit comments

Comments
 (0)