Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import java.util.*
* dateTimeNowString(): 2023-04-19T04:03:46.880Z
*/
fun dateTimeNowString(): String {
val sdf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.'Szzz")
// Note, we should specify locale = Locale.ROOT, otherwise the timestamp returned will use
// the default locale, which may not be what we want.
val sdf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.'Szzz", Locale.ROOT)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Android docs recommend using Locale.US, but since this might be used in JVM projects I opted to go with Locale.ROOT

The default locale is not appropriate for machine-readable output. The best choice there is usually Locale.US – this locale is guaranteed to be available on all devices, and the fact that it has no surprising special cases and is frequently used (especially for computer-computer communication) means that it tends to be the most efficient choice too.

val utc = TimeZone.getTimeZone("UTC");
sdf.timeZone = utc;
return sdf.format(Date()).replace("UTC", "Z")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.segment.analytics.kotlin.core.utilities

import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Test
import java.time.format.DateTimeFormatter.ISO_DATE_TIME

class DateTimeUtilsTest {

@Test
fun `dateTimeNowString() produces a string in the correct ISO8601 format`() {
val dateTimeNowString = dateTimeNowString()
val date = ISO_DATE_TIME.parse(dateTimeNowString)
assertNotNull(date)
}
}