function [Time,Startdate]=tsAnalyzeAbsTime(timeArray,Units,varargin)
%TSANALYZEABSTIME inteprets absolute date/time string and returns
%information needed
%
% [Time,Format,Startdate]=TSANALYZEABSTIME(timeArray, Unit, varargin) check
% the string content in the timeArray, which can be either a cell array of
% strings or a char array.  Unit should be one of 'years', 'weeks', 'days',
% 'hours', 'mins', 'secs'.  Varargin, if supplied, should be a string
% containing a valid absolute start date, e.g. '10-Oct-2004 12:34:56',
% which is used as the reference date for generating the relative time
% value. 
%
% Time is the numeric value (in the given unit) of TimeArray relative to
% either the reference date (if suuplied) or the first time point in
% TimeArray. However, if TimeArray contains only hour/minute/second (e.g.
% 'HH:MM:SS'), Time is the numeric value (in the given unit) of TimeArray
% relative to the '00:00:00' time point.
% 
% Format returns the default display format, which is either the Standard
% MATLAB Date format 0 ('dd-mmm-yyyy HH:MM:SS') or 13 ('HH:MM:SS').
%
% Startdate returns the first value in TimeArray if no reference date is
% supplied.  Otherwise, it returns the reference date.
%

%   Author(s): James G. Owen, Rong Chen
%   Copyright 1986-2004 The MathWorks, Inc.
%   $Revision: 1.1.6.1 $  $Date: 2004/12/26 21:46:04 $

try
    % get abs time in [year mon day hour min sec] format and sort them
    dateVec = sortrows(round(datevec(timeArray)));
catch
    error('not a valid absolute date/time vector.');
end
% Check for duplicate times
if min(sum(diff(dateVec),2))==0
    error('Duplicate times are invalid.');
end
% get time difference and convert it into date number format
YMD_half=dateVec;
YMD_half(:,4:6)=0;
HMS_half=dateVec;
HMS_half(:,1:3)=0;
YMD_dateNum=datenum(YMD_half);
HMS_dateNum=datenum(HMS_half);

% get the first time point string
if iscell(timeArray)
    refPoint=timeArray{1};
else
    refPoint=timeArray(1,:);
end

% start date is supplied by user
if ~isempty(varargin) && ~isempty(varargin{1})
    % reference time point is provided
    if ~(ischar(varargin{1}) && isvector(varargin{1}))
        error('The reference time point should be a string.');
    end
    if length(refPoint)<=11 && length(strfind(refPoint,':'))>0
        % contains only hour/minute/second information
        % ignore the reference time point
        Time = tsunitconv(Units,'days')*(YMD_dateNum-datenum('00:00:00')) + tsunitconv(Units,'days')*HMS_dateNum;
        % set format
        % Format = 'HH:MM:SS';
        % set start date empty
        Startdate = '';        
    else
        % time point is in date format
        if length(refPoint)<=11 && length(strfind(refPoint,':'))>0
            error('time point should be in absolute date format (e.g. ''10/25/2004 12:34:56'').');
        else
            try
                dateVecStart = round(datevec(varargin{1}));
            catch
                error('Format of the start date is not recognizable.');
            end
            % get time difference and convert it into date number format
            YMD_halfStart=dateVecStart;
            YMD_halfStart(:,4:6)=0;
            HMS_halfStart=dateVecStart;
            HMS_halfStart(:,1:3)=0;
            YMD_dateNumStart=datenum(YMD_halfStart);
            HMS_dateNumStart=datenum(HMS_halfStart);
            % contains absolute date information
            Time = tsunitconv(Units,'days')*(YMD_dateNum-YMD_dateNumStart) + tsunitconv(Units,'days')*(HMS_dateNum-HMS_dateNumStart);
            % set format
            % Format = 'dd-mmm-yyyy HH:MM:SS';
            % set start date
            Startdate = varargin{1};
        end
    end    
else
    % use string length and ':' to identify whether it contains date or not
    if length(refPoint)<=11 && length(strfind(refPoint,':'))>0
        % contains only hour/minute/second information
        Time = tsunitconv(Units,'days')*(YMD_dateNum-datenum('00:00:00')) + tsunitconv(Units,'days')*HMS_dateNum;
        % set format
        % Format = 'HH:MM:SS';
        % set start date empty
        Startdate = '';        
    else
        % contains absolute date information
        Time = tsunitconv(Units,'days')*(YMD_dateNum-YMD_dateNum(1)) + tsunitconv(Units,'days')*(HMS_dateNum-HMS_dateNum(1));
        % set format
        % Format = 'dd-mmm-yyyy HH:MM:SS';
        % set start date
        Startdate = datestr(datenum(dateVec(1,:)));
    end
end