0
# Time Types
1
2
Cross-platform time and duration types that map to Google Well-Known Types (google.protobuf.Duration and google.protobuf.Timestamp). These provide consistent time handling across all Kotlin multiplatform targets.
3
4
## Capabilities
5
6
### Duration
7
8
Represents a time duration with nanosecond precision, mapping to google.protobuf.Duration.
9
10
```kotlin { .api }
11
/**
12
* A measurement of time. Durations may be positive, zero, or negative.
13
* Maps to google.protobuf.Duration
14
*/
15
expect class Duration {
16
/** Get seconds component */
17
fun getSeconds(): Long
18
19
/** Get nanoseconds component (0-999,999,999) */
20
fun getNano(): Int
21
}
22
23
/** Create duration from seconds and nanoseconds */
24
expect fun durationOfSeconds(seconds: Long, nano: Long): Duration
25
```
26
27
### Instant
28
29
Represents a timestamp with nanosecond precision, mapping to google.protobuf.Timestamp.
30
31
```kotlin { .api }
32
/**
33
* Represents a timestamp (google.protobuf.Timestamp)
34
*/
35
expect class Instant {
36
/** Seconds since Unix epoch (1970-01-01T00:00:00Z) */
37
fun getEpochSecond(): Long
38
39
/** Nanoseconds component (0-999,999,999) */
40
fun getNano(): Int
41
}
42
43
/** Create instant from epoch seconds and nanoseconds */
44
expect fun ofEpochSecond(epochSecond: Long, nano: Long): Instant
45
```
46
47
**Usage Examples:**
48
49
```kotlin
50
import com.squareup.wire.*
51
52
// Create duration (5.5 seconds)
53
val duration = durationOfSeconds(5, 500_000_000)
54
println("Seconds: ${duration.getSeconds()}") // 5
55
println("Nanos: ${duration.getNano()}") // 500000000
56
57
// Create instant (Unix timestamp)
58
val instant = ofEpochSecond(1640995200, 123_456_789) // 2022-01-01T00:00:00.123456789Z
59
println("Epoch seconds: ${instant.getEpochSecond()}") // 1640995200
60
println("Nanos: ${instant.getNano()}") // 123456789
61
62
// Use with ProtoAdapter
63
val encodedDuration = ProtoAdapter.DURATION.encode(duration)
64
val decodedDuration = ProtoAdapter.DURATION.decode(encodedDuration)
65
66
val encodedInstant = ProtoAdapter.INSTANT.encode(instant)
67
val decodedInstant = ProtoAdapter.INSTANT.decode(encodedInstant)
68
```