% UTP_TIME_FORMAT
%
% <MethodDescription>
%
% Test the 'format' method of the time class.
%
% </MethodDescription>
%
% NOTE: in this test we manipulate user preferences thus we should make
% sure we restore the original user preferences at the end of the testing
%
% $Id: utp_time_format.m,v 1.3 2011/04/28 07:06:27 hewitson Exp $

function results = utp_time_format(varargin)
  
  % check inputs
  if nargin == 0
    
    % some keywords
    class   = 'time';
    mthd    = 'format';
    
    results = [];
    disp('******************************************************');
    disp(['****  Running UTPs for ' class '/' mthd]);
    disp('******************************************************');
    
    % get preferences
    prefs = getappdata(0, 'LTPDApreferences');
    oldTimezone   = char(prefs.getTimePrefs.getTimeTimezone);
    oldTimeformat = char(prefs.getTimePrefs.getTimestringFormat);
    
    % get preferences
    try
      results = [results utp_901];
    catch ex
      % restore preferences
      prefs.getTimePrefs.setTimeTimezone(oldTimezone);
      prefs.getTimePrefs.setTimestringFormat(oldTimeformat);
      rethrow(ex);
    end
    
    % restore preferences
    prefs.getTimePrefs.setTimeTimezone(oldTimezone);
    prefs.getTimePrefs.setTimestringFormat(oldTimeformat);
    
    disp('Done.');
    disp('******************************************************');
    
  elseif nargin == 1
    % check for UTP functions
    if strcmp(varargin{1}, 'isutp')
      results = 1;
    else
      results = 0;
    end
  else
    error('### Incorrect inputs')
  end
  
  
  %% UTP_901
  
  % <TestDescription>
  %
  % Tests 'format' static method.
  %
  % </TestDescription>
  function result = utp_901
    
    % <SyntaxDescription>
    % Use the static format method to produce a number of time strings in
    % different formats and for different time-zones.
    % </SyntaxDescription>
    stest = false;
    try
      % <SyntaxCode>
      % set time zone and time format in the toolbox preferences
      prefs.getTimePrefs.setTimeTimezone('CET');
      prefs.getTimePrefs.setTimestringFormat('yyyy-mm-dd HH:MM:SS');
      t = time('1970-01-01 00:00:00.000 UTC');
      str1 = t.format();
      str2 = t.format('HH:MM:SS');
      str3 = t.format('HH:MM:SS', 'JST');
      
      % set time zone and time format in the toolbox preferences
      prefs.getTimePrefs.setTimeTimezone('UTC');
      prefs.getTimePrefs.setTimestringFormat('yyyy-mm-dd HH:MM:SS');
      str4 = t.format();
      str5 = t.format('HH:MM:SS');
      str6 = t.format('HH:MM:SS', 'JST');
      % </SyntaxCode>
      stest = true;
    end
    
    atest = true;
    try
      % do not run algorithm tests if sintax tests failed
      assert(stest);
      
      % <AlgoDescription>
      % Check that the returned strings match the expected strings.
      % </AlgoDescription>
      % <AlgoCode>
      assert(strcmp(str1, '1970-01-01 01:00:00'));
      assert(strcmp(str2, '01:00:00'));
      assert(strcmp(str3, '09:00:00'));
      assert(strcmp(str4, '1970-01-01 00:00:00'));
      assert(strcmp(str5, '00:00:00'));
      assert(strcmp(str6, '09:00:00'));
      % </AlgoCode>
    catch
      atest = false;
    end
    
    % return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end
  
end
