Locate and syncronize timestamp
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ancalagon8
el 9 de Mayo de 2024
Comentada: Steven Lord
el 9 de Mayo de 2024
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?
0 comentarios
Respuesta aceptada
Steven Lord
el 9 de Mayo de 2024
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.
4 comentarios
Steven Lord
el 9 de Mayo de 2024
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
Más respuestas (0)
Ver también
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!