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.html,v 1.12 2008/03/31 10:27:40 hewitson 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.html,v 1.12 2008/03/31 10:27:40 hewitson Exp $
0024 %
0025 % HISTORY:     23-07-2007 Diepholz
0026 %                 Creation
0027 %
0028 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0029 
0030 %%% REMARK: This function don't use the generic set-function!!!
0031 
0032 VERSION       = '$Id: set.html,v 1.12 2008/03/31 10:27:40 hewitson Exp $';
0033 CATEGORY      = 'Helper';
0034 
0035 time_fields    = fieldnames(tt);
0036 property_argin = varargin;
0037 
0038 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0039 %%% Check if this is a special call: default parameter list, cvs-version, category
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 %%% Convert a plist into a key/value set
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     %% The 'key' name of a plist/param is always stored with upper case
0063     %% characters. To set a ao property with this key we have to lower the
0064     %% 'key' characters.
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     % Set the timeformat AND transform the current time string to this format
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     % Set the timezone AND compute the current time string.
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       % Set the right time format
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     % Set the time_str AND compute the utc_epoch_milli
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           %%% If the date does not match to the definitions
0153           %%% in parse_time_string then try to use 'real' matlab format
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       % Compare the existing timeformat with the 'new' timeformat
0163       % Is it not the same then use the 'new' timeformat
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       % 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 the unix epoche time AND compute the time string
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       % Set the right time format
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     % set all other time_fields
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   %        'match expression'   'MATLAB date format'
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

Generated on Mon 31-Mar-2008 12:20:24 by m2html © 2003