The Go Programming Language
› http://golang.org/
› Go Playground
Go Projects
› Revel Web Framework
imherer

Go 时间格式化问题

  •  
  •   imherer · May 13, 2019 · 4886 views
    This topic created in 2732 days ago, the information mentioned may be changed or developed.

    数据库用的 postgresql,字段类型为:TIMESTAMP

    因为这一列的值可能存在null所以在 scan 的时候这一列的变量类型定义为:sql.NullString,这样最后如果有值的话得到的数据为这样的格式:"xxxx-xx-xxTxx:xx:xxZ"

    这样会报错:time.Parse("layout", "xxxx-xx-xxTxx:xx:xxZ")

    写时间 v 站就要让验证手机号:所以上面的时间和 layout 没写具体的值

    23 replies  •  2019-05-14 10:20:52 +08:00
    kungfuchicken
    kungfuchicken
    May 13, 2019
    1
    每个字都看懂了,但是我没看懂你在问啥
    imherer
    imherer
    OP
    May 13, 2019
    2
    @kungfuchicken 刚才一直在编辑主题,里面不知道里面哪个时间写的不对,v 站一直让验证手机
    imherer
    imherer
    OP
    May 13, 2019
    3
    @kungfuchicken 想把这个直接格式化成北京时间
    www5070504
    www5070504
    May 13, 2019
    4
    没有指定时间格式的转换成时间戳的参数么
    imherer
    imherer
    OP
    May 13, 2019
    5
    @www5070504 我也刚接触 go 不久,官方的``time``包找了下好像是没有。 第三方的不清楚有没有。
    lcdtyph
    lcdtyph
    May 13, 2019 via iPhone
    6
    我猜你要找的是 strptime,不知道 go 有没有
    imherer
    imherer
    OP
    May 13, 2019
    7
    @lcdtyph 解决了,是前面 layout 不对。我 layout 用的 xxxx-xx-xx xx:xx:xx,这里需要用 xxxx-xx-xxTxx:xx:xxZ
    reus
    reus
    May 13, 2019
    8
    用 SQL 表达式 extract(epoch from xxx)::bigint 就可以返回时间戳,不用在 go 代码里解析
    reus
    reus
    May 13, 2019
    9
    数据库返回的不一定是 "xxxx-xx-xxTxx:xx:xxZ" 格式。默认应该是 ISO 格式的,你这里不知道是哪里转换成了 ISO 8601 格式。你要确定数据库和程序库会一直输出 ISO 8601 格式才行,不然以后可能会出错。
    keepeye
    keepeye
    May 13, 2019
    10
    没错,go 从数据库读出来的时间显示格式就是 tz 格式,很恶心,要自己 parse 再 format 成 datetime 格式
    keepeye
    keepeye
    May 13, 2019
    11
    不过我建议你字段类型直接设置成 *time.Time,不用字符串,省了再去 parse
    learnshare
    learnshare
    May 13, 2019
    12
    @keepeye 标准时间格式怎么恶心了 https://zh.wikipedia.org/wiki/ISO_8601
    本应该只在呈现的地方格式化
    keepeye
    keepeye
    May 13, 2019
    13
    @learnshare 变量类型是 time.Time 没问题。问题是 string 类型的,为什么要转成这种格式呢? mysql 的 timestamp 模式显示格式不是 yyyy-mm-dd hh:ii:ss 吗?
    Mitt
    Mitt
    May 13, 2019 via iPhone
    14
    @learnshare 问题不应该是 time.Parse 不能用这个值反格式化吗
    learnshare
    learnshare
    May 13, 2019
    15
    @keepeye 不是转成这种格式,是默认应该是这种格式

    @Mitt 楼主已经讲了,自己的 layout 格式不匹配
    heimeil
    heimeil
    May 13, 2019
    16
    time.RFC3339
    imherer
    imherer
    OP
    May 13, 2019
    17
    @keepeye 变量类型设置成 time.Time 的话,如果某一个值为 null 的时候 scan 会报错的。
    imherer
    imherer
    OP
    May 13, 2019
    18
    @heimeil
    @learnshare
    @keepeye
    对的。layout 用 time.RFC3339 就 ok 了

    不过结果最后会多一个 +0000 UTC,还得 Format 一次....
    imherer
    imherer
    OP
    May 13, 2019
    19
    @reus "用 SQL 表达式 extract(epoch from xxx)::bigint 就可以返回时间戳,不用在 go 代码里解析"

    这个对数据库的效率会有影响吗?会不会影响数据库性能呢?
    rrfeng
    rrfeng
    May 13, 2019
    20
    这肯定是 layout 有问题啊……
    YakuMioto
    YakuMioto
    May 13, 2019 via Android
    21
    2006-01-02T13:04:05Z
    reus
    reus
    May 14, 2019
    22
    @imherer 不会。你现在拿到的字符串一样是转换出来的结果。
    imherer
    imherer
    OP
    May 14, 2019
    23
    @reus 好的。谢谢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1289 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 74ms · UTC 22:16 · PVG 06:16 · LAX 14:16 · JFK 17:16
    ♥ Do have faith in what you're doing.