From c3ade297b3d472bdfc0ace11e150f298b1e1b8ac Mon Sep 17 00:00:00 2001 From: warrior Date: Fri, 3 Sep 2021 10:59:19 +0200 Subject: [PATCH 1/6] Add discord messaging timestamps Co-Authored-By: NyCode --- common/src/main/kotlin/DiscordTimestamp.kt | 83 ++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 common/src/main/kotlin/DiscordTimestamp.kt diff --git a/common/src/main/kotlin/DiscordTimestamp.kt b/common/src/main/kotlin/DiscordTimestamp.kt new file mode 100644 index 000000000000..e3c71714da5b --- /dev/null +++ b/common/src/main/kotlin/DiscordTimestamp.kt @@ -0,0 +1,83 @@ +package dev.kord.common + +import kotlinx.datetime.LocalDateTime +import kotlinx.datetime.TimeZone +import kotlinx.datetime.toInstant +import kotlinx.serialization.Serializable + +/** + * The [timestamp format](https://discord.com/developers/docs/reference#message-formatting-formats) of the message formats + */ +@Serializable +data class DiscordTimestamp( + + /** + * The Unix timestamp to be displayed + */ + val time: Long, + + /** + * The style of the timestamp + */ + val style: DiscordTimestampStyle = DiscordTimestampStyle.ShortDateTime +) : Comparable { + + /** + * Assemble the timestamp to the format supported by discord + */ + override fun toString(): String = "" + + override fun equals(other: Any?): Boolean = other is DiscordTimestamp && other.time == time + operator fun plus(value: Long): DiscordTimestamp = DiscordTimestamp(time + value, style) + operator fun minus(value: Long): DiscordTimestamp = DiscordTimestamp(time - value, style) + + override fun compareTo(other: DiscordTimestamp): Int = when { + time > other.time -> 1 + time < other.time -> -1 + else -> 0 + } +} + +fun LocalDateTime.toDiscordTimestamp(style: DiscordTimestampStyle? = null) = + DiscordTimestamp(toInstant(TimeZone.UTC).toEpochMilliseconds(), style ?: DiscordTimestampStyle.ShortDateTime) + +/** + * The class representing the [style of a timestamp](https://discord.com/developers/docs/reference#message-formatting-timestamp-styles) + */ +enum class DiscordTimestampStyle(val style: String) { + + /** + * For example 16:20 + */ + ShortTime("t"), + + /** + * For example 16:20:30 + */ + LongTime("T"), + + /** + * For example 20/04/2021 + */ + ShortDate("d"), + + /** + * For example 20 April 2021 + */ + LongDate("D"), + + /** + * For example 20 April 2021 16:20 + */ + ShortDateTime("f"), + + /** + * For example Tuesday, 20 April 2021 16:20 + */ + LongDateTime("F"), + + /** + * For example 2 months ago + */ + RelativeTime("R"), +} From 6fa242b3db198103f62db66017238b50c38c36f2 Mon Sep 17 00:00:00 2001 From: NyCode Date: Fri, 3 Sep 2021 11:10:54 +0200 Subject: [PATCH 2/6] Add toDiscordTimestamp function for Instant --- common/src/main/kotlin/DiscordTimestamp.kt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/common/src/main/kotlin/DiscordTimestamp.kt b/common/src/main/kotlin/DiscordTimestamp.kt index e3c71714da5b..09fb5eef7b59 100644 --- a/common/src/main/kotlin/DiscordTimestamp.kt +++ b/common/src/main/kotlin/DiscordTimestamp.kt @@ -1,5 +1,6 @@ package dev.kord.common +import kotlinx.datetime.Instant import kotlinx.datetime.LocalDateTime import kotlinx.datetime.TimeZone import kotlinx.datetime.toInstant @@ -41,6 +42,9 @@ data class DiscordTimestamp( fun LocalDateTime.toDiscordTimestamp(style: DiscordTimestampStyle? = null) = DiscordTimestamp(toInstant(TimeZone.UTC).toEpochMilliseconds(), style ?: DiscordTimestampStyle.ShortDateTime) +fun Instant.toDiscordTimestamp(style: DiscordTimestampStyle? = null) = + DiscordTimestamp(toEpochMilliseconds(), style ?: DiscordTimestampStyle.ShortDateTime) + /** * The class representing the [style of a timestamp](https://discord.com/developers/docs/reference#message-formatting-timestamp-styles) */ From 92a92aa368cfeb91b8c85bca6c05cb60c4bf657d Mon Sep 17 00:00:00 2001 From: NyCode Date: Fri, 3 Sep 2021 11:12:27 +0200 Subject: [PATCH 3/6] Use value getter instead of a function and add toString Co-Authored-By: warrior --- common/src/main/kotlin/DiscordTimestamp.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/common/src/main/kotlin/DiscordTimestamp.kt b/common/src/main/kotlin/DiscordTimestamp.kt index 09fb5eef7b59..a36966455cb3 100644 --- a/common/src/main/kotlin/DiscordTimestamp.kt +++ b/common/src/main/kotlin/DiscordTimestamp.kt @@ -23,10 +23,8 @@ data class DiscordTimestamp( val style: DiscordTimestampStyle = DiscordTimestampStyle.ShortDateTime ) : Comparable { - /** - * Assemble the timestamp to the format supported by discord - */ - override fun toString(): String = "" + val value: String + get() = "" override fun equals(other: Any?): Boolean = other is DiscordTimestamp && other.time == time operator fun plus(value: Long): DiscordTimestamp = DiscordTimestamp(time + value, style) @@ -37,6 +35,8 @@ data class DiscordTimestamp( time < other.time -> -1 else -> 0 } + + override fun toString() = value } fun LocalDateTime.toDiscordTimestamp(style: DiscordTimestampStyle? = null) = From fa9244a5ddf2eacb820cdc51d7d6c6fdf1008d49 Mon Sep 17 00:00:00 2001 From: warrior Date: Fri, 3 Sep 2021 12:15:31 +0200 Subject: [PATCH 4/6] Resolve requested changes Co-Authored-By: Lukellmann <47486203+Lukellmann@users.noreply.github.com> --- common/src/main/kotlin/DiscordTimestamp.kt | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/common/src/main/kotlin/DiscordTimestamp.kt b/common/src/main/kotlin/DiscordTimestamp.kt index a36966455cb3..62b4905e1e93 100644 --- a/common/src/main/kotlin/DiscordTimestamp.kt +++ b/common/src/main/kotlin/DiscordTimestamp.kt @@ -30,20 +30,16 @@ data class DiscordTimestamp( operator fun plus(value: Long): DiscordTimestamp = DiscordTimestamp(time + value, style) operator fun minus(value: Long): DiscordTimestamp = DiscordTimestamp(time - value, style) - override fun compareTo(other: DiscordTimestamp): Int = when { - time > other.time -> 1 - time < other.time -> -1 - else -> 0 - } + override fun compareTo(other: DiscordTimestamp): Int = time.compareTo(other.time) override fun toString() = value } -fun LocalDateTime.toDiscordTimestamp(style: DiscordTimestampStyle? = null) = - DiscordTimestamp(toInstant(TimeZone.UTC).toEpochMilliseconds(), style ?: DiscordTimestampStyle.ShortDateTime) +fun LocalDateTime.toDiscordTimestamp(style: DiscordTimestampStyle = DiscordTimestampStyle.ShortDateTime) = + toInstant(TimeZone.UTC).toDiscordTimestamp(style) -fun Instant.toDiscordTimestamp(style: DiscordTimestampStyle? = null) = - DiscordTimestamp(toEpochMilliseconds(), style ?: DiscordTimestampStyle.ShortDateTime) +fun Instant.toDiscordTimestamp(style: DiscordTimestampStyle = DiscordTimestampStyle.ShortDateTime) = + DiscordTimestamp(toEpochMilliseconds(), style) /** * The class representing the [style of a timestamp](https://discord.com/developers/docs/reference#message-formatting-timestamp-styles) From 1668eaf03e2fd953c042facd58d7df7479122ead Mon Sep 17 00:00:00 2001 From: warrior Date: Fri, 3 Sep 2021 14:04:32 +0200 Subject: [PATCH 5/6] Remove data class --- common/src/main/kotlin/DiscordTimestamp.kt | 40 ++-------------------- 1 file changed, 2 insertions(+), 38 deletions(-) diff --git a/common/src/main/kotlin/DiscordTimestamp.kt b/common/src/main/kotlin/DiscordTimestamp.kt index 62b4905e1e93..2b7eaa041667 100644 --- a/common/src/main/kotlin/DiscordTimestamp.kt +++ b/common/src/main/kotlin/DiscordTimestamp.kt @@ -1,45 +1,9 @@ package dev.kord.common import kotlinx.datetime.Instant -import kotlinx.datetime.LocalDateTime -import kotlinx.datetime.TimeZone -import kotlinx.datetime.toInstant -import kotlinx.serialization.Serializable -/** - * The [timestamp format](https://discord.com/developers/docs/reference#message-formatting-formats) of the message formats - */ -@Serializable -data class DiscordTimestamp( - - /** - * The Unix timestamp to be displayed - */ - val time: Long, - - /** - * The style of the timestamp - */ - val style: DiscordTimestampStyle = DiscordTimestampStyle.ShortDateTime -) : Comparable { - - val value: String - get() = "" - - override fun equals(other: Any?): Boolean = other is DiscordTimestamp && other.time == time - operator fun plus(value: Long): DiscordTimestamp = DiscordTimestamp(time + value, style) - operator fun minus(value: Long): DiscordTimestamp = DiscordTimestamp(time - value, style) - - override fun compareTo(other: DiscordTimestamp): Int = time.compareTo(other.time) - - override fun toString() = value -} - -fun LocalDateTime.toDiscordTimestamp(style: DiscordTimestampStyle = DiscordTimestampStyle.ShortDateTime) = - toInstant(TimeZone.UTC).toDiscordTimestamp(style) - -fun Instant.toDiscordTimestamp(style: DiscordTimestampStyle = DiscordTimestampStyle.ShortDateTime) = - DiscordTimestamp(toEpochMilliseconds(), style) +fun Instant.toMessageFormat(style: DiscordTimestampStyle = DiscordTimestampStyle.ShortDateTime) = + "" /** * The class representing the [style of a timestamp](https://discord.com/developers/docs/reference#message-formatting-timestamp-styles) From 20890414d3ef379ab5fb5fd99c9a9119ad0c37fb Mon Sep 17 00:00:00 2001 From: warrior Date: Fri, 3 Sep 2021 14:05:11 +0200 Subject: [PATCH 6/6] Use seconds instead of milliseconds, so it will show up the right way --- common/src/main/kotlin/DiscordTimestamp.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/src/main/kotlin/DiscordTimestamp.kt b/common/src/main/kotlin/DiscordTimestamp.kt index 2b7eaa041667..a6711a49e7ac 100644 --- a/common/src/main/kotlin/DiscordTimestamp.kt +++ b/common/src/main/kotlin/DiscordTimestamp.kt @@ -3,7 +3,7 @@ package dev.kord.common import kotlinx.datetime.Instant fun Instant.toMessageFormat(style: DiscordTimestampStyle = DiscordTimestampStyle.ShortDateTime) = - "" + "" /** * The class representing the [style of a timestamp](https://discord.com/developers/docs/reference#message-formatting-timestamp-styles)