フィルターのクリア

Locate and syncronize timestamp

3 ビュー (過去 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