function time=zeitdecode(t1,varargin)
% returns a decoded time from string t1 (does not work for dates
% earlier then year 99 except if var.argument YYYY is given)
% as day,month,year,hour,minute,second
% if no date can be decoded it interactivly asks the user
% normally format as used in datenum plus:
%   2000/12/31 if year is larger then 31
%   use of german short month names Okt,Mai
%   separating . instead of -
%   single year if one value between 1800 and 2010
%   27.1.2003 or 27.1.2003 23:45:34
%     this format as given implies dd.mm.yyyy. It can also be
%     formatted in another way, as long 3 or 6 integer numbers
%     are readable in the input text. 
%
% VARIBLE ARGUMENTS
%   'dd/mm/yy': if format found would normally
%               be 'yy/mm/dd' or 'yy-mm-dd'
%               interpret it as dd/mm/yy
%   'JUL' : give back time in julian days
%   'YYYY': assumes year is always given as 4 digits, this implies
%           that years smaller then 99 are assumed to be really
%           years till 99 AC instead of aroud a pivot year

IJUL=0; ddmmyy=0; YYYY=false;
for ii=1:nargin-1
    t=varargin{ii};
    if ~isstr(t); error('unknown varargi'); end
    switch upper(t)
     case 'JUL'
      IJUL=1;
     case 'DD/MM/YY'
      ddmmyy=1;
     case 'YYYY'
      YYYY=true;
     otherwise
      error('unknown varargi')
    end
end
t2=t1; % just a copy of the original string

% search for format 2000/12/31 or 2000-12-31
Nowyear=datevec(date); Nowyear=Nowyear(1);
t1=strrep(t1,':',' '); t1=strrep(t1,'/',' '); t1=strrep(t1,'-',' ');
t1=strrep(t1,'\0',' ');
eval('time=str2num(t1);','time=[];');
if isempty(time)  % search for format with 3 or 6 integers,
                  % e.g. 27.1.2002 this format implies dd.mm.yyyy
  for ii=1:6; 
    time(ii)=iles(t1,ii);
    if isnan(time(ii)) break; end; 
  end
  if length(time)==6 & isnan(time(6)); time(6)=0; end  % Nov.05, falls keine sekunden da, dann sec=0
  if ~(length(time)==6 | length(time)==4) ; time=[]; end 
  if  length(time)==4; time=time(1:3); end
  if ~isempty(time); ddmmyy=1; end
end

if length(time)>6 | length(time)<3 ; 
    time=[];
else
  % check if time 
  time=[time zeros(1,6-length(time))];
  if length(time)>3 & ( time(4)<0 | time(4)>24); time=[]; end
  if length(time)>4 & ( time(5)<0 | time(5)>60); time=[]; end
  if length(time)>5 & ( time(6)<0 | time(6)>60); time=[]; end
  if length(time)==6
    if ddmmyy==1; time(1:3)=time([3 2 1]); end
    if time(2)>0 & time(2)<13 & time(3)>0 & time(3)<32 & time(1)<Nowyear+100 
      if time(1)<100 & ~YYYY;
	while time(1)<Nowyear-100; time(1)=time(1)+100; end
      end
      time=datenum(time);
    else
      time=[];
    end
  end
end
t1=t2;
if isempty(time);
  t1=strrep(t1,':',' '); t1=strrep(t1,'-',' '); t1=strrep(t1,'/',' '); t1=strrep(t1,'\0',' ');
  eval('time=str2num(t1);','time=[];');
  if length(time)>6 | length(time)<3; 
    time=[]; 
  else
    time=[time zeros(1,6-length(time))];
    if length(time)>3 & ( time(4)<0 | time(4)>24); time=[]; end
    if length(time)>4 & ( time(5)<0 | time(5)>60); time=[]; end
    if length(time)>5 & ( time(6)<0 | time(6)>60); time=[]; end
    if length(time)==6 & time(1)>31 & time(2)<13 & time(3)<32 & ...
	  time(1)<Nowyear+100 & time(2)>0 & time(3)>0
      time=datenum(time);
    else
      time=[];
    end
  end
end

t1=t2;
% jetzt normale datenum versuchen
if isempty(time);
   eval('time=datenum(t1);','time=[];');
end
if isempty(time);  % german month names (3-characters)
    ii=findstr(upper(t1),'OKT'); 
    if ~isempty(ii); t1(ii(1):ii(1)+2)='Oct'; end  
    ii=findstr(upper(t1),'DEZ'); 
    if ~isempty(ii); t1(ii(1):ii(1)+2)='Dec'; end  
    ii=findstr(upper(t1),'MAI'); 
    if ~isempty(ii); t1(ii(1):ii(1)+2)='May'; end  
    eval('time=datenum(t1);','time=[];');
end
if isempty(time);  % separating . instead of -
    ii=findstr(t1,'.');
    if ~isempty(ii); t1(ii)='-'; end
    eval('time=datenum(t1);','time=[];');
end       
if isempty(time);  % single year
    % one value between 1800 and 2010
    ii=str2num(t2);
    if length(ii)==1; 
        if ii>1800 & ii<2010; time=datenum(ii,1,1); end
    end
end       

if isempty(time);                    
    warning(['Time could not be read. String:',t2]);
    fprintf('known time formats and example:\n')
    fprintf(' dd-mmm-yyyy HH:MM:SS   01-Mar-2000 15:45:17 \n')
    fprintf(' dd-mmm-yyyy             01-Mar-2000  \n')
    fprintf(' mm/dd/yy                03/01/00    \n') 
    fprintf(' mm/dd/yyyy              03/01/2000 \n')
    while isempty(time)
        t2=input('Give right Time String:','s');
	t1=t2;
        eval('time=datenum(t1);','time=[];');
    end                    
end
if IJUL==1
    time=time+julianday(1,1,0)-1;
else
   time=datevec(time);time=time([3 2 1 4 5 6]);
end
return
