1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package android.tools.common
18
19 import kotlin.js.JsExport
20 import kotlin.js.JsName
21
22 @JsExport
<lambda>null23 class TimestampFactory(private val realTimestampFormatter: (Long) -> String = { it.toString() }) {
<lambda>null24 private val empty by lazy { Timestamp(0L, 0L, 0L, realTimestampFormatter) }
<lambda>null25 private val min by lazy { Timestamp(1, 1, 1, realTimestampFormatter) }
<lambda>null26 private val max by lazy {
27 Timestamp(Long.MAX_VALUE, Long.MAX_VALUE, Long.MAX_VALUE, realTimestampFormatter)
28 }
29
minnull30 fun min(): Timestamp = min
31 fun max(): Timestamp = max
32 fun empty(): Timestamp = empty
33
34 @JsName("fromLong")
35 fun from(
36 elapsedNanos: Long? = null,
37 systemUptimeNanos: Long? = null,
38 unixNanos: Long? = null,
39 ): Timestamp {
40 return Timestamp(
41 elapsedNanos ?: 0L,
42 systemUptimeNanos ?: 0L,
43 unixNanos ?: 0L,
44 realTimestampFormatter
45 )
46 }
47
48 @JsName("fromString")
fromnull49 fun from(
50 elapsedNanos: String? = null,
51 systemUptimeNanos: String? = null,
52 unixNanos: String? = null,
53 ): Timestamp {
54 return from(
55 (elapsedNanos ?: "0").toLong(),
56 (systemUptimeNanos ?: "0").toLong(),
57 (unixNanos ?: "0").toLong()
58 )
59 }
60
61 @JsName("fromWithOffsetLong")
fromnull62 fun from(elapsedNanos: Long, elapsedOffsetNanos: Long): Timestamp {
63 return Timestamp(
64 elapsedNanos = elapsedNanos,
65 unixNanos = elapsedNanos + elapsedOffsetNanos,
66 realTimestampFormatter = realTimestampFormatter
67 )
68 }
69
70 @JsName("fromWithOffsetString")
fromnull71 fun from(elapsedNanos: String, elapsedOffsetNanos: String): Timestamp {
72 val elapsedNanosLong = elapsedNanos.toLong()
73 return from(
74 elapsedNanos = elapsedNanosLong,
75 unixNanos = elapsedNanosLong + elapsedOffsetNanos.toLong()
76 )
77 }
78 }
79