Convert julian date SAC to specific format date
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Daphne Sagel Aguilar
le 11 Mar 2021
Commenté : Walter Roberson
le 23 Mar 2021
I need convert: CM.SJC..HHE.D.2015.213.172556.SAC to specific format date, examples: 2015 7 26
Please
0 commentaires
Réponse acceptée
Walter Roberson
le 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.
Plus de réponses (1)
Peter Perkins
le 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 commentaire
Walter Roberson
le 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.')
Voir également
Catégories
En savoir plus sur Dates and Time dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!