필터 지우기
필터 지우기

Locate and syncronize timestamp

조회 수: 4 (최근 30일)
Ancalagon8
Ancalagon8 2024년 5월 9일
댓글: Steven Lord 2024년 5월 9일
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?

채택된 답변

Steven Lord
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')
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.
  댓글 수: 4
Ancalagon8
Ancalagon8 2024년 5월 9일
편집: Ancalagon8 2024년 5월 9일
No, please let me make it more clear:
With difference2 I can calculate the error between dt1 (real time) and dt2 (wrong time from rasberry).
Now when I have another date and time (dt3-real time) and knowing difference2, I need to calcuiate the corresponding time (dt4 - wrong time from rasberry). Maybe something like
dt4 = dt3- differnce2
will do the work?
Steven Lord
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')
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

제품

Community Treasure Hunt

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

Start Hunting!

Translated by