Standards for Date/Time addition?

前端 未结 8 1120
自闭症患者
自闭症患者 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:26

    I believe the defacto standard is ISO 8601. Unfortunately, there are many ambiguities, for example:

    Date arithmetic is not defined

    2001-03-30 + P1M = 2001-04-29 (Add 30 days)
    2001-03-30 + P1M = 2001-04-30 (Add 1 mon.)
    

    Addition is not commutative or associative

    2001-03-30 + P1D + P1M = 2001-04-30
    2001-03-30 + P1M + P1D = 2001-05-01
    

    Subtraction is not the inverse of Addition.

    Precision of decimal fractions can vary.

    The full specification can be found at http://www.iso.org/iso/catalogue_detail.htm?csnumber=26780

    I think each product is attempting to adhere to an impossible to implement standard. The ambiguous parts are open to interpretation and so everyone interprets. This is the same standard that opened us up to the Y2K bug!!

    Myself, I favor an implementation that converts a date/time to a 1970 based number (UNIX timestamp), performs the calculation and converts back. I believe this is the approach taken by Oracle/MySQL. I am surprised that more attention has not been paid this issue, as it is really important, sometimes critical, in so many applications. Thanks for the question!

    Edit: While doing some more reading, I found Joe Celko's thoughts on different date/time representations and standardization HERE.

提交回复
热议问题