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.9 2007/10/16 17:37:21 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 try
0130 d_num = datenum(val, tt.timeformat.format_str);
0131 new_time_str = datestr(d_num, tt.timeformat.default_matlab_str);
0132 catch
0133 error('### The timeformat ''%s'' does not match to the value ''%s''',...
0134 tt.timeformat.format_str, val);
0135 end
0136
0137 t_format = java.text.SimpleDateFormat(tt.timeformat.default_java_str);
0138 t_format.setTimeZone(tt.timezone);
0139 new_time = t_format.parse(new_time_str);
0140
0141 tt.utc_epoch_milli = new_time.getTime;
0142 tt.time_str = val;
0143
0144
0145 if ~isempty(tt.timeformat.format_str)
0146 tt = set(tt, 'timeformat', tt.timeformat.format_str);
0147 elseif tt.timeformat.format_nr >= 0
0148 tt = set(tt, 'timeformat', tt.timeformat.format_nr);
0149 else
0150 error('### No time format is set. Should not happen.');
0151 end
0152
0153
0154
0155
0156 elseif (strcmp(prop, 'utc_epoch_milli')) && (compute)
0157
0158 if ~isnumeric(val)
0159 error('### The utc epoch time must be a number');
0160 end
0161
0162 t_format = java.text.SimpleDateFormat(tt.timeformat.default_java_str);
0163 tt.time_str = char(t_format.format (val));
0164 tt.utc_epoch_milli = val;
0165
0166
0167 if ~isempty(tt.timeformat.format_str)
0168 tt = set(tt, 'timeformat', tt.timeformat.format_str);
0169 elseif tt.timeformat.format_nr >= 0
0170 tt = set(tt, 'timeformat', tt.timeformat.format_nr);
0171 else
0172 error('### No time format is set. Should not happen.');
0173 end
0174
0175
0176
0177
0178 else
0179 tt.(prop) = val;
0180 end
0181 end
0182
0183 end
0184
0185 else
0186 if ischar(varargin{2})
0187 error('### please use: %s = set(%s, ''%s'', ''%s'');', ...
0188 inputname(1), ...
0189 inputname(1), ...
0190 varargin{1}, ...
0191 varargin{2});
0192 elseif isnumeric(varargin{2})
0193 error('### please use: %s = set(%s, ''%s'', %d);', ...
0194 inputname(1), ...
0195 inputname(1), ...
0196 varargin{1}, ...
0197 varargin{2});
0198 else
0199 error('### please use: %s = set(%s, ''key'', ''value'');', ...
0200 inputname(1), ...
0201 inputname(1));
0202 end
0203 end