Skip to content

Commit d124a36

Browse files
Locale Timestamp (#170)
* Specify Locale.ROOT when calling dateTimeNowString() * Add test * Add comment * Update test
1 parent a414dd9 commit d124a36

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

core/src/main/java/com/segment/analytics/kotlin/core/utilities/DateTimeUtils.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import java.util.*
1212
* dateTimeNowString(): 2023-04-19T04:03:46.880Z
1313
*/
1414
fun dateTimeNowString(): String {
15-
val sdf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.'Szzz")
15+
// Note, we should specify locale = Locale.ROOT, otherwise the timestamp returned will use
16+
// the default locale, which may not be what we want.
17+
val sdf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.'Szzz", Locale.ROOT)
1618
val utc = TimeZone.getTimeZone("UTC");
1719
sdf.timeZone = utc;
1820
return sdf.format(Date()).replace("UTC", "Z")
19-
}
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.segment.analytics.kotlin.core.utilities
2+
3+
import org.junit.jupiter.api.Assertions.assertNotNull
4+
import org.junit.jupiter.api.Test
5+
import java.time.format.DateTimeFormatter.ISO_DATE_TIME
6+
7+
class DateTimeUtilsTest {
8+
9+
@Test
10+
fun `dateTimeNowString() produces a string in the correct ISO8601 format`() {
11+
val dateTimeNowString = dateTimeNowString()
12+
val date = ISO_DATE_TIME.parse(dateTimeNowString)
13+
assertNotNull(date)
14+
}
15+
}

0 commit comments

Comments
 (0)