Converting UTC time to seconds

30 views (last 30 days)
Yogang Singh
Yogang Singh on 19 Nov 2020
Commented: Steven Lord on 19 Nov 2020
I have a UTC Time stamp on my acquired data in format :
hh:mm:ss.SSSS
This data is taken at 20 ms and I need it to convert to duration in seconds and make a time series of it taking into account the start time and end time (even milli seconds taken into account)
I am currently using :
Dp = duration(hh,mm:mm,ss:ss);
tp= (minutes(D))*60;
Guidance required to make it into a time series which is generic in way that milli seconds are taken into account as well.

Accepted Answer

Steven Lord
Steven Lord on 19 Nov 2020
s = '12:34:56.789';
formatSpec = 'hh:mm:ss.SSS';
d = duration(s, 'InputFormat', formatSpec, 'Format', formatSpec)
d = duration
12:34:56.789
  2 Comments
Yogang Singh
Yogang Singh on 19 Nov 2020
Thanks !!
The table A comprises of my time data in UTC in the 'hh:mm:ss.SS' format and I have used following script to make the time data into time series in seconds:
Dpt = table2array(A);
formatSpec = 'hh:mm:ss.SS';
dp = duration(Dpt, 'InputFormat', formatSpec, 'Format', formatSpec);
Tp = hours(dp)*3600+(minutes(dp))*60 +seconds(dp);
Steven Lord
Steven Lord on 19 Nov 2020
Just call seconds.
s = '12:34:56.789';
formatSpec = 'hh:mm:ss.SSS';
d = duration(s, 'InputFormat', formatSpec, 'Format', formatSpec)
d = duration
12:34:56.789
format longg
dInSeconds = seconds(d)
dInSeconds =
45296.789
To check we can perform the calculations manually. To avoid the "magic numbers" 3600 and 60 I use the capability of MATLAB to convert between double and duration arrays.
secondsPerHour = seconds(hours(1)); % 3600
secondsPerMinute = seconds(minutes(1)); % 60
dInSeconds2 = secondsPerHour*12+secondsPerMinute*34+56.789
dInSeconds2 =
45296.789
Looks pretty good to me.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!