Convert julian date SAC to specific format date
3 次查看(过去 30 天)
显示 更早的评论
I need convert: CM.SJC..HHE.D.2015.213.172556.SAC to specific format date, examples: 2015 7 26
Please
0 个评论
采纳的回答
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
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.
更多回答(1 个)
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
2021-3-23
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.')
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!