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(plist)
               t1 = time('14:00:00', 'HH:MM:SS')
               t1 = time('T1: 14:00:00', 'T1: HH:MM:SS')

 VERSION:      $Id: time.m,v 1.15 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 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(plist)
0020 %               t1 = time('14:00:00', 'HH:MM:SS')
0021 %               t1 = time('T1: 14:00:00', 'T1: HH:MM:SS')
0022 %
0023 % VERSION:      $Id: time.m,v 1.15 2008/01/15 17:36:36 ingo Exp $
0024 %
0025 % HISTORY:      23-07-2007 Diepholz
0026 %                  Creation
0027 %
0028 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0029 
0030 VERSION = '$Id: time.m,v 1.15 2008/01/15 17:36:36 ingo Exp $';
0031 
0032 % Check if this is a call for parameters
0033 if nargin == 2
0034   if isa(varargin{1}, 'time') && ischar(varargin{2})
0035     in = char(varargin{2});
0036     if strcmp(in, 'Params')
0037       varargout{1} = plist;
0038       return
0039     elseif strcmp(in, 'Version')
0040       varargout{1} = VERSION;
0041       return
0042     end
0043   end
0044 end
0045 
0046 import java.util.GregorianCalendar;
0047 import java.util.TimeZone;
0048 
0049 %%%%%%%%%%%%%%%%%%%%%%%%%%%   Get default values   %%%%%%%%%%%%%%%%%%%%%%%%%%%%
0050 
0051 setup_timezone   = getappdata(0, 'timezone');
0052 setup_format_str = getappdata(0, 'time_format_str');
0053 
0054 % REMARK: The default properties must match together.
0055 %         They must represent the same time format.
0056 default_matlab_str = 'yyyy-mm-dd HH:MM:SS.FFF';
0057 default_matlab_nr  = -1;
0058 default_java_str   = 'yyyy-MM-dd HH:mm:ss.SSS';
0059 
0060 %%%%%%%%%%%%%%%%%%%%%%%%%%%%   Get current time   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0061 
0062 % Define local/utc timezone
0063 timezone = TimeZone.getTimeZone(setup_timezone);
0064 
0065 % Define a utc calendar
0066 cal_utc    = GregorianCalendar(timezone);
0067 
0068 % Get the unix time (timezone = UTC) in milliseconds
0069 utc_epoch_milli = cal_utc.getTimeInMillis;
0070 
0071 %%%%%%%%%%%%%%%%%%%%%%%%%   define time properties   %%%%%%%%%%%%%%%%%%%%%%%%%%
0072 
0073   function tt = init
0074     tt.name            = 'time-object';
0075     tt.utc_epoch_milli = utc_epoch_milli;
0076 
0077     tt.timezone        = timezone;
0078 
0079     tt.timeformat      = timeformat(setup_format_str,   ...
0080                                     default_matlab_str, ...
0081                                     default_matlab_nr,  ...
0082                                     default_java_str);
0083 
0084     t_format = java.text.SimpleDateFormat(default_java_str);
0085     t_format.setTimeZone(tt.timezone);
0086 
0087     tt.time_str = char(t_format.format (tt.utc_epoch_milli));
0088     tt.created  = tt.time_str;
0089     tt.version  = VERSION;
0090 
0091     tt = class(tt, 'time');
0092 
0093     % Set the ltpda_startup time format
0094     tt = set(tt, 'timeformat', tt.timeformat.format_str);
0095   end
0096 
0097 %%%%%%%%%%%%%%%%%%%%%%%%%%%   Create time object   %%%%%%%%%%%%%%%%%%%%%%%%%%%%
0098 
0099 %%%%%%%%%%  t1 = time()   %%%%%%%%%%
0100 if nargin == 0
0101 
0102   tt = init();
0103 
0104 elseif nargin == 1
0105 
0106   %%%%%%%%%% Create from XML fragment %%%%%%%%%%%
0107   if isa(varargin{1}, 'org.apache.xerces.dom.DeferredElementImpl')
0108     tt = fromxml(varargin{1});
0109 
0110   %%%%%%%%%%   t1 = time(time-object)   %%%%%%%%%%
0111   elseif isa(varargin{1}, 'time')
0112 
0113     tt = varargin{1};
0114 
0115   %%%%%%%%%%   t1 = time('t0_default')   %%%%%%%%%%
0116   elseif strcmp(varargin{1}, 't0_default')
0117 
0118     tt = init();
0119     tt = set(tt, 'utc_epoch_milli', 0);
0120 
0121   %%%%%%%%%%   t1 = time('2007-08-03 10:00:00')   %%%%%%%%%%
0122   %%%%%%%%%%   t1 = time('foo.mat')               %%%%%%%%%%
0123   %%%%%%%%%%   t1 = time('foo.xml')               %%%%%%%%%%
0124   elseif ischar(varargin{1})
0125 
0126     % is this a filename?
0127     [path, name, ext, vers] = fileparts(varargin{1});
0128     if ~isempty(ext) && length(ext) == 4 && isempty(str2num(ext(2)))
0129 
0130       switch ext
0131         case '.mat'
0132           tt = load(varargin{1});
0133           tt = tt.a;
0134         case '.xml'
0135           tt = xmlparse(time(), varargin{1});
0136         otherwise
0137           error('### Unknown file type.');
0138       end
0139 
0140     else
0141       tt = init();
0142       tt = set(tt, 'time_str', varargin{1});
0143     end
0144 
0145   %%%%%%%%%%   t1 = time(1186135200000)   %%%%%%%%%%
0146   elseif isnumeric(varargin{1})
0147 
0148     tt = init();
0149     tt = set(tt, 'utc_epoch_milli', varargin{1});
0150 
0151   %%%%%%%%%%   t1 = time(struct)   %%%%%%%%%%
0152   elseif isstruct(varargin{1})
0153 
0154     tt = init();
0155 
0156     fields = fieldnames(varargin{1});
0157     for ii = 1:length(fields)
0158       field = fields{ii};
0159       %%% timeformat -> timeformat-object
0160       if strcmp(field, 'timeformat')
0161         tf = varargin{1}.timeformat;
0162         if isstruct(tf)
0163           tf = timeformat(tf);
0164         end
0165         tt.timeformat = tf;
0166       %% All other
0167       else
0168         try
0169           tt.(field) = varargin{1}.(field);
0170         catch
0171           error('### The field ''%s'' in the struct is not a time property.', field)
0172         end
0173       end
0174     end
0175 
0176   %%%%%%%%%%   t1 = time(plist)   %%%%%%%%%%
0177   elseif isa(varargin{1}, 'plist')
0178 
0179     pl_timezone    = find(varargin{1}, 'timezone');
0180     pl_epoch_milli = find(varargin{1}, 'utc_epoch_milli');
0181     pl_time_str    = find(varargin{1}, 'time_str');
0182     pl_timeformat  = find(varargin{1}, 'timeformat');
0183 
0184     tt = init();
0185 
0186     if ~isempty(pl_timeformat)
0187       tt = set(tt, 'timeformat', pl_timeformat);
0188     else
0189       warning(['### The parameter list doesn''t contain a timeformat.'...
0190                sprintf('\n         ### Use the default timeformat %s'),...
0191                tt.timeformat.setup_format_str]);
0192     end
0193 
0194     if ~isempty(pl_timezone)
0195       tt = set(tt, 'timezone', pl_timezone);
0196     else
0197       warning(['### The parameter list doesn''t contain a timezone. '...
0198                 sprintf('\n         ### Use the default timezone ''UTC''')]);
0199       tt = set(tt, 'timezone', 'UTC');
0200     end
0201 
0202     if ~isempty(pl_epoch_milli)
0203       tt = set(tt, 'utc_epoch_milli', pl_epoch_milli);
0204     elseif ~isempty(pl_time_str)
0205       tt = set(tt, 'time_str', pl_time_str);
0206 
0207       if ~isempty(pl_timeformat)
0208         tt = set(tt, 'timeformat', pl_timeformat);
0209       end
0210 
0211     else
0212       error (['### The parameter list must contain '...
0213               '''utc_epoch_milli'' OR ''time_str'''])
0214     end
0215 
0216   %%%%%%%%%%   Unknown constructor   %%%%%%%%%%
0217   else
0218     error ('### Unknown constructor');
0219   end
0220 
0221 elseif nargin == 2
0222 
0223   %%%%%%%%%%   t1 = time('14:00:00', 'HH:MM:SS')   %%%%%%%%%%
0224   if ischar(varargin{1}) && ischar(varargin{2})
0225 
0226     tt = init();
0227     tt = set(tt, 'timeformat', varargin{2});
0228     tt = set(tt, 'time_str'  , varargin{1});
0229 
0230   %%%%%%%%%%% From DATABASE
0231   elseif isa(varargin{1}, 'database')
0232     tt = retrieve(varargin{1}, varargin{2:end});
0233   %%%%%%%%%%   Unknown constructor   %%%%%%%%%%
0234   else
0235     error ('### Unknown constructor');
0236   end
0237 
0238 else
0239 
0240   error ('### Unknown number of inputs');
0241 
0242 end
0243 
0244 varargout{1} = tt;
0245 
0246 end % function varargout = time(varargin)
0247 
0248 
0249 
0250

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