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 if (nargout ~= 0)
0028
0029 compute = true;
0030 if nargin == 4 && strcmp(varargin{3}, 'direct')
0031 compute = false;
0032 end
0033
0034 fields = fieldnames(tt);
0035
0036 property_argin = varargin;
0037
0038 while length(property_argin) >= 2
0039 prop = property_argin{1};
0040 val = property_argin{2};
0041 property_argin = property_argin(3:end);
0042
0043 if ~ismember(prop, fields)
0044 error(['### ''', prop, ''' is not a valid time property.']);
0045 else
0046
0047
0048
0049 if (strcmp(prop, 'timeformat')) && (compute)
0050
0051 if isa(val, 'timeformat')
0052 tt.timeformat = val;
0053 tt = set(tt, 'timeformat', char(val));
0054
0055 else
0056 t_format = java.text.SimpleDateFormat(tt.timeformat.default_java_str);
0057 t_format.setTimeZone(tt.timezone);
0058 current_time_str = char(t_format.format (tt.utc_epoch_milli));
0059
0060 d_num = datenum (current_time_str, tt.timeformat.default_matlab_str);
0061 tt.time_str = datestr(d_num, val);
0062
0063 if isnumeric(val)
0064 tt.timeformat = set(tt.timeformat, 'format_nr', val);
0065 else
0066 tt.timeformat = set(tt.timeformat, 'format_str', val);
0067 end
0068 end
0069
0070
0071
0072
0073 elseif (strcmp(prop, 'timezone')) && (compute)
0074
0075 available_IDs = cell(java.util.TimeZone.getAvailableIDs);
0076
0077 if strcmp (class(val), 'sun.util.calendar.ZoneInfo')
0078 tt.timezone = val;
0079 elseif ischar(val) && ismember(val, available_IDs)
0080 tt.timezone = java.util.TimeZone.getTimeZone(val);
0081 else
0082 error (sprintf(['### Please use a timezone ID\n'...
0083 'USE: get(time, ''timezone_IDs'') OR a java TimeZone']));
0084 end
0085
0086 t_format = java.text.SimpleDateFormat(tt.timeformat.default_java_str);
0087 t_format.setTimeZone(tt.timezone);
0088 tt.time_str = char(t_format.format(tt.utc_epoch_milli));
0089
0090
0091 if ~isempty(tt.timeformat.format_str)
0092 tt = set(tt, 'timeformat', tt.timeformat.format_str);
0093 elseif tt.timeformat.format_nr >= 0
0094 tt = set(tt, 'timeformat', tt.timeformat.format_nr);
0095 else
0096 error('### No time format is set. Should not happen.');
0097 end
0098
0099
0100
0101
0102 elseif (strcmp(prop, 'time_str')) && (compute)
0103
0104 if isempty(tt.timeformat.format_str)
0105 error (['### To set this property please set bevore a matlab format string.'...
0106 sprintf('\n') ...
0107 ' USE: tt = set(tt, ''timeformat'', ''yyyy-mm-dd HH:MM:SS'')']);
0108 end
0109
0110 try
0111 d_num = datenum(val, tt.timeformat.format_str);
0112 new_time_str = datestr(d_num, tt.timeformat.default_matlab_str);
0113 catch
0114 error('### The timeformat ''%s'' does not match to the value ''%s''',...
0115 tt.timeformat.format_str, val);
0116 end
0117
0118 t_format = java.text.SimpleDateFormat(tt.timeformat.default_java_str);
0119 t_format.setTimeZone(tt.timezone);
0120 new_time = t_format.parse(new_time_str);
0121
0122 tt.utc_epoch_milli = new_time.getTime;
0123 tt.time_str = val;
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, 'utc_epoch_milli')) && (compute)
0138
0139 if ~isnumeric(val)
0140 error('### The utc epoch time must be a number');
0141 end
0142
0143 t_format = java.text.SimpleDateFormat(tt.timeformat.default_java_str);
0144 tt.time_str = char(t_format.format (val));
0145 tt.utc_epoch_milli = val;
0146
0147
0148 if ~isempty(tt.timeformat.format_str)
0149 tt = set(tt, 'timeformat', tt.timeformat.format_str);
0150 elseif tt.timeformat.format_nr >= 0
0151 tt = set(tt, 'timeformat', tt.timeformat.format_nr);
0152 else
0153 error('### No time format is set. Should not happen.');
0154 end
0155
0156
0157
0158
0159 else
0160 tt.(prop) = val;
0161 end
0162 end
0163
0164 end
0165
0166 else
0167 if ischar(varargin{2})
0168 error('### please use: %s = set(%s, ''%s'', ''%s'');', ...
0169 inputname(1), ...
0170 inputname(1), ...
0171 varargin{1}, ...
0172 varargin{2});
0173 elseif isnumeric(varargin{2})
0174 error('### please use: %s = set(%s, ''%s'', %d);', ...
0175 inputname(1), ...
0176 inputname(1), ...
0177 varargin{1}, ...
0178 varargin{2});
0179 else
0180 error('### please use: %s = set(%s, ''key'', ''value'');', ...
0181 inputname(1), ...
0182 inputname(1));
0183 end
0184 end