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

Generated on Mon 03-Sep-2007 12:12:34 by m2html © 2003