mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-06-30 05:17:47 +00:00
37 lines
939 B
Kotlin
37 lines
939 B
Kotlin
|
package com.github.insanusmokrassar.TelegramBotAPI.types
|
||
|
|
||
|
import kotlinx.serialization.*
|
||
|
import org.joda.time.DateTime
|
||
|
import java.util.concurrent.TimeUnit
|
||
|
|
||
|
@Serializable(TelegramDateSerializer::class)
|
||
|
class TelegramDate(
|
||
|
private val date: Long
|
||
|
) {
|
||
|
constructor(dateTime: DateTime) : this(
|
||
|
TimeUnit.MILLISECONDS.toSeconds(dateTime.millis)
|
||
|
)
|
||
|
|
||
|
@Transient
|
||
|
val asDate: DateTime = DateTime(
|
||
|
TimeUnit.SECONDS.toMillis(date)
|
||
|
)
|
||
|
}
|
||
|
|
||
|
fun DateTime.toTelegramDate(): TelegramDate = TelegramDate(this)
|
||
|
|
||
|
@Serializer(TelegramDate::class)
|
||
|
internal class TelegramDateSerializer: KSerializer<TelegramDate> {
|
||
|
override fun serialize(output: Encoder, obj: TelegramDate) {
|
||
|
output.encodeLong(
|
||
|
TimeUnit.MILLISECONDS.toSeconds(obj.asDate.millis)
|
||
|
)
|
||
|
}
|
||
|
|
||
|
override fun deserialize(input: Decoder): TelegramDate {
|
||
|
return TelegramDate(
|
||
|
input.decodeLong()
|
||
|
)
|
||
|
}
|
||
|
}
|