• 令人困惑的 Go time.AddDate


    我们经常会使用 Go time 包 AddDate() ,对日期进行计算。而它得到的结果,可能会往往超出我们的“预期”。(为什么预期要打引号,因为我们的预期可能是模糊、偏差的)。

    引例

    假设,今天是10月31日,是10月的最后一天,我们想通过 AddDate() 计算下个月的最后一天。

    today := time.Date(2022, 10, 31, 0, 0, 0, 0, time.Local)
    nextDay := today.AddDate(0, 1, 0)
    fmt.Println(nextDay.Format("20060102"))
    
    // 输出:20221201
    
    • 1
    • 2
    • 3
    • 4
    • 5

    结果输出: 20221201 ,而非我们预期的下个月最后一天11月30日。

    Go Time 包中是这么处理的:

    AddDate()
    Format()
    
    • 1
    • 2

    只要是涉及到大小月的最后一天都会出现这个问题。

    today := time.Date(2022, 3, 31, 0, 0, 0, 0, time.Local)
    d := today.AddDate(0, -1, 0)
    fmt.Println(d.Format("20060102"))
    // 20220303
    
    today := time.Date(2022, 3, 31, 0, 0, 0, 0, time.Local)
    d := today.AddDate(0, 1, 0)
    fmt.Println(d.Format("20060102"))
    // 20220501
    
    today := time.Date(2022, 10, 31, 0, 0, 0, 0, time.Local)
    d := today.AddDate(0, -1, 0)
    fmt.Println(d.Format("20060102"))
    // 20221001
    
    today := time.Date(2022, 10, 31, 0, 0, 0, 0, time.Local)
    d := today.AddDate(0, 1, 0)
    fmt.Println(d.Format("20060102"))
    // 20221201
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    源码分析

    看一下 Go Time 包具体源码,仍以开头 10-31 + 1 month 的例子为用例。

    AddDate() ,首先对 month+1 ,然后调用 Date() 处理。

    // time/time.go
    
    func (t Time) AddDate(years int, months int, days int) Time {
        year, month, day := t.Date() // 获取当前年月日
        hour, min, sec := t.Clock() // 获取当前时分秒
        return Date(year+years, month+Month(months), day+days, hour, min, sec, int(t.nsec()), t.Location())
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Date() 中此时传入的参数是

    • year 2020
    • month 11
    • day 31
    • hour、min、sec、nsec 为运行时的时分秒纳秒

    d 计算的是绝对纪元到今天之前的天数:

    **d = 今年之前的天数 + 年初到当月之前的天数 + 月初到当天之前的天数;**

    最终,将 d 转换成纳秒 + 当天经过的纳秒 存储在 Time 对象中。

    // time/time.go
    
    func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time {
        ……
    
        // Compute days since the absolute epoch.
        d := daysSinceEpoch(year)
    
        // Add in days before this month.
        d += uint64(daysBefore[month-1])
        if isLeap(year) && month >= March {
            d++ // February 29
        }
    
        // Add in days before today.
        d += uint64(day - 1)
    
        // Add in time elapsed today.
        abs := d * secondsPerDay
        abs += uint64(hour*secondsPerHour + min*secondsPerMinute + sec)
    
        ……
        return t
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    对 Date() 输入 2022-11-31 和输入 2022-12-01 ,将得到同样的 d(天数)。两者底层存储的时候都是一样的数据,Format() 时将 2022-11-31 的Time 格式化成 2022-12-01 也就不例外了,输出当然要显示让人看得懂的常规标准日期嘛。

    // 2022-11-31
    d = 2022年之前的天数 + 1月到10月的总天数 + 30天
    
    // 2022-12-01
    d = 2022年之前的天数 + 1月到11月的总天数 + 0天
      = 2022年之前的天数 + 1月到10月的总天数 + 30天 + 0天
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    你甚至可以往 Date() 输入非标准日期 2022-11-35 ,它和标准日期 2022-12-05 ,将得到同样的 d (天数)。

    “非标准日期”和“标准日期”就像天平的两边,虽然形式不一样,但他们实际的质量(d 天数)是一样的。记住这句话,后面有用。

    预期偏差

    我们弄清楚了原理,但仍然不能接受这个结果。这样的结果是 Go 的 bug 吗?还是 Go Time 包偷懒了?

    然而并不是,恰恰是我们的“预期”出现了问题。

    正常来说,我们预期 10-30 + 1 month 是 11-30 日,这很合理。那我们为什么还期待 10-31 + 1 month 也是 11-30 日?仅仅因为 10-31 是当前月的最后一天,我们也期待 +1 month 后是下个月的最后一天吗?

    10-30 和 10-31 两个日期相差一天,进行同样的 +1 month 操作后,就变成为了同一天。这就像 1 + 10 = 2 + 10 一样的结果,这显然不合理。

    Go 目前的处理结果是正确的,并且他在 AddDate() 注释中也注明了会处理“溢出”的情况。况且,不止 Go 语言这么处理,PHP 也是这么处理的,见鸟哥文章 令人困惑的strtotime - 风雪之隅 。

    怎么解决

    道理我都懂,但我就是想获取上/下一个月的最后一天怎么办?

    利用前面源码分析阶段,提到的“天平原理”,就能拿到我们想要的结果。

    today := time.Date(2022, 10, 31, 0, 0, 0, 0, time.Local)
    d := today.Day()
    
    // 上个月最后一天
    // 10-00 日 等于 9-30 日
    day1 := today.AddDate(0, 0, -d)
    fmt.Println(day1.Format("20060102"))
    
    // 下个月最后一天
    // 12-00 日 等于 11-30 日
    day2 := today.AddDate(0, 2, -d)
    fmt.Println(day2.Format("20060102"))
    
    // 20220930
    // 20221130
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    结语

    最初,发现这个问题是看鸟哥文章,当时认为那是 PHP 的“坑”,并没有深入思考过。如今,在 Go 语言再次遇到这个问题,重新思考,发现日期函数本应该就那么设计,是我们对日期函数理解不够,产生了错误的“预期”。

  • 相关阅读:
    多线程能否提高jdbc插入速度?
    快速构建基于Paddle Serving部署的Paddle Detection目标检测Docker镜像
    揭秘B站,程序员穿女装敲代码,效率更高是真的吗?
    LLM微调(一)| 单GPU使用QLoRA微调Llama 2.0实战
    Java面试题大全(2021版)
    从Multirepo到Monorepo 袋鼠云数栈前端研发效率提升探索之路
    C++学习之强制类型转换
    注解与反射学习
    RocketMq源码分析(八)--消息消费流程
    算法通关村第一关-链表青铜挑战笔记
  • 原文地址:https://blog.csdn.net/m0_70748381/article/details/126902682