File size: 6,759 Bytes
5ab75a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
function timeFormat = getCcsdsTimeFormat(timeString)
% getCcsdsTimeFormat - Determines format of CCSDS time string
%
% Syntax: getCcsdsTimeFormat(timeString);
%
% =========================================================================
%
% Copyright (c) 2023 United States Government as represented by the
% Administrator of the National Aeronautics and Space Administration.
% All Rights Reserved.
%
% =========================================================================
%
% This software developed under RIGHTS IN DATA - special works
% (FAR 52.227-17) as modified by NFS 1852.227-17.
%
% =========================================================================
%
% Description:
%
%   Given a date string in one of several formats, this function determines
%   which format is currently in use and returns it as a character string
%   showing the positioning of various time units (i.e.,
%   yyyy-DDDTHH:MM:SS).
%
%    The CCSDS time format is required to be of the general form
%
%        yyyy-[mm-dd|ddd]THH:MM:SS[.F*][Z]
%
%    (1) The date and time fields are separated by a "T".
%    (2) The date field has a four digit year followed by either a two 
%        digit month and two digit day, or a three digit day-of-year.  
%    (3) The year, month, day, and day-of-year fields are separated by a
%       dash.
%    (4) The hours, minutes and seconds fields are each two digits 
%        separated by colons.
%    (5) The fraction of seconds is optional and can have any number of
%        digits.
%    (6) If a fraction of seconds is provided, it is separated from the two
%        digit seconds by a period.
%    (7) The time string can end with an optional "Z" time zone indicator
%
% =========================================================================
%
% Input:
%
%   timeString  -   CCSDS time string to be parsed
%
% =========================================================================
%
% Output:
%
%   timeFormat  -   Time format string defining the format of the input
%                   CCSDS time string 
%
% =========================================================================
%
% Initial version: Jun 2016;  Latest update: Jun 2023
%
% ----------------- BEGIN CODE -----------------

% Initialize output
timeFormat = [];

% Find location of the "T" date and time field separator
idxT = strfind(timeString,'T');
if numel(idxT) == 0
   fprintf('*** Error -- Invalid CCSDS time string: %s (no calendar-time separator) \n', timeString);
   warning('getCcsdsTimeFormat:NoTSeparatorFound', 'No "T" separator found between date and time portions of the string');
   return
elseif numel(idxT) > 1
   fprintf('*** Error -- Invalid CCSDS time string: %s (multiple calendar-time separators) \n', timeString);
   warning('getCcsdsTimeFormat:MoreThanOneTSeparatorFound', 'More than one "T" separator found between date and time portions of the string');
   return
end

% Check that the first four characters are numbers
% This prevents formats such as mm/dd/yy from being read as yyyy-DDD
if isempty(regexp(timeString, '\<\d{4}', 'once'))
    fprintf('*** Error -- Invalid CCSDS time string: %s (invalid year) \n', timeString);
    warning('getCcsdsTimeFormat:InvalidDateFormat', 'Date format not one of yyyy-mm-dd or yyyy-DDD');
    return
end

if idxT == 11
    if ~(all(isstrprop(timeString(6:7), 'digit')) && all(isstrprop(timeString(9:10), 'digit')))
        fprintf('*** Error -- Invalid CCSDS time string: %s (invalid day or month) \n', timeString);
        warning('getCcsdsTimeFormat:InvalidDateFormat', 'Date format not one of yyyy-mm-dd or yyyy-DDD');
        return
    end
    timeFormat = 'yyyy-mm-ddTHH:MM:SS';
elseif idxT == 9
    if ~all(isstrprop(timeString(6:8), 'digit'))
        fprintf('*** Error -- Invalid CCSDS time string: %s (invalid day of year) \n', timeString);
        warning('getCcsdsTimeFormat:InvalidDateFormat', 'Date format not one of yyyy-mm-dd or yyyy-DDD');
        return
    end
    timeFormat = 'yyyy-DDDTHH:MM:SS';
else
   fprintf('*** Error -- Invalid CCSDS time string: %s (incorrect calendar-time separator) \n', timeString);
   warning('getCcsdsTimeFormat:InvalidDateFormat', 'Date format not one of yyyy-mm-dd or yyyy-DDD');
   return
end

if isempty(regexp(timeString(idxT + 1:end), '\d{2}(.\d{2}){2}', 'once'))
   fprintf('*** Error -- Invalid CCSDS time string: %s (invalid hour, minute, or second) \n', timeString);
   warning('getCcsdsTimeFormat:InvalidDateFormat', 'Time format not HH:MM:SS(.F...F)');
   return
end

% Check if 'Z' time zone indicator appended to the string
if strcmp(timeString(end),'Z')
   zOpt = true;
else
   zOpt = false;
end

% Find location of the fraction of seconds decimal separator
idxDecimal = strfind(timeString,'.');
if numel(idxDecimal) > 1
   fprintf('*** Error -- Invalid CCSDS time string: %s (multiple fraction separators) \n', timeString);
   warning('getCcsdsTimeFormat:MoreThanOneFractionSeparatorFound', 'More than one fraction of seconds decimal separator (".") found.');
   timeFormat = [];
   return
end

if ~all(isstrprop(timeString(idxT + 10:end - zOpt), 'digit'))
   fprintf('*** Error -- Invalid CCSDS time string: %s (invalid fractional second) \n', timeString);
   warning('getCcsdsTimeFormat:InvalidDateFormat', 'Time format not HH:MM:SS(.F...F)');
   return
end

nfrac = 0;
if ~isempty(idxDecimal)
   if zOpt
      nfrac = numel(timeString) - idxDecimal - 1;
   else
      nfrac = numel(timeString) - idxDecimal;
   end
end

if nfrac > 0
   fracStr = ['.'  repmat('F',1,nfrac)];
else
   fracStr = '';
end

if zOpt
   fracStr = [fracStr 'Z'];
end
timeFormat = [timeFormat fracStr];

% ----------------- END OF CODE -----------------
%
% Please record any changes to the software in the change history 
% shown below:
%
% ----------------- CHANGE HISTORY ------------------
% Developer      |    Date     |     Description
% ---------------------------------------------------
% R. Coon        | 2016-Jun-14 | Created
% E. White       | 2023-Jun-07 | Fixed mm/dd/yy reading as yyyy-DDD and
%                                added checks and warnings, added updated 
%                                header and footer

% =========================================================================
%
% Copyright (c) 2023 United States Government as represented by the
% Administrator of the National Aeronautics and Space Administration.
% All Rights Reserved.
%
% =========================================================================
%
% This software developed under RIGHTS IN DATA - special works
% (FAR 52.227-17) as modified by NFS 1852.227-17.