Datetime comparison near full hours
1 view (last 30 days)
Show older comments
Hello,
I have this piece of code that compares two sets of datetimes:
c = 1
for k = 1:size(TR_SoftStart,1)
Years = abs(year(PC_SoftStart) - year(TR_SoftStart.Data(k))) < 1;
Months = abs(month(PC_SoftStart) - month(TR_SoftStart.Data(k))) < 1;
Days = abs(day(PC_SoftStart) - day(TR_SoftStart.Data(k))) < 1;
Hours = abs(hour(PC_SoftStart) - hour(TR_SoftStart.Data(k))) < 1;
Minutes = abs(minute(PC_SoftStart) - minute(TR_SoftStart.Data(k))) <= 1;
Test = Years & Months & Days & Hours & Minutes;
if any(Test,'all')
disp(k)
else
errors_SoftStart(c,:) = TR_SoftStart(k,:);
c = c + 1;
end
end
It basically checks if the datetimes on TR_SoftStart have pairs on PC_SoftStart with a ±1 minute tolerance. However, there is one case in which the script doesn't work. Whenever it's close to the full hour (e.g.: 15:59:00 and 16:00:00) it doesn't understand that it infact has just 1 minute between them. I know that it's because the math used analyses only the individual parameters (years, then months, and so on). I'm really struggling to come up with a workaround for this. Any ideas?
Thanks in advance,
Arthur.
Accepted Answer
Hank
on 9 Mar 2020
Edited: Hank
on 9 Mar 2020
Don't bother splitting the time components yourself.
t1 = datetime('9-mar-2020 16:00:00');
t0 = datetime('9-mar-2020 15:59:03');
dt = t1-t0
dt =
duration
00:00:57
Here are two datetimes which are 1 minute appart. Subtracting them returns a duration datatype.
Set your tolerance value as another duration object
tol = duration(0,1,0); % 1 minute duration
dt < tol; % true
2 Comments
Steven Lord
on 10 Mar 2020
Don't forget to wrap it in abs.
(t0-t1) < seconds(1) % true since t0-t1 is negative
abs(t0-t1) < seconds(1) % false
More Answers (2)
dpb
on 10 Mar 2020
isBad=~cell2mat(arrayfun(@(t) any(abs(PC_SoftStart-t)<=minutes(1)&~isnat(PC_SoftStart),'all'),TR_SoftStart.Data,'UniformOutput',0));
TSS_Missing=TR_SoftStart(isBad,:);
results
>> TSS_Missing(1:5,:)
ans =
5×2 table
Status Data
_________________ ____________________
"Aumento_Gradual" 27-Jun-2019 06:17:00
"Aumento_Gradual" 28-Jun-2019 00:24:00
"Aumento_Gradual" 28-Jun-2019 22:22:00
"Aumento_Gradual" 29-Jun-2019 16:52:00
"Aumento_Gradual" 30-Jun-2019 13:25:00
>>
Check results...
>> PC=sort(PC_SoftStart(~isnat(PC_SoftStart)));
PC(1:10)
ans =
10×1 datetime array
27.Jun.2019 00:00:00
27.Jun.2019 05:35:00
27.Jun.2019 05:56:00
27.Jun.2019 15:14:00
27.Jun.2019 15:35:00
27.Jun.2019 23:50:00
28.Jun.2019 00:00:00
28.Jun.2019 00:11:00
28.Jun.2019 21:16:00
28.Jun.2019 21:18:00
>>
Interval including first two in the missing table doesn't have a match as shouldn't...
0 Comments
Steven Lord
on 10 Mar 2020
I'm not certain, but from the way you've described your problem storing your time-based data as a timetable and using withtol indexing to extract the appropriate rows from that timetable may be useful to you. See the "Index on Specified Times with Tolerances" section on this documentation page for an example that seems somewhat similar to your use case.
1 Comment
dpb
on 10 Mar 2020
Edited: dpb
on 10 Mar 2020
Ah! I'd come across withtol once before but forgot about it, Steven!
>> isOK=withtol(TR_SoftStart.Data,minutes(1));
Error using withtol (line 78)
Tolerance exceeds half the smallest interval between subscript times, and might
result in selecting duplicate rows. Tolerance must be less than 1 min.
>> min(diff(sort(TR_SoftStart.Data)))
ans =
duration
00:02:00
>>
It will take seconds(59.999) however.
I've not tried to revamp the above to make use of; looks like would have to recast to a timetalble to use the resulting selection object as it doesn't seem to serve as just an indexing expression into a datetime array.
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!