Info

This question is locked. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

Locate and syncronize timestamp

5 Ansichten (letzte 30 Tage)
Ancalagon8
Ancalagon8 am 9 Mai 2024
Locked: Rena Berman am 13 Jan. 2025
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 Kommentar
Rena Berman
Rena Berman am 13 Jan. 2025

(Answers Dev) Restored edit

Akzeptierte Antwort

Steven Lord
Steven Lord am 9 Mai 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')
dt1 = datetime
08-May-2024 10:57:00
dt2 = datetime(last_record_raspberry, 'InputFormat', 'dd_MM_HH_mm')
dt2 = datetime
13-Mar-2024 14:33:00
What's the difference between the two dates?
difference = dt1-dt2
difference = duration
1340:24:00
Let's change that format to days, hours, minutes, seconds.
difference.Format = 'dd:hh:mm:ss'
difference = duration
55:20:24:00
Or to take into account leap year, etc. take the calendar difference.
difference2 = caldiff([dt2, dt1])
difference2 = calendarDuration
1mo 24d 20h 24m 0s
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 Kommentare
Steven Lord
Steven Lord am 9 Mai 2024
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')
dt1 = datetime
08-May-2024 10:57:00
dt2 = datetime(last_record_raspberry, 'InputFormat', 'dd_MM_HH_mm')
dt2 = datetime
13-Mar-2024 14:33:00
earthquake_record='07_05_19_45';
dt3 = datetime(earthquake_record, 'InputFormat', 'dd_MM_HH_mm')
dt3 = datetime
07-May-2024 19:45:00
abs(dt3-dt1) < abs(dt3-dt2)
ans = logical
1
or
[~, location] = min(abs([dt1, dt2]-dt3))
location = 1
Steven Lord
Steven Lord am 9 Mai 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')
dt = datetime
09-May-2024 16:43:40
du = days(1)
du = duration
1 day
cdu = caldays(1)
cdu = calendarDuration
1d
tomorrow1 = dt + du % datetime + duration = datetime
tomorrow1 = datetime
10-May-2024 16:43:40
tomorrow2 = dt + cdu % datetime + calendarDuration = datetime
tomorrow2 = datetime
10-May-2024 16:43:40
yesterday1 = dt - du % ditto for subtraction
yesterday1 = datetime
08-May-2024 16:43:40
yesterday2 = dt - cdu
yesterday2 = datetime
08-May-2024 16:43:40

Weitere Antworten (0)

This question is locked.

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by