mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-06-16 23:05:26 +00:00
40 lines
1004 B
Kotlin
40 lines
1004 B
Kotlin
package com.github.insanusmokrassar.TelegramBotAPI.types
|
|
|
|
import kotlinx.serialization.*
|
|
import org.joda.time.DateTime
|
|
import java.util.concurrent.TimeUnit
|
|
|
|
@Serializable(TelegramDateSerializer::class)
|
|
data class TelegramDate(
|
|
/**
|
|
* Contains UNIX time (seconds)
|
|
*/
|
|
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 object TelegramDateSerializer : KSerializer<TelegramDate> {
|
|
override fun serialize(encoder: Encoder, obj: TelegramDate) {
|
|
encoder.encodeLong(
|
|
TimeUnit.MILLISECONDS.toSeconds(obj.asDate.millis)
|
|
)
|
|
}
|
|
|
|
override fun deserialize(decoder: Decoder): TelegramDate {
|
|
return TelegramDate(
|
|
decoder.decodeLong()
|
|
)
|
|
}
|
|
}
|