日期/时间添加标准 [英] Standards for Date/Time addition?

查看:175
本文介绍了日期/时间添加标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找日期/时间添加的标准。我找不到任何东西。特别是我希望找到一个规范,定义当你在1月31日之前添加一个月的时候应该发生什么。是正确的答案2月28日(/ 29日)? 3月1日3月2日?

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?

我已经看到不同工具之间的不一致的实现(在这种情况下是PHP和MySQL),我试图找到一些基础的标准我的工作。

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.

不同的结果:

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

(对于格式更改抱歉,我的oracle foo是弱)

(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


推荐答案

根据POSIX.1-2001标准,下个月(在递增 tm_mon 之前调用 mktime )通过调整值直到适合。所以,例如,2001年1月31日下个月是2001年3月3日。这是因为$ code> tm_mday 的31不符合 tm_mon 为1(2月),所以它被标准化为2(3月)和 tm_mday tm_mon c> of 3。

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.

2000年1月31日下个月是2000年3月2日,因为2月份有29天。根据

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.

标准的伟大之处在于有很多选择。检查SQL标准,我敢打赌你可以找到下个月的不同意思。我怀疑ISO 8601可能会给你另一个选择。要点是,有很多不同的行为,下个月的含义是非常具体的领域。

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.

编辑:我认为 ve发现SQL-92如何处理它,显然从1月31日起要求下个月是一个错误。

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

链接:

  • 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

这篇关于日期/时间添加标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆