Adding number of seconds from changing base date

6 vues (au cours des 30 derniers jours)
Eric Beamesderfer
Eric Beamesderfer le 13 Août 2020
Commenté : Eric Beamesderfer le 13 Août 2020
This ones been giving me some trouble. I'm calling a file where the 'time' variable is the seconds since midnight of the day of interest which constantly changes:
In this test example, I am attempting to call four days of data at one time in a for loop. 's' is just the changing file name, but here when I attempt to take the 'time_raw' from the time variable for each file, it gives me the seconds since midnight of the current day, not the days of interest. I attempted to make some datetime values (t) of the days of interest and thought to concatenate them together (day of interest plus datetime of seconds since midnight) but I am unable to at this time. Feel like I'm missing one line of code to fix this, just not sure what that would be.
Currently, my 't2' variable is the correct number of seconds found in 'time' but for August 12, 2020 not April 16-19, 2010.

Réponse acceptée

Steven Lord
Steven Lord le 13 Août 2020
You can construct a datetime directly from the number of seconds. From the documentation page for datetime:
"t = datetime(X,'ConvertFrom',dateType) converts the numeric values in X to a datetime array t. The dateType argument specifies the type of values in X."
One of the dateType options is 'epochtime', "Number of seconds since an epoch." If you use this "You must also specify epochValue, which is a scalar datetime, or a character vector or string scalar representing the epoch time."
>> datetime(100, 'ConvertFrom', 'epochtime', 'Epoch', datetime(2010, 4, 16))
ans =
datetime
16-Apr-2010 00:01:40
  2 commentaires
Eric Beamesderfer
Eric Beamesderfer le 13 Août 2020
Thanks, Steven. This and the other answer are very close to working for me. However, I should have mentioned the number of seconds resets with each file/day. So on the next day, its back to close to zero. Currently, this solution restarts the number of seconds but doesn't update the correct day.
Eric Beamesderfer
Eric Beamesderfer le 13 Août 2020
Slightly modifying your answer to add a changing date worked. Thanks again!

Connectez-vous pour commenter.

Plus de réponses (1)

Mohammad Sami
Mohammad Sami le 13 Août 2020
Since the time is given to you as seconds since some date. you can read it as a double and then add it to the date of interests.
dateofinterest = datetime(2010,04,16);
% read the seconds
s = 1:100; % replace with your code to read the value.
out = dateofinterest + seconds(s);
  1 commentaire
Eric Beamesderfer
Eric Beamesderfer le 13 Août 2020
Thank you, Mohammad. This didn't work at first, as the time (number of seconds since midnight) restarts with each day/file. Initially, your solution created a time array where after midnight, the time restarted, but it was still for the first day (April 16th) not the second day (April 17th). I slightly modified your answer and then it worked.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by