Convert julian date SAC to specific format date

조회 수: 3 (최근 30일)
Daphne Sagel Aguilar
Daphne Sagel Aguilar 2021년 3월 11일
댓글: Walter Roberson 2021년 3월 23일
I need convert: CM.SJC..HHE.D.2015.213.172556.SAC to specific format date, examples: 2015 7 26
Please

채택된 답변

Walter Roberson
Walter Roberson 2021년 3월 12일
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
Daphne Sagel Aguilar
Daphne Sagel Aguilar 2021년 3월 12일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Peter Perkins
Peter Perkins 2021년 3월 23일
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
Walter Roberson
Walter Roberson 2021년 3월 23일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by