@@ -3,30 +3,31 @@ package com.segment.analytics.kotlin.core.utilities
33import java.text.SimpleDateFormat
44import java.util.*
55
6- object SegmentInstant {
7-
8- // Note, we should specify locale = Locale.ROOT, otherwise the timestamp returned will use
9- // the default locale, which may not be what we want.
10- private val formatter: SimpleDateFormat =
11- SimpleDateFormat (" yyyy-MM-dd'T'HH:mm:ss'.'SSSzzz" , Locale .ROOT ).apply {
12- timeZone = TimeZone .getTimeZone(" UTC" )
6+ class SegmentInstant {
7+ companion object {
8+ private val formatters = object : ThreadLocal <SimpleDateFormat >() {
9+ override fun initialValue (): SimpleDateFormat {
10+ return SimpleDateFormat (" yyyy-MM-dd'T'HH:mm:ss'.'SSSzzz" , Locale .ROOT ).apply {
11+ timeZone = TimeZone .getTimeZone(" UTC" )
12+ }
13+ }
1314 }
1415
15- /* *
16- * This function is a replacement for Instant.now().toString(). It produces strings in a
17- * compatible format.
18- *
19- * Ex:
20- * Instant.now(): 2023-04-19T04:03:46.880969Z
21- * dateTimeNowString(): 2023-04-19T04:03:46.880Z
22- */
23- fun now (): String {
24- return from(Date ())
25- }
16+ /* *
17+ * This function is a replacement for Instant.now().toString(). It produces strings in a
18+ * compatible format.
19+ *
20+ * Ex:
21+ * Instant.now(): 2023-04-19T04:03:46.880969Z
22+ * dateTimeNowString(): 2023-04-19T04:03:46.880Z
23+ */
24+ fun now (): String {
25+ return from(Date ())
26+ }
2627
27- internal fun from (date : Date ): String {
28- // internal use only. for testing purpose.
29- return formatter.format(date).replace( " UTC " , " Z " )
28+ internal fun from (date : Date ): String {
29+ return formatters.get().format(date).replace( " UTC " , " Z " )
30+ }
3031 }
3132}
3233
0 commit comments