Convert julian date SAC to specific format date

6 views (last 30 days)
I need convert: CM.SJC..HHE.D.2015.213.172556.SAC to specific format date, examples: 2015 7 26
Please

Accepted Answer

Walter Roberson
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
output = datetime
2015 8 2 05:25:56
days(datetime(2015,7,26)-datetime(2015,1,1))
ans = 206
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.
  1 Comment
Daphne Sagel Aguilar
Daphne Sagel Aguilar on 12 Mar 2021
Thank you very much, you have given us a plus in our work. We were working with the length of the SAC file, and with this code we can divide it into parts, excellent.

Sign in to comment.

More Answers (1)

Peter Perkins
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
Walter Roberson on 23 Mar 2021
The regexp version is not so bad
t1 = 'CM.SJC..HHE.D.2015.213.172556.SAC'
t1 = 'CM.SJC..HHE.D.2015.213.172556.SAC'
t3 = regexp(t1, '\d[\d.]+', 'match')
t3 = 1×1 cell array
{'2015.213.172556.'}
d3 = datetime(t3, 'InputFormat', 'uuuu.DDD.HHmmss.')
d3 = datetime
01-Aug-2015 17:25:56

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by