1

我试图通过建立一个实践博客来加快 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
4

1 回答 1

2

所以我看了一下 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?
)
于 2019-01-05T03:27:10.927 回答