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
0031
0032 VERSION = '$Id: set.m,v 1.15 2008/02/25 18:08:49 ingo Exp $';
0033 CATEGORY = 'Helper';
0034
0035 time_fields = fieldnames(tt);
0036 property_argin = varargin;
0037
0038
0039
0040
0041 if ~isempty(varargin{1}) == 1 && ischar(varargin{1})
0042 in = varargin{1};
0043 if strcmp(in, 'Params')
0044 tt = GET_DEFAULT_PLIST;
0045 return
0046 elseif strcmp(in, 'Version')
0047 tt = VERSION;
0048 return
0049 elseif strcmp(in, 'Category')
0050 tt = CATEGORY;
0051 return
0052 end
0053 end
0054
0055
0056
0057
0058 if nargin >= 2 && isa(property_argin{1}, 'plist')
0059 pl = property_argin{1};
0060 property_argin = {};
0061 for ii = 1:nparams(pl)
0062
0063
0064
0065 property_argin{end+1} = lower(pl.params(ii).key);
0066 property_argin{end+1} = pl.params(ii).val;
0067 end
0068 end
0069
0070 if (nargout ~= 0)
0071
0072 compute = true;
0073 if nargin == 4 && strcmp(varargin{3}, 'direct')
0074 compute = false;
0075 end
0076
0077 while length(property_argin) >= 2
0078 prop = property_argin{1};
0079 val = property_argin{2};
0080 property_argin = property_argin(3:end);
0081
0082
0083
0084
0085 if (strcmp(prop, 'timeformat')) && (compute)
0086
0087 if isa(val, 'timeformat')
0088 tt.timeformat = val;
0089
0090 else
0091 t_format = java.text.SimpleDateFormat(tt.timeformat.default_java_str);
0092 t_format.setTimeZone(tt.timezone);
0093 current_time_str = char(t_format.format (tt.utc_epoch_milli));
0094
0095 d_num = datenum (current_time_str, tt.timeformat.default_matlab_str);
0096 tt.time_str = datestr(d_num, val);
0097
0098 if isnumeric(val)
0099 tt.timeformat = set(tt.timeformat, 'format_nr', val);
0100 else
0101 tt.timeformat = set(tt.timeformat, 'format_str', val);
0102 end
0103 end
0104
0105
0106
0107
0108 elseif (strcmp(prop, 'timezone')) && (compute)
0109
0110 available_IDs = cell(java.util.TimeZone.getAvailableIDs);
0111
0112 if strcmp (class(val), 'sun.util.calendar.ZoneInfo')
0113 tt.timezone = val;
0114 elseif ischar(val) && ismember(val, available_IDs)
0115 tt.timezone = java.util.TimeZone.getTimeZone(val);
0116 else
0117 error (sprintf(['### Please use a timezone ID\n'...
0118 'USE: get(time, ''timezone_IDs'') OR a java TimeZone']));
0119 end
0120
0121 t_format = java.text.SimpleDateFormat(tt.timeformat.default_java_str);
0122 t_format.setTimeZone(tt.timezone);
0123 tt.time_str = char(t_format.format(tt.utc_epoch_milli));
0124
0125
0126 if ~isempty(tt.timeformat.format_str)
0127 tt = set(tt, 'timeformat', tt.timeformat.format_str);
0128 elseif tt.timeformat.format_nr >= 0
0129 tt = set(tt, 'timeformat', tt.timeformat.format_nr);
0130 else
0131 error('### No time format is set. Should not happen.');
0132 end
0133
0134
0135
0136
0137 elseif (strcmp(prop, 'time_str')) && (compute)
0138
0139 if isempty(tt.timeformat.format_str)
0140 error (['### To set this property please set bevore a matlab format string.'...
0141 sprintf('\n') ...
0142 ' USE: tt = set(tt, ''timeformat'', ''yyyy-mm-dd HH:MM:SS'')']);
0143 end
0144
0145 parse_str = parse_time_string(val);
0146
0147 try
0148 d_num = datenum(val, parse_str);
0149 new_time_str = datestr(d_num, tt.timeformat.default_matlab_str);
0150 catch
0151 try
0152
0153
0154 d_num = datenum(val);
0155 new_time_str = datestr(d_num, tt.timeformat.default_matlab_str);
0156 catch
0157 error('### The timeformat ''%s'' does not match to the value ''%s''',...
0158 parse_str, val);
0159 end
0160 end
0161
0162
0163
0164 if strcmp (tt.timeformat.format_str, parse_str) == 0
0165 tt.timeformat = set(tt.timeformat, 'format_str', parse_str);
0166 end
0167
0168 t_format = java.text.SimpleDateFormat(tt.timeformat.default_java_str);
0169 t_format.setTimeZone(tt.timezone);
0170 new_time = t_format.parse(new_time_str);
0171
0172 tt.utc_epoch_milli = new_time.getTime;
0173 tt.time_str = 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 elseif (strcmp(prop, 'utc_epoch_milli')) && (compute)
0188
0189 if ~isnumeric(val)
0190 error('### The utc epoch time must be a number');
0191 end
0192
0193 t_format = java.text.SimpleDateFormat(tt.timeformat.default_java_str);
0194 tt.time_str = char(t_format.format (val));
0195 tt.utc_epoch_milli = val;
0196
0197
0198 if ~isempty(tt.timeformat.format_str)
0199 tt = set(tt, 'timeformat', tt.timeformat.format_str);
0200 elseif tt.timeformat.format_nr >= 0
0201 tt = set(tt, 'timeformat', tt.timeformat.format_nr);
0202 else
0203 error('### No time format is set. Should not happen.');
0204 end
0205
0206
0207
0208
0209 else
0210 try
0211 tt.(prop) = val;
0212 catch
0213 error(['### ''', prop, ''' is not a valid time property.']);
0214 end
0215 end
0216
0217 end
0218
0219 else
0220 display(tt);
0221 error('\n### No output variable!\n### Please use: %s = set(%s, ''key'', ''value'');', inputname(1), inputname(1));
0222 end
0223
0224 end
0225
0226 function ret = parse_time_string(in_str)
0227
0228 ret = in_str;
0229
0230
0231 parse = {'\d{2}:\d{2}:\d{2}', 'HH:MM:SS'; ...
0232 '\d{2}:\d{2}', 'MM:SS'; ...
0233 '\d{2}-\d{2}-\d{4}', 'dd-mm-yyyy'; ...
0234 '\d{2}\.\d{2}\.\d{4}', 'dd.mm.yyyy'; ...
0235 '\d{4}-\d{2}-\d{2}', 'yyyy-mm-dd'; ...
0236 '\d{4}\.\d{2}\.\d{2}', 'yyyy.mm.dd'; ...
0237 '\d{2}-\d{2}', 'mm-dd'; ...
0238 '\.\d{1,3}', '.FFF'};
0239
0240 for ii = 1:size(parse,1)
0241
0242 curr_parse = parse{ii,1};
0243 [match] = regexp(ret, curr_parse, 'match');
0244
0245 if ~isempty(match) && length(match) == 1
0246 replace = parse{ii,2};
0247 ret = strrep(ret, match{1}, replace);
0248 end
0249
0250 end
0251
0252 end
0253
0254 function default_plist = GET_DEFAULT_PLIST
0255 default_plist = plist('name', '', ...
0256 'utc_epoch_milli', [], ...
0257 'timezone', java.util.TimeZone.getTimeZone('UTC'), ...
0258 'timeformat', timeformat(), ...
0259 'time_str', '', ...
0260 'plist', plist(), ...
0261 'created', time(), ...
0262 'version', '');
0263 end
0264