Skip to content

Commit 1f36b21

Browse files
committed
Add utils to convert between Protobuf Timestamp/Duration to java.time equivalent
* Scaffold :exts:protobuf:temporal module * Add module to settings script
1 parent 55f17ba commit 1f36b21

File tree

5 files changed

+37
-0
lines changed

5 files changed

+37
-0
lines changed

exts/protobuf/temporal/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
plugins {
2+
com.edricchan.studybuddy.library.kotlin
3+
com.edricchan.studybuddy.library.testing.`kotest-junit5`
4+
}
5+
6+
dependencies {
7+
api(libs.protobuf.kotlin.lite)
8+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.edricchan.studybuddy.exts.protobuf.temporal
2+
3+
import com.google.protobuf.duration
4+
import java.time.Duration
5+
// We use DurationP instead of DurationProto as there's also a class with the same name
6+
// in the package
7+
import com.google.protobuf.Duration as DurationP
8+
9+
fun DurationP.toJavaDuration(): Duration = Duration.ofSeconds(
10+
seconds, nanos.toLong()
11+
)
12+
13+
fun Duration.toProtoDuration(): DurationP = duration {
14+
seconds = this@toProtoDuration.seconds
15+
nanos = this@toProtoDuration.nano
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.edricchan.studybuddy.exts.protobuf.temporal
2+
3+
import com.google.protobuf.Timestamp
4+
import com.google.protobuf.timestamp
5+
import java.time.Instant
6+
7+
fun Timestamp.toInstant(): Instant = Instant.ofEpochSecond(seconds, nanos.toLong())
8+
fun Instant.toTimestamp(): Timestamp = timestamp {
9+
seconds = epochSecond
10+
nanos = nano
11+
}

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ include(
151151
":exts:firebase:temporal",
152152
":exts:material",
153153
":exts:material:date-picker",
154+
":exts:protobuf:temporal",
154155
":features:help",
155156
":features:auth",
156157
":features:settings",

0 commit comments

Comments
 (0)