datetime error/inconsistency
Mostra commenti meno recenti
>> 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 Commenti
James Tursa
il 15 Set 2017
@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.
Risposte (2)
Walter Roberson
il 15 Set 2017
0 voti
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 Commenti
dpb
il 15 Set 2017
Walter Roberson
il 15 Set 2017
Modificato: Walter Roberson
il 15 Set 2017
[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]
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
il 15 Set 2017
Modificato: James Tursa
il 15 Set 2017
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 Commento
dpb
il 15 Set 2017
Categorie
Scopri di più su Dates and Time in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!