【问题标题】:How to handle Postgres timestamps with Kotlin?如何使用 Kotlin 处理 Postgres 时间戳?
【发布时间】:2019-01-05 01:43:14
【问题描述】:

我正在尝试通过建立一个实践博客来跟上 Postgres 和 Kotlin 的速度。现在,我只是在处理虚拟内容,但几周后,我将开始一个更大的项目。我在示例应用程序中生成的时间戳似乎有点疯狂。我在这里做错了什么?

我得到了一些巨大的日期对象,我真的只想要一个像 psql sh 给出的时间戳:

 SELECT * FROM posts;
 id |   title   |       content       |   type    | status |        datecreated         |        datemodified
----+-----------+---------------------+-----------+--------+----------------------------+----------------------------
  1 | Hey there | This is the content | Hey there | draft  | 2019-01-04 20:28:05.978762 | 2019-01-04 20:28:05.978762

控制器:

class PostController {
    fun index(): ArrayList<Post> {
        val posts: ArrayList<Post> = arrayListOf()
        transaction {
            Posts.selectAll().map { it ->
                posts.add(Post(
                        id = it[Posts.id], content = it[Posts.content],
                        title = it[Posts.title], type = it[Posts.type],
                        status = it[Posts.status], dateCreated = it[Posts.dateCreated],
                        dateModified = it[Posts.dateModified]
                ))
            }
        }
        return posts
    }
}

数据类:

import org.joda.time.DateTime

data class Post(
        val id: Int,
        val title: String,
        val content: String,
        val type: String,
        val status: String,
        val dateCreated: DateTime?,
        val dateModified: DateTime?
)

我正在使用 Jetbrains Exposed 库。它没有在文档中提到这样的事情:

导入 org.jetbrains.exposed.sql.Table

object Posts : Table() {
    val id = integer("id").primaryKey().autoIncrement()
    val title = text("title")
    val content = text("content")
    val type = text("type")
    val status = text("status")
    val dateCreated = datetime("datecreated")
    val dateModified = datetime("datemodified")
}

回复:

{
  "id": 1,
  "title": "Hey there",
  "content": "This is the content",
  "type": "Hey there",
  "status": "draft",
  "dateCreated": {
    "iMillis": 1546651744314,
    "iChronology": {
    "iBase": {
"iBase": {
"iBase": {
"iMinDaysInFirstWeek": 4
}
},
"iParam": {
"iZone": {
"iTransitions": [
-9223372036854776000,
...... goes on, seemingly forever

【问题讨论】:

    标签: postgresql kotlin kotlin-exposed


    【解决方案1】:

    所以我查看了 Exposed 源代码中的 DateColumnType.kt。显然这一直是一个选项:

    data class Post(
            val id: Int,
            val title: String,
            val content: String,
            val type: String,
            val status: String,
            val dateCreated: String?,
            val dateModified: String?
    )
    

    【讨论】:

    • 你能详细说明一下吗?我不明白做了什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 2013-01-14
    • 1970-01-01
    • 2016-07-11
    • 2021-09-09
    • 1970-01-01
    相关资源
    最近更新 更多