datetime error/inconsistency

>> datetime(1963,0,213)
ans =
01-Jul-1963
>> datestr(datenum(1963,0,213))
ans =
01-Aug-1963
>> sum(eomday(1963,1:7))
ans =
212
>>
First is wrong; old datenum gets it right.
Whassup w/ that???

댓글 수: 2

Stephen23
Stephen23 2017년 9월 15일
편집: Stephen23 2017년 9월 15일
"old datenum gets it right."
I always found datenum's inconsistent treatment of zero and negative month values as bizarre and sometimes downright counterproductive. Good to see that this has been fixed with datetime.
James Tursa
James Tursa 2017년 9월 15일
@Stephen: "bizarre" ... agreed. The special treatment of zero or negative month values was never intuitive to me and I could never come up with a reason why I would want that behavior. I much prefer the datetime treatment.

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2017년 9월 15일

0 개 추천

Month 0 is the last month of the previous year, not the first month of that year.
"If an element of the Y, M, or D inputs falls outside the conventional range, then datetime adjusts both that element and the same element of the previous input."

댓글 수: 3

dpb
dpb 2017년 9월 15일
But the examples don't help with this one; it adjusts 70 min to 1 hr 10 min the next hour, or -15 min goes back one hour and leaves 45 sec.
Y + 0M + NDays isn't Y-1M + N Days; it's the N Days of the year, just as datenum has done "since forever". It's introducing yet another inconsistent incompatibility that's unnecessary that's the most frustrating.
Walter Roberson
Walter Roberson 2017년 9월 15일
편집: Walter Roberson 2017년 9월 15일
[2014 1 1] is not one month and one day past the beginning of 2014: it is 0 months and 0 days past the beginning of 2014. Therefore [2014 0 1] is 1 month and 0 days before the beginning of 2014, which is [2013 12 1]
Stephen23
Stephen23 2017년 9월 15일
편집: Stephen23 2017년 9월 15일
The inconsistency is fundamentally a human one: we start counting all time units from zero, but start counting from one for dates, months and years ( year zero is a myth, and has never been used in the Anno Domini system):
>> datevec(1)
ans =
0 1 1 0 0 0
IMO it is quite appropriate to treat month 0 as being the previous month from month 1, and would expect this to extend in both directions without any special cases.... even better, we could simply change our language conventions and use a consistent start value for all units. Anyone up for a change?

댓글을 달려면 로그인하십시오.

James Tursa
James Tursa 2017년 9월 15일
편집: James Tursa 2017년 9월 15일

0 개 추천

This inconsistency has been noted before in previous posts. E.g., with datenum you get the same answer with month=0 and month=1:
>> datenum(1963,0,1)
ans =
716972
>> datenum(1963,1,1)
ans =
716972
I would have to do a search to try and find those relevant old posts to see what the conclusions were, if any. From the datenum doc:
"If an element falls outside the conventional range, datenum adjusts both that date vector element and the previous element. For example, if the minutes element is 70, datenum adjusts the hours element by 1 and sets the minutes element to 10. If the minutes element is -15, then datevec decreases the hours element by 1 and sets the minutes element to 45. Month values are an exception. datenum sets month values less than 1 to 1."
So, for what the user probably intends, the datetime interpretation of month=0 is probably what would be considered "correct". But that could of course depend on the actual application. E.g., to get a match:
>> datenum(1963,1,213-31) <-- adjusted by number of days in December
ans =
717153
>> datenum(datetime(1963,0,213))
ans =
717153

댓글 수: 1

dpb
dpb 2017년 9월 15일
I just discovered it.
I'm adamantly opposed to the new interpretation -- see note to Walter on why.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

제품

질문:

dpb
2017년 9월 15일

댓글:

2017년 9월 15일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by