1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-30 21:37:50 +00:00
tgbotapi/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/TelegramDate.kt

39 lines
891 B
Kotlin
Raw Normal View History

2018-12-26 08:07:24 +00:00
package com.github.insanusmokrassar.TelegramBotAPI.types
import com.soywiz.klock.DateTime
2019-12-06 10:46:38 +00:00
import kotlinx.serialization.*
2018-12-26 08:07:24 +00:00
@Serializable(TelegramDateSerializer::class)
data class TelegramDate(
/**
* Contains UNIX time (seconds)
*/
2019-12-03 05:07:25 +00:00
internal val date: Long
2018-12-26 08:07:24 +00:00
) {
constructor(dateTime: DateTime) : this(
2019-12-03 05:07:25 +00:00
dateTime.unixMillisLong / 1000
2018-12-26 08:07:24 +00:00
)
@Transient
val asDate: DateTime = DateTime(
2019-12-03 05:07:25 +00:00
date * 1000
2018-12-26 08:07:24 +00:00
)
}
fun DateTime.toTelegramDate(): TelegramDate = TelegramDate(this)
@Serializer(TelegramDate::class)
2019-11-29 06:25:22 +00:00
internal object TelegramDateSerializer : KSerializer<TelegramDate> {
2019-02-21 06:21:33 +00:00
override fun serialize(encoder: Encoder, obj: TelegramDate) {
encoder.encodeLong(
2019-12-03 05:07:25 +00:00
obj.date
2018-12-26 08:07:24 +00:00
)
}
2019-02-21 06:21:33 +00:00
override fun deserialize(decoder: Decoder): TelegramDate {
2018-12-26 08:07:24 +00:00
return TelegramDate(
2019-02-21 06:21:33 +00:00
decoder.decodeLong()
2018-12-26 08:07:24 +00:00
)
}
}