V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
imherer
V2EX  ›  Go 编程语言

Go 时间格式化问题

  •  
  •   imherer · 2019-05-13 13:34:13 +08:00 · 3858 次点击
    这是一个创建于 1809 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

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

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

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

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

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

    这个对数据库的效率会有影响吗?会不会影响数据库性能呢?
    rrfeng
        20
    rrfeng  
       2019-05-13 20:09:39 +08:00
    这肯定是 layout 有问题啊……
    YakuMioto
        21
    YakuMioto  
       2019-05-13 20:14:12 +08:00 via Android
    2006-01-02T13:04:05Z
    reus
        22
    reus  
       2019-05-14 08:43:52 +08:00
    @imherer 不会。你现在拿到的字符串一样是转换出来的结果。
    imherer
        23
    imherer  
    OP
       2019-05-14 10:20:52 +08:00
    @reus 好的。谢谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5557 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 03:32 · PVG 11:32 · LAX 20:32 · JFK 23:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.