Skip to content

Commit 2a54fe7

Browse files
committed
Add DateTimeUtil#parseTimestamp()
1 parent b2fbaf8 commit 2a54fe7

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

baseLib/src/main/java/me/ycdev/android/lib/common/utils/DateTimeUtils.kt

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,39 @@
11
package me.ycdev.android.lib.common.utils
22

3+
import androidx.annotation.VisibleForTesting
34
import java.text.ParseException
45
import java.text.SimpleDateFormat
56
import java.util.Date
67
import java.util.Locale
8+
import java.util.TimeZone
79

810
@Suppress("unused")
911
object DateTimeUtils {
12+
private val timeFormatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS", Locale.US)
13+
private var timeZone: TimeZone? = null
14+
15+
private fun updateTimeZoneIfNeeded() {
16+
if (timeZone == null && TimeZone.getDefault() != timeFormatter.timeZone) {
17+
// system timezone changed!
18+
timeFormatter.timeZone = TimeZone.getDefault()
19+
}
20+
}
21+
22+
@VisibleForTesting
23+
fun setTimeZoneForTestCases(zone: TimeZone) {
24+
timeZone = zone
25+
timeFormatter.timeZone = zone
26+
}
27+
28+
/**
29+
* @param timeStr Time string in the format "yyyy-MM-dd HH:mm:ss:SSS"
30+
*/
31+
@Throws(ParseException::class)
32+
fun parseTimestamp(timeStr: String): Long {
33+
updateTimeZoneIfNeeded()
34+
return timeFormatter.parse(timeStr)?.time ?: 0
35+
}
36+
1037
/**
1138
* Generate file name from system time in the format "yyyyMMdd-HHmmss-SSS",
1239
* @param sysTime System time in milliseconds
@@ -30,7 +57,8 @@ object DateTimeUtils {
3057
* @param timeStamp System time in milliseconds
3158
*/
3259
fun getReadableTimeStamp(timeStamp: Long): String {
33-
return SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS", Locale.US).format(Date(timeStamp))
60+
updateTimeZoneIfNeeded()
61+
return timeFormatter.format(Date(timeStamp))
3462
}
3563

3664
/**
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package me.ycdev.android.lib.common.utils
2+
3+
import java.lang.reflect.GenericArrayType
4+
import java.lang.reflect.ParameterizedType
5+
import java.lang.reflect.Type
6+
import java.lang.reflect.TypeVariable
7+
import java.lang.reflect.WildcardType
8+
9+
object TypeUtils {
10+
fun getRawType(type: Type): Class<*> {
11+
if (type is Class<*>) {
12+
// Type is a normal class.
13+
return type
14+
}
15+
if (type is ParameterizedType) {
16+
val parameterizedType: ParameterizedType = type
17+
18+
// I'm not exactly sure why getRawType() returns Type instead of Class. Neal isn't either but
19+
// suspects some pathological case related to nested classes exists.
20+
val rawType: Type = parameterizedType.rawType
21+
require(rawType is Class<*>)
22+
return rawType
23+
}
24+
if (type is GenericArrayType) {
25+
val componentType: Type = type.genericComponentType
26+
return java.lang.reflect.Array.newInstance(getRawType(componentType), 0).javaClass
27+
}
28+
if (type is TypeVariable<*>) {
29+
// We could use the variable's bounds, but that won't work if there are multiple. Having a raw
30+
// type that's more general than necessary is okay.
31+
return Any::class.java
32+
}
33+
if (type is WildcardType) {
34+
return getRawType(type.upperBounds[0])
35+
}
36+
throw IllegalArgumentException(
37+
"Expected a Class, ParameterizedType, or " +
38+
"GenericArrayType, but <" +
39+
type +
40+
"> is of type " +
41+
type.javaClass.name
42+
)
43+
}
44+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package me.ycdev.android.lib.common.utils
2+
3+
import com.google.common.truth.Truth.assertThat
4+
import org.junit.Test
5+
6+
class TypeUtilsTest {
7+
@Test
8+
fun getRawType() {
9+
assertThat(TypeUtils.getRawType(TypeUtils::class.java)).isEqualTo(TypeUtils::class.java)
10+
assertThat(TypeUtils.getRawType(dummyArrayList().javaClass)).isEqualTo(ArrayList::class.java)
11+
assertThat(TypeUtils.getRawType(Array<String>::class.java)).isEqualTo(Array<String>::class.java)
12+
}
13+
14+
private fun dummyArrayList(): ArrayList<String> = arrayListOf()
15+
16+
private fun dummyArray(): Array<String> = arrayOf()
17+
}

0 commit comments

Comments
 (0)