Home > classes > @time > set.m

set

PURPOSE ^

SET set time properties.

SYNOPSIS ^

function tt = set(tt, varargin)

DESCRIPTION ^

 SET set time properties.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 DESCRIPTION: SET set time properties.

 CALL:        tt = set(tt, 'property', 'value');
              tt = set(tt, 'property', 'value', 'direct');

 EXAMPLES:    tt = set(tt, 'name',            'my name');
              tt = set(tt, 'utc_epoch_milli',  1185468272023);
              tt = set(tt, 'timezone',        'UTC');
              tt = set(tt, 'timeformat',      'yyyy-mm-dd HH:MM:SS');
              tt = set(tt, 'timeformat',       31);
              tt = set(tt, 'timeformat',       timeformat);
              tt = set(tt, 'time_str',        '2007-07-26 18:44:32');
              tt = set(tt, 'created',         '2007-07-26 18:44:32');
              tt = set(tt, 'version',         'cvs version string');

 OPTION:      'direct' Sets the value direct without computing the new format

 VERSION:     $Id: set.m,v 1.9 2007/10/16 17:37:21 ingo Exp $

 HISTORY:     23-07-2007 Diepholz
                 Creation

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function tt = set(tt, varargin)
0002 % SET set time properties.
0003 %
0004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0005 %
0006 % DESCRIPTION: SET set time properties.
0007 %
0008 % CALL:        tt = set(tt, 'property', 'value');
0009 %              tt = set(tt, 'property', 'value', 'direct');
0010 %
0011 % EXAMPLES:    tt = set(tt, 'name',            'my name');
0012 %              tt = set(tt, 'utc_epoch_milli',  1185468272023);
0013 %              tt = set(tt, 'timezone',        'UTC');
0014 %              tt = set(tt, 'timeformat',      'yyyy-mm-dd HH:MM:SS');
0015 %              tt = set(tt, 'timeformat',       31);
0016 %              tt = set(tt, 'timeformat',       timeformat);
0017 %              tt = set(tt, 'time_str',        '2007-07-26 18:44:32');
0018 %              tt = set(tt, 'created',         '2007-07-26 18:44:32');
0019 %              tt = set(tt, 'version',         'cvs version string');
0020 %
0021 % OPTION:      'direct' Sets the value direct without computing the new format
0022 %
0023 % VERSION:     $Id: set.m,v 1.9 2007/10/16 17:37:21 ingo Exp $
0024 %
0025 % HISTORY:     23-07-2007 Diepholz
0026 %                 Creation
0027 %
0028 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0029 
0030 VERSION = '$Id: set.m,v 1.9 2007/10/16 17:37:21 ingo Exp $';
0031 
0032 % Check if this is a call for parameters or for the cvs-version number
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       % Set the timeformat AND transform the current time string to this format
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       % Set the timezone AND compute the current time string.
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         % Set the right time format
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       % Set the time_str AND compute the utc_epoch_milli
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         % Set the right time format
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       % Set the unix epoche time AND compute the time string
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         % Set the right time format
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       % set all other fields
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

Generated on Thu 01-Nov-2007 09:42:34 by m2html © 2003