@@ -19,8 +19,83 @@ import android.icu.util.Calendar
1919import org.junit.Assert.assertEquals
2020import org.junit.Test
2121import 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
2226
2327class TimeUtilsTest {
28+ @Test
29+ fun getTimeStamp_returnsTimeStampAsUTC () {
30+ TimeZone .setDefault(TimeZone .getTimeZone(" UTC" ))
31+ try {
32+ val time = MockTime (2020 , 7 , 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 , 7 , 7 , 3 , 0 , 0 , 0 , 0 )
68+ val expected = MockTime (2020 , 7 , 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 , 7 , 7 , 5 , 0 , 0 , 0 , 0 )
80+ val expected = MockTime (2020 , 7 , 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 , 7 , 7 , 4 , 0 , 0 , 0 , 0 )
92+ val expected = MockTime (2020 , 7 , 7 , 4 , 0 , 0 , 0 , 0 ).calendar().timeInMillis
93+ assertEquals(expected, getDayStart(input))
94+ } finally {
95+ TimeZone .setDefault(null )
96+ }
97+ }
98+
2499 @Test
25100 fun getDayStart_AtJanuary1_returnsDayStart () {
26101 TimeZone .setDefault(TimeZone .getTimeZone(" UTC" ))
0 commit comments