(Answers Dev) Restored edit
Info
This question is locked. 请重新打开它进行编辑或回答。
Locate and syncronize timestamp
1 次查看(过去 30 天)
显示 更早的评论
I have a raspberry without a real time clock which is creating minutelly files. To calculate the estimated time i have as indication the time that i pluged it off. I created a script like this:
last_record='08_05_10_57'; % Correct time and date
last_record_raspberry='13_03_14_33'; % Not correct time and date
tmp_last_record = str2double(split(last_record(1:length(last_record)),'_'));
tmp_last_record_raspberry = str2double(split(last_record_raspberry(1:length(last_record_raspberry)),'_'));
month = tmp_last_record_raspberry(2)-tmp_last_record(2);
day = tmp_last_record_raspberry(1)-tmp_last_record(1);
hour = tmp_last_record_raspberry(3)-tmp_last_record(3);
minutes = tmp_last_record_raspberry(4)-tmp_last_record(4);
earthquake_record='07_05_19_45'; %I want to fix/locate this time with the corresponding file
earthquake_record= str2double(split(earthquake_record(1:length(earthquake_record)),'_'));
earthquake_record_raspberry=earthquake_record;
earthquake_record_raspberry_day=earthquake_record_raspberry(1)+day;
earthquake_record_raspberry_month=earthquake_record_raspberry(2)+month;
earthquake_record_raspberry_hour=earthquake_record_raspberry(1)+hour;
earthquake_record_raspberry_minutes=earthquake_record_raspberry(2)+minutes;
earthquake_record_raspberry=[earthquake_record_raspberry_day;earthquake_record_raspberry_month;earthquake_record_raspberry_hour;earthquake_record_raspberry_minutes]
which corrects/locates the day and month of the raspberry with the real, but for minutes and seconds i have some problems e.g. 11:-19 instead of 10:41..
Can anyone help me?
1 个评论
采纳的回答
Steven Lord
2024-5-9
Rather than splitting the strings representing dates and times into vectors of numbers and trying to perform date and time arithmetic yourself, convert them into datetime arrays and let MATLAB do the arithmetic.
last_record='08_05_10_57'; % Correct time and date
last_record_raspberry='13_03_14_33'; % Not correct time and date
Based on your code this is arranged as day_month_hour_minute.
dt1 = datetime(last_record, 'InputFormat', 'dd_MM_HH_mm')
dt2 = datetime(last_record_raspberry, 'InputFormat', 'dd_MM_HH_mm')
What's the difference between the two dates?
difference = dt1-dt2
Let's change that format to days, hours, minutes, seconds.
difference.Format = 'dd:hh:mm:ss'
Or to take into account leap year, etc. take the calendar difference.
difference2 = caldiff([dt2, dt1])
If you have tabular data, you could use the datetime array as the RowTimes of a timetable array and use retime or synchronize on it.
2 个评论
Steven Lord
2024-5-9
I'm not 100% certain I understand what you're asking, but if you're asking how you can determine whether dt1 or dt2 was closer to dt3:
last_record='08_05_10_57'; % Correct time and date
last_record_raspberry='13_03_14_33'; % Not correct time and date
dt1 = datetime(last_record, 'InputFormat', 'dd_MM_HH_mm')
dt2 = datetime(last_record_raspberry, 'InputFormat', 'dd_MM_HH_mm')
earthquake_record='07_05_19_45';
dt3 = datetime(earthquake_record, 'InputFormat', 'dd_MM_HH_mm')
abs(dt3-dt1) < abs(dt3-dt2)
or
[~, location] = min(abs([dt1, dt2]-dt3))
Steven Lord
2024-5-9
The difference between two datetime arrays is a duration or a calendarDuration. You can add either of those to a datetime or subtract them from a datetime to get another datetime.
dt = datetime('now')
du = days(1)
cdu = caldays(1)
tomorrow1 = dt + du % datetime + duration = datetime
tomorrow2 = dt + cdu % datetime + calendarDuration = datetime
yesterday1 = dt - du % ditto for subtraction
yesterday2 = dt - cdu
更多回答(0 个)
This question is locked.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calendar 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!