Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance time consistency between GitHub, Pingh server, and client #45

Merged
merged 16 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import com.google.protobuf.Timestamp
import com.google.protobuf.util.Timestamps
import java.time.Duration
import java.time.LocalDateTime
import java.time.ZoneOffset
import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter

/**
Expand Down Expand Up @@ -64,15 +64,22 @@ internal fun Timestamp.add(duration: com.google.protobuf.Duration): Timestamp =
* - If the difference is exactly one day, returns the string `"yesterday"`;
* - In all other cases, returns the day and month.
*
* Note that this timestamp must be in UTC. A default time zone offset
* is applied to the UTC time.
*
* @receiver The timestamp represents a time in the past in UTC.
* @throws IllegalArgumentException if the `Timestamp` is not from the past.
*/
public fun Timestamp.howMuchTimeHasPassed(): String {
val thisDatetime = LocalDateTime.ofEpochSecond(this.seconds, this.nanos, ZoneOffset.UTC)
val offset = OffsetDateTime.now().offset
val thisDatetime = LocalDateTime.ofEpochSecond(this.seconds, this.nanos, offset)
val difference = Duration.between(
thisDatetime,
LocalDateTime.now(ZoneOffset.UTC)
LocalDateTime.now(offset)
)
require(difference > Duration.ZERO) { "" }
require(difference > Duration.ZERO) {
"This `Timestamp` must indicate a time in the past."
MykytaPimonovTD marked this conversation as resolved.
Show resolved Hide resolved
}
return when {
difference.toMinutes() < 1L -> "just now"
difference.toMinutes() == 1L -> "a minute ago"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ package io.spine.internal.dependency

// https://github.com/spine-examples/Pingh
public object Pingh {
private const val version = "1.0.0-SNAPSHOT.5"
private const val version = "1.0.0-SNAPSHOT.6"
public const val client: String = "io.spine.examples.pingh:client:$version"
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ package io.spine.examples.pingh.desktop

import com.google.protobuf.Timestamp
import java.time.LocalDateTime
import java.time.ZoneOffset
import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter

/**
Expand All @@ -40,9 +40,14 @@ private val datetimeFormat = DateTimeFormatter.ofPattern("dd MMM HH:mm")
/**
* Converts `Timestamp` to the `dd MMM HH:mm` string.
*
* Note that this timestamp must be in UTC. A default time zone offset
* is applied to the UTC time.
*
* Examples:
* - `10 May 14:37`
* - `05 Sep 04:00`
*
* @receiver The timestamp in UTC.
*/
internal fun Timestamp.toDatetime(): String =
datetimeFormat.format(toLocalDateTime())
Expand All @@ -51,4 +56,4 @@ internal fun Timestamp.toDatetime(): String =
* Converts this `Timestamp` to `LocalDateTime`.
*/
private fun Timestamp.toLocalDateTime(): LocalDateTime =
LocalDateTime.ofEpochSecond(this.seconds, this.nanos, ZoneOffset.UTC)
LocalDateTime.ofEpochSecond(this.seconds, this.nanos, OffsetDateTime.now().offset)
2 changes: 1 addition & 1 deletion version.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
/**
* The version of the `Pingh` to publish.
*/
val pinghVersion: String by extra("1.0.0-SNAPSHOT.5")
val pinghVersion: String by extra("1.0.0-SNAPSHOT.6")
Loading