Home > classes > @time > time.m

time

PURPOSE ^

TIME time object class constructor.

SYNOPSIS ^

function varargout = time(varargin)

DESCRIPTION ^

 TIME time object class constructor.

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

 DESCRIPTION:  TIME time object class constructor.
               Create a time object.

 PROPERTIES:   utc_epoch_milli  - Unix epoch time of the utc time.
               timezone         - Timezone of the date/time
               timeformat       - Matlab timeformat of the date/time
               time_str         - Time string of the unix time in the specified
                                  timezone.

 CONSTRUCTORS: t1 = time()
               t1 = time('t0_default')
               t1 = time('2007-06-06 14:00:05')
               t1 = time(1186135200000)
               t1 = time('14:00:00', 'HH:MM:SS')
               t1 = time('14:00:00',  31)               31 --> MATLAB format Nr.
               t1 = time('T1: 14:00:00', 'T1: HH:MM:SS')
               t1 = time(plist)

 PLIST KEYS:   mandatory: 'utc_epoch_milli' : Time in milliseconds
                   or     'time_str'        : Time string
               optional:  'timezone'        : Timezone (string or java object)
                          'timeformat'      : Time format (string)

 VERSION:      $Id: time.m,v 1.22 2008/02/24 10:15:56 hewitson Exp $

 HISTORY:      23-07-2007 Diepholz
                  Creation

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function varargout = time(varargin)
0002 % TIME time object class constructor.
0003 %
0004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0005 %
0006 % DESCRIPTION:  TIME time object class constructor.
0007 %               Create a time object.
0008 %
0009 % PROPERTIES:   utc_epoch_milli  - Unix epoch time of the utc time.
0010 %               timezone         - Timezone of the date/time
0011 %               timeformat       - Matlab timeformat of the date/time
0012 %               time_str         - Time string of the unix time in the specified
0013 %                                  timezone.
0014 %
0015 % CONSTRUCTORS: t1 = time()
0016 %               t1 = time('t0_default')
0017 %               t1 = time('2007-06-06 14:00:05')
0018 %               t1 = time(1186135200000)
0019 %               t1 = time('14:00:00', 'HH:MM:SS')
0020 %               t1 = time('14:00:00',  31)               31 --> MATLAB format Nr.
0021 %               t1 = time('T1: 14:00:00', 'T1: HH:MM:SS')
0022 %               t1 = time(plist)
0023 %
0024 % PLIST KEYS:   mandatory: 'utc_epoch_milli' : Time in milliseconds
0025 %                   or     'time_str'        : Time string
0026 %               optional:  'timezone'        : Timezone (string or java object)
0027 %                          'timeformat'      : Time format (string)
0028 %
0029 % VERSION:      $Id: time.m,v 1.22 2008/02/24 10:15:56 hewitson Exp $
0030 %
0031 % HISTORY:      23-07-2007 Diepholz
0032 %                  Creation
0033 %
0034 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0035 
0036 ALGONAME = mfilename;
0037 VERSION  = '$Id: time.m,v 1.22 2008/02/24 10:15:56 hewitson Exp $';
0038 CATEGORY = 'Constructor';
0039 
0040 % Check if this is a call for parameters
0041 if (nargin == 2 || nargin == 3)
0042   if isa(varargin{1}, 'time') && ischar(varargin{2})
0043     in = char(varargin{2});
0044     if strcmp(in, 'Params')
0045       if nargin == 2
0046         varargout{1} = getDefaultPlist();
0047       else
0048         varargout{1} = getDefaultPlist(varargin{3});
0049       end
0050       return
0051     elseif strcmp(in, 'Version')
0052       varargout{1} = VERSION;
0053       return
0054     elseif strcmp(in, 'Category')
0055       varargout{1} = CATEGORY;
0056       return
0057     end
0058   end
0059 end
0060 
0061 import java.util.GregorianCalendar;
0062 import java.util.TimeZone;
0063 
0064 %%%%%%%%%%%%%%%%%%%%%%%%%%%   Get default values   %%%%%%%%%%%%%%%%%%%%%%%%%%%%
0065 
0066 setup_timezone   = getappdata(0, 'timezone');
0067 setup_format_str = getappdata(0, 'time_format_str');
0068 
0069 % REMARK: The default properties must match together.
0070 %         They must represent the same time format.
0071 default_matlab_str = 'yyyy-mm-dd HH:MM:SS.FFF';
0072 default_matlab_nr  = -1;
0073 default_java_str   = 'yyyy-MM-dd HH:mm:ss.SSS';
0074 
0075 %%%%%%%%%%%%%%%%%%%%%%%%%%%%   Get current time   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0076 
0077 % Define local/utc timezone
0078 timezone = TimeZone.getTimeZone(setup_timezone);
0079 
0080 % Define a utc calendar
0081 cal_utc    = GregorianCalendar(timezone);
0082 
0083 % Get the unix time (timezone = UTC) in milliseconds
0084 utc_epoch_milli = cal_utc.getTimeInMillis;
0085 
0086 %%%%%%%%%%%%%%%%%%%%%%%%%   define time properties   %%%%%%%%%%%%%%%%%%%%%%%%%%
0087 
0088   function tt = init
0089     tt.name            = 'time-object';
0090     tt.utc_epoch_milli = utc_epoch_milli;
0091 
0092     tt.timezone        = timezone;
0093 
0094     tt.timeformat      = timeformat(setup_format_str,   ...
0095                                     default_matlab_str, ...
0096                                     default_matlab_nr,  ...
0097                                     default_java_str);
0098 
0099     t_format = java.text.SimpleDateFormat(default_java_str);
0100     t_format.setTimeZone(tt.timezone);
0101 
0102     tt.time_str = char(t_format.format (tt.utc_epoch_milli));
0103     tt.created  = tt.time_str;
0104     tt.version  = VERSION;
0105     tt.plist    = '';
0106     tt = class(tt, 'time');
0107 
0108     % Set the ltpda_startup time format
0109     tt = set(tt, 'timeformat', tt.timeformat.format_str);
0110   end
0111 
0112 %%%%%%%%%%%%%%%%%%%%%%%%%%%   Create time object   %%%%%%%%%%%%%%%%%%%%%%%%%%%%
0113 
0114 %%%%%%%%%%  t1 = time()   %%%%%%%%%%
0115 if nargin == 0
0116 
0117   tt = init();
0118 
0119 elseif nargin == 1
0120 
0121   %%%%%%%%%%   t1 = time(time-object)   %%%%%%%%%%
0122   if isa(varargin{1}, 'time')
0123 
0124     tt = varargin{1};
0125 
0126   %%%%%%%%%%   t1 = time('t0_default')   %%%%%%%%%%
0127   elseif strcmp(varargin{1}, 't0_default')
0128 
0129     tt = init();
0130     tt = set(tt, 'utc_epoch_milli', 0);
0131 
0132   %%%%%%%%%%   t1 = time('2007-08-03 10:00:00')   %%%%%%%%%%
0133   %%%%%%%%%%   t1 = time('foo.mat')               %%%%%%%%%%
0134   %%%%%%%%%%   t1 = time('foo.xml')               %%%%%%%%%%
0135   elseif ischar(varargin{1})
0136 
0137     % is this a filename?
0138     [path, name, ext, vers] = fileparts(varargin{1});
0139     if ~isempty(ext) && length(ext) == 4 && isempty(str2num(ext(2)))
0140 
0141       switch ext
0142         case '.mat'
0143           tt = load(varargin{1});
0144           tt = tt.a;
0145         case '.xml'
0146           root_node = xmlread(varargin{1});
0147           tt = ltpda_xmlread(root_node, 'time');
0148         otherwise
0149           error('### Unknown file type.');
0150       end
0151 
0152     else
0153       tt = init();
0154       tt = set(tt, 'time_str', varargin{1});
0155     end
0156 
0157   %%%%%%%%%%   t1 = time(1186135200000)   %%%%%%%%%%
0158   elseif isnumeric(varargin{1})
0159 
0160     tt = init();
0161     tt = set(tt, 'utc_epoch_milli', varargin{1});
0162 
0163   %%%%%%%%%%   t1 = time(struct)   %%%%%%%%%%
0164   elseif isstruct(varargin{1})
0165 
0166     tt = init();
0167 
0168     fields = fieldnames(varargin{1});
0169     for ii = 1:length(fields)
0170       field = fields{ii};
0171       %%% timeformat -> timeformat-object
0172       if strcmp(field, 'timeformat')
0173         tf = varargin{1}.timeformat;
0174         if isstruct(tf)
0175           tf = timeformat(tf);
0176         end
0177         tt.timeformat = tf;
0178       %% All other
0179       else
0180         try
0181           tt.(field) = varargin{1}.(field);
0182         catch
0183           error('### The field ''%s'' in the struct is not a time property.', field)
0184         end
0185       end
0186     end
0187 
0188   %%%%%%%%%%   t1 = time(plist)   %%%%%%%%%%
0189   elseif isa(varargin{1}, 'plist')
0190 
0191     pl_timezone    = find(varargin{1}, 'timezone');
0192     pl_epoch_milli = find(varargin{1}, 'utc_epoch_milli');
0193     pl_time_str    = find(varargin{1}, 'time_str');
0194     pl_timeformat  = find(varargin{1}, 'timeformat');
0195     hostname       = find(varargin{1}, 'hostname');
0196 
0197     tt = init();
0198 
0199     if ~isempty(hostname)
0200       tt = timeFromRepository(varargin{1}, VERSION, ALGONAME);
0201     else
0202       
0203       if ~isempty(pl_timeformat)
0204         tt = set(tt, 'timeformat', pl_timeformat);
0205       else
0206         warning(['### The parameter list doesn''t contain a timeformat.'...
0207           sprintf('\n         ### Use the default timeformat %s'),...
0208           tt.timeformat.default_matlab_str]);
0209       end
0210 
0211       if ~isempty(pl_timezone)
0212         tt = set(tt, 'timezone', pl_timezone);
0213       else
0214         warning(['### The parameter list doesn''t contain a timezone. '...
0215           sprintf('\n         ### Use the default timezone ''UTC''')]);
0216         tt = set(tt, 'timezone', 'UTC');
0217       end
0218 
0219       if ~isempty(pl_epoch_milli)
0220         tt = set(tt, 'utc_epoch_milli', pl_epoch_milli);
0221       elseif ~isempty(pl_time_str)
0222         tt = set(tt, 'time_str', pl_time_str);
0223 
0224         if ~isempty(pl_timeformat)
0225           tt = set(tt, 'timeformat', pl_timeformat);
0226         end
0227 
0228       else
0229         error (['### The parameter list must contain '...
0230           '''utc_epoch_milli'' OR ''time_str'''])
0231       end
0232     end
0233     
0234     % Store input plist
0235     tt.plist = remove(varargin{1}, 'conn');
0236 
0237   %%%%%%%%%%   Unknown constructor   %%%%%%%%%%
0238   else
0239     error ('### Unknown constructor');
0240   end
0241 
0242 elseif nargin == 2
0243 
0244   %%%%%%%%%%   t1 = time('14:00:00', 'HH:MM:SS')   %%%%%%%%%%
0245   if ischar(varargin{1}) && (ischar(varargin{2}) || isnumeric(varargin{2}))
0246 
0247     tt = init();
0248     tt = set(tt, 'time_str'  , varargin{1});
0249     tt = set(tt, 'timeformat', varargin{2});
0250 
0251   %%%%%%%%%%% From DATABASE
0252   elseif isa(varargin{1}, 'database')
0253     tt = retrieve(varargin{1}, varargin{2:end});
0254   %%%%%%%%%%   Unknown constructor   %%%%%%%%%%
0255   else
0256     error ('### Unknown constructor');
0257   end
0258 
0259 else
0260 
0261   error ('### Unknown number of inputs');
0262 
0263 end
0264 
0265 varargout{1} = tt;
0266 
0267 
0268 
0269 %--------------------------------------------------------------------------
0270 % construct a time from a repository
0271 %
0272   function fs = timeFromRepository(pl, VERSION, ALGONAME)
0273 
0274     dpl = getDefaultPlist('From Repository');
0275     pl  = combine(pl, dpl);
0276 
0277     % Get parameters
0278     conn = find(pl, 'conn');
0279     hostname = find(pl, 'hostname');
0280     database = find(pl, 'database');
0281     ids      = find(pl, 'id');
0282 
0283     % do we have a connection?
0284     closeConn = 0;
0285     if isempty(conn)
0286       closeConn = 1;
0287       % Connect to repository
0288       conn = mysql_connect(hostname, database);
0289     end
0290     if ~isa(conn, 'database')
0291       error('### connection failed.');
0292     end
0293     % Get each ID
0294     Nids = length(ids);
0295     fs  = [];
0296     for kk=1:Nids
0297 
0298       %---- This id
0299       id = ids(kk);
0300       disp(sprintf('  - retrieving ID %d', id));
0301 
0302       %---- check ID object type
0303       tt = mysql_getObjType(conn, id);
0304       %---- If this is a time
0305       if strcmp(tt, mfilename)
0306         %---- call database constructor
0307         a = ltpda_obj_retrieve(conn, id);
0308         %---- Add history
0309         %---- Add to output array
0310         fs = [fs a];
0311       else
0312         warning('    !skipping ID %d, type %s', id, tt);
0313       end
0314 
0315     end
0316 
0317     % close connection
0318     if closeConn
0319       close(conn);
0320     end
0321 
0322   end
0323 
0324 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0325 %
0326 % FUNCTION:    getDefaultPlist
0327 %
0328 % DESCRIPTION: Get default params
0329 %
0330 % HISTORY:     11-02-2008 M Hueller
0331 %                 Creation
0332 %
0333 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0334   function out = getDefaultPlist(varargin)
0335 
0336     % list of available parameter sets
0337     sets = {'From Time Definition', 'From Repository'};
0338 
0339     if nargin == 0
0340       out = sets;
0341       return
0342     end
0343 
0344     set = varargin{1};
0345     switch set
0346       case 'From Repository'
0347         out = plist('hostname', 'localhost', 'database', 'ltpda', 'ID', []);
0348       case 'From Time Definition'
0349         out = plist('utc_epoch_milli', 0, ...
0350                     'timezone', 'UTC', ...
0351                     'timeformat', 'yyyy-mm-dd HH:MM:SS.FFF');
0352       otherwise
0353         out = plist();
0354     end
0355 
0356   end
0357 
0358 end % function varargout = time(varargin)
0359 
0360 
0361 
0362

Generated on Fri 07-Mar-2008 15:46:43 by m2html © 2003