Standards for Date/Time addition?

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

    Joda-Time in Java chooses the previous valid date when an invalid one is created. For example, 2011-01-31 + P1M = 2011-02-28. I believe this is the most widely chosen default choice in date-time libraries, and thus a de facto standard.

    ThreeTen/JSR-310 provides a strategy pattern for this, with four choices, see the code.

    More amusing is the question of what the answer to 2011-01-31 + P1M-1D is. If you add the month, then resolve the invalid date, then subtract the day, you get 2011-02-27. But I think most users expect 2011-02-28 because the period is being added in a single lump. See how ThreeTen handles this here.

    I have considered trying to write a general purpose best practices in date/time calculations, or actual spec, but haven't really had the time!

提交回复
热议问题