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')

 VERSION:      $Id: time.m,v 1.12 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:

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

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