Standards for Date/Time addition?

前端 未结 8 1112
自闭症患者
自闭症患者 2021-02-02 15:40

I\'m looking for standards for Date/Time addition. I haven\'t been able to find any. In particular I\'m hoping to find a spec that defines what should happen when you add a mont

8条回答
  •  礼貌的吻别
    2021-02-02 16:08

    According to the POSIX.1-2001 standard, next month (as in incrementing tm_mon before calling mktime) is done by adjusting the values until they fit. So, for example, next month from January 31, 2001 is March 3, 2001. This is because the tm_mday of 31 isn't valid with tm_mon of 1 (February), so it is normalized to tm_mon of 2 (March) and tm_mday of 3.

    The next month from January 31, 2000 is March 2, 2000, because Feb. has 29 days that year. The next month from January, 1 2038 doesn't exist, depending.

    The great thing about standards is there are so many to chose from. Check the SQL standard, I bet you can find a different meaning of next month. I suspect ISO 8601 may give you yet another choice. Point is, there are many different behaviors, the meaning of 'next month' is very domain-specific.

    edit: I think I've found how SQL-92 handles it, apparently asking for next month from January 31 is an error.

    Links:

    • SQL-92: http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt
    • POSIX: http://pubs.opengroup.org/onlinepubs/9699919799/ (though apparently that version now defers to ISO C, which doesn't seem as clear. The mktime manpage on my machine is clear, though)
    • ISO C: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf
    • Java: http://download.oracle.com/javase/6/docs/api/java/util/Calendar.html

提交回复
热议问题