1 /*
2 * Copyright (C) 2024 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
18
<lambda>null19 class TimestampFactory(private val realTimestampFormatter: (Long) -> String = { it.toString() }) {
<lambda>null20 private val empty by lazy { Timestamp(0L, 0L, 0L, realTimestampFormatter) }
<lambda>null21 private val min by lazy { Timestamp(1, 1, 1, realTimestampFormatter) }
<lambda>null22 private val max by lazy {
23 Timestamp(Long.MAX_VALUE, Long.MAX_VALUE, Long.MAX_VALUE, realTimestampFormatter)
24 }
25
minnull26 fun min(): Timestamp = min
27
28 fun max(): Timestamp = max
29
30 fun empty(): Timestamp = empty
31
32 fun from(
33 elapsedNanos: Long? = null,
34 systemUptimeNanos: Long? = null,
35 unixNanos: Long? = null,
36 ): Timestamp {
37 return Timestamp(
38 elapsedNanos ?: 0L,
39 systemUptimeNanos ?: 0L,
40 unixNanos ?: 0L,
41 realTimestampFormatter,
42 )
43 }
44
fromnull45 fun from(
46 elapsedNanos: String? = null,
47 systemUptimeNanos: String? = null,
48 unixNanos: String? = null,
49 ): Timestamp {
50 return from(
51 (elapsedNanos ?: "0").toLong(),
52 (systemUptimeNanos ?: "0").toLong(),
53 (unixNanos ?: "0").toLong(),
54 )
55 }
56
fromnull57 fun from(elapsedNanos: Long, elapsedOffsetNanos: Long): Timestamp {
58 return Timestamp(
59 elapsedNanos = elapsedNanos,
60 unixNanos = elapsedNanos + elapsedOffsetNanos,
61 realTimestampFormatter = realTimestampFormatter,
62 )
63 }
64
fromnull65 fun from(elapsedNanos: String, elapsedOffsetNanos: String): Timestamp {
66 val elapsedNanosLong = elapsedNanos.toLong()
67 return from(
68 elapsedNanos = elapsedNanosLong,
69 unixNanos = elapsedNanosLong + elapsedOffsetNanos.toLong(),
70 )
71 }
72 }
73