Standards for Date/Time addition?

2019-03-11 07:20发布

问题:

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 month to a date like January 31st. Is the right answer February 28th(/29th)? March 1st? March 2nd?

I've seen inconsistent implementations between different tools (PHP & MySQL in this case), and I'm trying to find some sort of standards to base my work on.

Differing Results:

PHP

$end = strtotime("+1 month", 1314835200);
//1317513600   Sat, 01 Oct 2011 20:00:00 -0400

MySQL

SELECT UNIX_TIMESTAMP(DATE_ADD(FROM_UNIXTIME(1314835200), INTERVAL 1 MONTH));
#1317427200    Fri, 30 Sep 2011 20:00:00 -0400

Oracle

SELECT ADD_MONTHS('31-Aug-11', 1) FROM dual;
#30-SEP-11

(sorry for the format change, my oracle foo is weak)

Java

Calendar c = Calendar.getInstance();
c.clear();
c.set( 2011, Calendar.AUGUST, 31 );
c.add( Calendar.MONTH, 1 );
c.getTime()
#Fri Sep 30 00:00:00 EDT 2011

回答1:

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


回答2:

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.



回答3:

Query:

SELECT
ADDDATE(DATE('2010-12-31'), INTERVAL 1 MONTH) 'Dec + Month',
ADDDATE(DATE('2011-01-31'), INTERVAL 1 MONTH) 'Jan + Month',
ADDDATE(DATE('2011-02-28'), INTERVAL 1 MONTH) 'Feb + Month',
ADDDATE(DATE('2011-03-31'), INTERVAL 1 MONTH) 'Mar + Month';

Output:

    Dec + Month  Jan + Month  Feb + Month   Mar + Month
    2011-01-31   2011-02-28   2011-03-28    2011-04-30

My conclusion:

  1. Calculate the number of days in the month of the input date.
  2. Add that many days to the input date.
  3. Check if the day in the resulting date exceeds the maximun number of days in the resulting month.
  4. If yes, then change the resulting day to maximum day of the resulting month.

If you add MONTH, YEAR_MONTH, or YEAR and the resulting date has a day that is larger than the maximum day for the new month, the day is adjusted to the maximum days in the new month

source

Problem here is that it doesn't mention that the month is actually the month from the input date.



回答4:

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!



回答5:

First day of the month + 1 month should equal the first of the next month. Trying this on SQL Server

          SELECT CAST ('01/01/2012' AS DateTime), DATEADD (m, 1, '01/01/2012')
UNION ALL SELECT CAST ('02/01/2012' AS DateTime), DATEADD (m, 1, '02/01/2012')
UNION ALL SELECT CAST ('03/01/2012' AS DateTime), DATEADD (m, 1, '03/01/2012')
UNION ALL SELECT CAST ('04/01/2012' AS DateTime), DATEADD (m, 1, '04/01/2012')
UNION ALL SELECT CAST ('05/01/2012' AS DateTime), DATEADD (m, 1, '05/01/2012')

This results in

----------------------- -----------------------
2012-01-01              2012-02-01             
2012-02-01              2012-03-01             
2012-03-01              2012-04-01             
2012-04-01              2012-05-01             
2012-05-01              2012-06-01             

Last day of this month + 1 month should equal last day of next month. This should go for next month, current month, 10 months down, etc.

          SELECT CAST ('01/31/2012' AS DateTime), DATEADD (m, 1, '01/31/2012')
UNION ALL SELECT CAST ('01/30/2012' AS DateTime), DATEADD (m, 1, '01/30/2012')
UNION ALL SELECT CAST ('01/29/2012' AS DateTime), DATEADD (m, 1, '01/29/2012')
UNION ALL SELECT CAST ('01/28/2012' AS DateTime), DATEADD (m, 1, '01/28/2012')
UNION ALL SELECT CAST ('01/27/2012' AS DateTime), DATEADD (m, 1, '01/27/2012')
UNION ALL SELECT CAST ('01/26/2012' AS DateTime), DATEADD (m, 1, '01/26/2012')

This results in

----------------------- -----------------------
2012-01-31              2012-02-29             
2012-01-30              2012-02-29             
2012-01-29              2012-02-29             
2012-01-28              2012-02-28             
2012-01-27              2012-02-27             
2012-01-26              2012-02-26             

See how 31, 30, 29 all become feb 29 (2012 is a leap year).

p.s. I took off the time parts (all zeroes) to help make it more readable



回答6:

There is no widely accepted standard. The reason for the different implementations is that folks can't agree on what the standard should be. Many popular software systems give answers that no one would expect. Documentation is always necessary, therefore, to tell the user what your system will deliver. You choose a methodology, however, based on what you think most folks will expect.

I think most people on the street would agree that:

  1. You can't define a 'month' by a certain number of days. So...
  2. When it's Jan 31 and someone says "I'll meet you a month from today", they mean the last day of February. Basically, it's add to the month, then find the day number that is closest to today without exceeding it.

The exception comes in accounting, where sometimes a month means 30 days.

EDIT: I asked some folks around here, "If it's March 31 and someone says I'll meet you a month from today, what day are you going to meet them?" Most said, April 30, but a few said April 28 because it's four weeks away. The few were interpreting work schedules and thinking "if we met on this weekday, we'll meet again on the same weekday". Basically, if they met on the last Thursday of the month, and they're due to meet in one month, it'll be on the last Thursday of that month.

So, there ya go. :\



回答7:

Try the mysql date function :

SELECT ADDDATE('2011-01-31', INTERVAL 1 MONTH) // 2011-02-28

Input date with leap year

SELECT ADDDATE('2012-01-31', INTERVAL 1 MONTH) // 2012-02-29



回答8:

In the .NET framework the behavior of System.DateTime.AddMonths is as follows:

The AddMonths method calculates the resulting month and year, taking into account leap years and the number of days in a month, then adjusts the day part of the resulting DateTime object. If the resulting day is not a valid day in the resulting month, the last valid day of the resulting month is used. For example, March 31st + 1 month = April 30th [rather than April 31st].

I've tested how it works exactly:

Console.WriteLine(new DateTime(2008,2,27).AddMonths(1));
Console.WriteLine(new DateTime(2008,2,28).AddMonths(1));
Console.WriteLine(new DateTime(2008,2,29).AddMonths(1));
Console.WriteLine(new DateTime(2011,2,27).AddMonths(1));
Console.WriteLine(new DateTime(2011,2,28).AddMonths(1));
Console.WriteLine(new DateTime(2008,1,30).AddMonths(1));
Console.WriteLine(new DateTime(2008,1,31).AddMonths(1));
Console.WriteLine(new DateTime(2011,1,30).AddMonths(1));
Console.WriteLine(new DateTime(2011,1,31).AddMonths(1));
/* output
3/27/2008 12:00:00 AM
3/28/2008 12:00:00 AM
3/29/2008 12:00:00 AM
3/27/2011 12:00:00 AM
3/28/2011 12:00:00 AM
2/29/2008 12:00:00 AM
2/29/2008 12:00:00 AM
2/28/2011 12:00:00 AM
2/28/2011 12:00:00 AM
    */