0001 function tt = set(tt, varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030 VERSION = '$Id: set.m,v 1.11 2008/01/15 17:36:36 ingo Exp $';
0031
0032
0033 if nargin == 2
0034 if isa(tt, 'time') && ischar(varargin{1})
0035 in = char(varargin{1});
0036 if strcmp(in, 'Params')
0037 tt = plist();
0038 return
0039 elseif strcmp(in, 'Version')
0040 tt = VERSION;
0041 return
0042 end
0043 end
0044 end
0045
0046 if (nargout ~= 0)
0047
0048 compute = true;
0049 if nargin == 4 && strcmp(varargin{3}, 'direct')
0050 compute = false;
0051 end
0052
0053 fields = fieldnames(tt);
0054
0055 property_argin = varargin;
0056
0057 while length(property_argin) >= 2
0058 prop = property_argin{1};
0059 val = property_argin{2};
0060 property_argin = property_argin(3:end);
0061
0062 if ~ismember(prop, fields)
0063 error(['### ''', prop, ''' is not a valid time property.']);
0064 else
0065
0066
0067
0068 if (strcmp(prop, 'timeformat')) && (compute)
0069
0070 if isa(val, 'timeformat')
0071 tt.timeformat = val;
0072 tt = set(tt, 'timeformat', char(val));
0073
0074 else
0075 t_format = java.text.SimpleDateFormat(tt.timeformat.default_java_str);
0076 t_format.setTimeZone(tt.timezone);
0077 current_time_str = char(t_format.format (tt.utc_epoch_milli));
0078
0079 d_num = datenum (current_time_str, tt.timeformat.default_matlab_str);
0080 tt.time_str = datestr(d_num, val);
0081
0082 if isnumeric(val)
0083 tt.timeformat = set(tt.timeformat, 'format_nr', val);
0084 else
0085 tt.timeformat = set(tt.timeformat, 'format_str', val);
0086 end
0087 end
0088
0089
0090
0091
0092 elseif (strcmp(prop, 'timezone')) && (compute)
0093
0094 available_IDs = cell(java.util.TimeZone.getAvailableIDs);
0095
0096 if strcmp (class(val), 'sun.util.calendar.ZoneInfo')
0097 tt.timezone = val;
0098 elseif ischar(val) && ismember(val, available_IDs)
0099 tt.timezone = java.util.TimeZone.getTimeZone(val);
0100 else
0101 error (sprintf(['### Please use a timezone ID\n'...
0102 'USE: get(time, ''timezone_IDs'') OR a java TimeZone']));
0103 end
0104
0105 t_format = java.text.SimpleDateFormat(tt.timeformat.default_java_str);
0106 t_format.setTimeZone(tt.timezone);
0107 tt.time_str = char(t_format.format(tt.utc_epoch_milli));
0108
0109
0110 if ~isempty(tt.timeformat.format_str)
0111 tt = set(tt, 'timeformat', tt.timeformat.format_str);
0112 elseif tt.timeformat.format_nr >= 0
0113 tt = set(tt, 'timeformat', tt.timeformat.format_nr);
0114 else
0115 error('### No time format is set. Should not happen.');
0116 end
0117
0118
0119
0120
0121 elseif (strcmp(prop, 'time_str')) && (compute)
0122
0123 if isempty(tt.timeformat.format_str)
0124 error (['### To set this property please set bevore a matlab format string.'...
0125 sprintf('\n') ...
0126 ' USE: tt = set(tt, ''timeformat'', ''yyyy-mm-dd HH:MM:SS'')']);
0127 end
0128
0129 parse_str = parse_time_string(val);
0130
0131 try
0132 d_num = datenum(val, parse_str);
0133
0134 new_time_str = datestr(d_num, tt.timeformat.default_matlab_str);
0135 catch
0136 error('### The timeformat ''%s'' does not match to the value ''%s''',...
0137 parse_str, val);
0138 end
0139
0140
0141
0142 if strcmp (tt.timeformat.format_str, parse_str) == 0
0143 tt.timeformat = set(tt.timeformat, 'format_str', parse_str);
0144 end
0145
0146 t_format = java.text.SimpleDateFormat(tt.timeformat.default_java_str);
0147 t_format.setTimeZone(tt.timezone);
0148 new_time = t_format.parse(new_time_str);
0149
0150 tt.utc_epoch_milli = new_time.getTime;
0151 tt.time_str = val;
0152
0153
0154 if ~isempty(tt.timeformat.format_str)
0155 tt = set(tt, 'timeformat', tt.timeformat.format_str);
0156 elseif tt.timeformat.format_nr >= 0
0157 tt = set(tt, 'timeformat', tt.timeformat.format_nr);
0158 else
0159 error('### No time format is set. Should not happen.');
0160 end
0161
0162
0163
0164
0165 elseif (strcmp(prop, 'utc_epoch_milli')) && (compute)
0166
0167 if ~isnumeric(val)
0168 error('### The utc epoch time must be a number');
0169 end
0170
0171 t_format = java.text.SimpleDateFormat(tt.timeformat.default_java_str);
0172 tt.time_str = char(t_format.format (val));
0173 tt.utc_epoch_milli = val;
0174
0175
0176 if ~isempty(tt.timeformat.format_str)
0177 tt = set(tt, 'timeformat', tt.timeformat.format_str);
0178 elseif tt.timeformat.format_nr >= 0
0179 tt = set(tt, 'timeformat', tt.timeformat.format_nr);
0180 else
0181 error('### No time format is set. Should not happen.');
0182 end
0183
0184
0185
0186
0187 else
0188 tt.(prop) = val;
0189 end
0190 end
0191
0192 end
0193
0194 else
0195 if ischar(varargin{2})
0196 error('### please use: %s = set(%s, ''%s'', ''%s'');', ...
0197 inputname(1), ...
0198 inputname(1), ...
0199 varargin{1}, ...
0200 varargin{2});
0201 elseif isnumeric(varargin{2})
0202 error('### please use: %s = set(%s, ''%s'', %d);', ...
0203 inputname(1), ...
0204 inputname(1), ...
0205 varargin{1}, ...
0206 varargin{2});
0207 else
0208 error('### please use: %s = set(%s, ''key'', ''value'');', ...
0209 inputname(1), ...
0210 inputname(1));
0211 end
0212 end
0213
0214 end
0215
0216 function ret = parse_time_string(in_str)
0217
0218 ret = in_str;
0219
0220
0221 parse = {'\d{2}:\d{2}:\d{2}', 'HH:MM:SS'; ...
0222 '\d{2}:\d{2}' 'MM:SS'; ...
0223 '\d{2}-\d{2}-\d{4}', 'dd-mm-yyyy'; ...
0224 '\d{4}-\d{2}-\d{2}', 'yyyy-mm-dd'; ...
0225 '\d{2}-\d{2}' , 'mm-dd'; ...
0226 '\.\d{1,3}', '.FFF'};
0227
0228 for ii = 1:size(parse,1)
0229
0230 curr_parse = parse{ii,1};
0231 [match] = regexp(ret, curr_parse, 'match');
0232
0233 if ~isempty(match) && length(match) == 1
0234 replace = parse{ii,2};
0235 ret = strrep(ret, match{1}, replace);
0236 end
0237
0238 end
0239
0240 end
0241
0242
0243
0244
0245