function checkTimeFormat(h,name,colrow,indexStr)
% CHECKTIMEFORMAT checks the time format of the given row and/or column and
% update the information stored in 'h.IOData.formatcell' struct

% stored information
%   name: string, checked active sheet name
%   columnIndex: integer, checked columnindex
%   rowIndex: integer, checked rowindex
%   columnIsAbsTime: integer, a time format 
%   columnFormat: cell of string, original NumberFormat in excel
%   rowIsAbsTime: integer, a time format 
%   rowFormat: cell of string, original NumberFormat in excel
%   
%   time format:
%   -1      double values, which could be relative time points
%   >=0     absolute date/time format supported by Matlab
%   NaN     not a time format (unrecognizable), e.g. a string
%
% inputs:   name: a string of the active sheetname
%           colrow: a string of 'row', 'column', 'both'
%           index: a string of '1', '2', ... or 'A', 'B', ...

% Author: Rong Chen 
% Revised: 
% Copyright 1986-2004 The MathWorks, Inc.

% this function is only called when ActiveX web component is available
if isempty(h.Handles.ActiveX)
    return
end

if strcmp(colrow,'column')
    % check a column
    % get column index
    index=h.findcolumnnumber(indexStr);
    % check if recheck is necessary
    if ~isempty(index)
        if h.IOData.formatcell.columnIndex~=index || ~strcmp(h.IOData.formatcell.name,name)
            % check and update
            localCheckColumn(h,name,index);
        end
    end
elseif strcmp(colrow,'row')
    % check a row
    try
        % get row index
        index=str2double(indexStr);
    catch
        return
    end
    % check if recheck is necessary
    if h.IOData.formatcell.rowIndex~=index || ~strcmp(h.IOData.formatcell.name,name)
        % check and update
        localCheckRow(h,name,index);
    end
elseif strcmp(colrow,'both')
    % only called once at the initialization stage
    % get index
    index=str2double(indexStr);
    % try column
    localCheckColumn(h,name,index);
    % try row
    localCheckRow(h,name,index);
end
          

function localCheckColumn(h,name,index)
% check time format for a column and store information into formatcell

% get sheet from the original excel workbook
sheet = get(h.Handles.originalSheets,'Item',name);
% initialize flag storage
h.IOData.formatcell.columnIsAbsTime=NaN;
h.IOData.formatcell.columnFormat={''};
% get column letter
columnLetter=h.findcolumnletter(index+sheet.UsedRange.Column-1);
% check each cell until reaches checkLimit
% be careful with the usedrange offsets
for i=min(size(sheet.UsedRange.Value,1),h.IOData.checkLimit):-1:1
    % get numberformat
    tmpstr=[columnLetter num2str(i+sheet.UsedRange.Row-1)];
    tmpcell={sheet.Range(tmpstr).NumberFormat};
    % if it is date/time, set the flag true
    tmpFlag=h.IsTimeFormat(tmpcell,{sheet.Range(tmpstr).Value});
    if tmpFlag<0
        % relative time
        h.IOData.formatcell.columnIsAbsTime=-1;
        h.IOData.formatcell.columnFormat={'General'};
        break;
    else
        if ~isnan(tmpFlag)
            % absolute time
            h.IOData.formatcell.columnIsAbsTime=tmpFlag;
            h.IOData.formatcell.columnFormat=tmpcell;
            break;
        end
    end
end
h.IOData.formatcell.columnIndex=index;
h.IOData.formatcell.rowIndex=0;
h.IOData.formatcell.name=name;


function localCheckRow(h,name,index)
% check time format for a row and store information into formatcell

% get sheet from the original excel workbook
sheet = get(h.Handles.originalSheets,'Item',name);
% initialize flag storage
h.IOData.formatcell.rowIsAbsTime=NaN;
h.IOData.formatcellrowFormat={''};
% get row number
rowNumber=num2str(index+sheet.UsedRange.Row-1);
% check each cell until reaches checkLimit
% be careful with the usedrange offsets
for i=min(size(sheet.UsedRange.Value,2),h.IOData.checkLimit):-1:1
    % get numberformat
    tmpstr=[h.findcolumnletter(i+sheet.UsedRange.Column-1) rowNumber];
    readinfo=get(sheet,'Range',tmpstr);
    tmpcell={readinfo.NumberFormat};
    % if it is date/time, set the flag true
    tmpFlag=h.IsTimeFormat(tmpcell,{readinfo.Value});
    if tmpFlag<0
        % relative time
        h.IOData.formatcell.rowIsAbsTime=-1;
        h.IOData.formatcell.rowFormat={'General'};
        break;
    else
        if ~isnan(tmpFlag)
            h.IOData.formatcell.rowIsAbsTime=tmpFlag;
            h.IOData.formatcell.rowFormat=tmpcell;
            break;
        end
    end
end
h.IOData.formatcell.columnIndex=0;
h.IOData.formatcell.rowIndex=index;
h.IOData.formatcell.name=name;

