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.11 2008/01/15 17:36:36 ingo Exp $

 HISTORY:     23-07-2007 Diepholz
                 Creation

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

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.11 2008/01/15 17:36:36 ingo Exp $
0024 %
0025 % HISTORY:     23-07-2007 Diepholz
0026 %                 Creation
0027 %
0028 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0029 
0030 VERSION = '$Id: set.m,v 1.11 2008/01/15 17:36:36 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         parse_str = parse_time_string(val);
0130 
0131         try
0132           d_num = datenum(val, parse_str);
0133 %           d_num = datenum(val, tt.timeformat.format_str);
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         % Compare the existing timeformat with the 'new' timeformat
0141         % Is it not the same then use the 'new' timeformat
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         % Set the right time format
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       % Set the unix epoche time AND compute the time string
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         % Set the right time format
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       % set all other fields
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   %        'match expression'   'MATLAB date format'
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

Generated on Tue 22-Jan-2008 10:39:13 by m2html © 2003