Convert julian date SAC to specific format date
3 views (last 30 days)
Show older comments
Daphne Sagel Aguilar
on 11 Mar 2021
Commented: Walter Roberson
on 23 Mar 2021
I need convert: CM.SJC..HHE.D.2015.213.172556.SAC to specific format date, examples: 2015 7 26
Please
0 Comments
Accepted Answer
Walter Roberson
on 12 Mar 2021
S = 'CM.SJC..HHE.D.2015.213.172556.SAC';
parts = regexp(S,'\.','split');
year = str2double(parts{6});
doy = str2double(parts{7});
hour = str2double(parts{8}(1:2));
minute = str2double(parts{8}(3:4));
second = str2double(parts{8}(5:6));
output = datetime(year, 1, 1, hour, minute, second, 'Format', 'yyyy M d hh:mm:ss', 'timezone','utc') + days(doy);
output
days(datetime(2015,7,26)-datetime(2015,1,1))
Could you confirm that you really do have Julian days? Julian Days are number of days since a particular date around 4700 BCE. As you can see, there is a difference of exactly one week between your intended output and a calculation based upon day of the year.
More Answers (1)
Peter Perkins
on 23 Mar 2021
I'm gonna suggest another solution that might be simpler to follow.
First thing is that this isn't a Julian Date in the strictly correct sense of the word. In many fields though, "Julian day/date" means "day of year". That's what you have.
datetime text conversion will handle literals in a timestamp (the double-quoted format contains single-quote-escaped literals), and will handle day of year ("D", not "d"). So this
>> t1 = 'CM.SJC..HHE.D.2015.213.172556.SAC'
t1 =
'CM.SJC..HHE.D.2015.213.172556.SAC'
>> d1 = datetime(t1,'InputFormat',"'CM.SJC..HHE.D.'uuuu.DDD.HHmmss'.SAC'")
would be the way to go. Unfortunately, there's a fairly recent (and VERY narrowly-scoped) bug in parsing timestamps that causes that to error (thanks, Walter, for reporting it). That will be fixed, but for now, there are two work-arounds. One is to lower-case everything:
>> t2 = lower(t1)
t2 =
'cm.sjc..hhe.d.2015.213.172556.sac'
d2 = datetime(t2,'InputFormat',"'cm.sjc..hhe.d.'uuuu.DDD.HHmmss'.sac'")
d2 =
datetime
01-Aug-2015 17:25:56
The other is to peel off the literals. In versions since R2016b it's easiest to use some string functions rather than regexp:
>> t3 = extractAfter(extractBefore(t1,".SAC"),"CM.SJC..HHE.D.")
t3 =
'2015.213.172556'
d3 = datetime(t3,'InputFormat',"uuuu.DDD.HHmmss")
d3 =
datetime
01-Aug-2015 17:25:56
The above shows scalars, but of course it's completely vectorized.
1 Comment
Walter Roberson
on 23 Mar 2021
The regexp version is not so bad
t1 = 'CM.SJC..HHE.D.2015.213.172556.SAC'
t3 = regexp(t1, '\d[\d.]+', 'match')
d3 = datetime(t3, 'InputFormat', 'uuuu.DDD.HHmmss.')
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!