Problems trying to find fakes dates.
1 回表示 (過去 30 日間)
古いコメントを表示
idx =
2005 2 28 18 0 0
2005 2 28 19 0 0
2005 2 28 20 0 0
2005 2 28 21 0 0
2005 2 28 22 0 0
2005 2 28 23 0 0
2005 3 1 0 0 0
2005 3 1 1 0 0
2005 3 1 2 0 0
>> time = datenum(idx)
time =
732371.75
732371.791666667
732371.833333333
732371.875
732371.916666667
732371.958333333
732372
732372.041666667
732372.083333333
% looking for a date that is in the domain
>> find(time == datenum(2005,2,28,18,0,0))
ans =
1
% looking for a date that is NOT in the domain
>> find(time == datenum(2005,2,29,0,0,0))
ans =
7
% the output shuld be 0×1 empty double column vector.
% Why when u try to find fakes dates, the output is a value of an existing date in the domain?
3 件のコメント
回答 (2 件)
the cyclist
2023 年 1 月 11 日
編集済み: the cyclist
2023 年 1 月 11 日
As recommended there, and many other places in the documentation, use of the newer datetime data type is encouraged.
2 件のコメント
the cyclist
2023 年 1 月 11 日
Sorry, I should not have implied that using datetime would solve the carryover issue. datetime also implement the carryover algorithm discussed on that page.
I did some searching, and did not find a definitve date validation method in MATLAB. The topic has come up a few times in this forum. It seems to me that this thread is your best bet, which seems to have settled on this code:
function valid = valid_date(year, month, date)
if(nargin ~= 3)
valid = false;
elseif ((~isscalar(year)||(mod(year,1)~=0) || year<0))
valid = false;
elseif ((~isscalar(month))||(mod(month,1)~=0) || (month<=0) || (month>12))
valid = false;
elseif ((~isscalar(date))||(mod(date,1)~=0) || (date<=0))
valid = false;
elseif(any(month==[1:2:7,8:2:12])&& date>31)
valid = false;
elseif (any(month==[4,6,9,11]) && date>30)
valid = false;
elseif month==2 && date>(28+(mod(year,400)==0 || (mod(year,100)~=0 && mod(year,4)==0)))
valid=false;
else
valid = true;
end
Eric Sofen
2023 年 1 月 26 日
The reason datetime(2005,2,29) works is that we want to support constructing a vector of datetimes:
datetime(2023,1,1:100)
Note that this is only for constructing datetime from numeric inputs. For text timestamps, it needs to be a valid date/time on the calendar:
% This errors:
% datetime("2005-02-29")
% Error using datetime
% Could not recognize the date/time format of '2005-02-29'. You can specify a format using the 'InputFormat' parameter. If the date/time text contains day, month, or time zone names in a language foreign to the 'en_US' locale, those might not be recognized. You can specify a different locale using the 'Locale' parameter.
In your actual workflow, do you have access to the original Y/M/D data before constructing a datetime from it? If so, you could compare the month/day resulting from constructing the datetime to the original to check for spurious data with values that datetime wrapped to the next month:
ymd = [2005 2 28; 2005 2 29; 2005 3 1]
d = datetime(ymd);
find(d.Month == ymd(:,2))
I'd be hesitant to construct a full date validation scheme as @the cyclist laid out. Yes, it's doable, but many over the years have fallen into various traps around leap years and Daylight Saving Time!
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Dates and Time についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!