Code covered by the BSD License  

Highlights from
Date Odometer class

image thumbnail

Date Odometer class

by

 

12 Apr 2011 (Updated )

Add an odometer-like object to a plot to show the progression of time.

which_digits_change(date_1, date_2, style)
function digits_changed = which_digits_change(date_1, date_2, style)
% Returns a vector of which digits changed between date_1 & date_2.
%
% digits_changed = which_digits_change(date_1, date_2, [style])
%
% Ex.
%  digits_changed = which_digits_change(datenum(2011, 04, 11, 00, 00, 00), ...
%                                       datenum(2011, 04, 12, 00, 00, 00));
%   Since default style is 'mm/dd/yyyy', only digit 4 (least day digit)
%   changes and function returns 4.
%
%  digits_changed = which_digits_change(datenum(2011, 04, 11, 00, 00, 00), ...
%                                       datenum(2011, 04, 20, 00, 00, 00));
%   Since default style is 'mm/dd/yyyy', digits 3&4 change and function
%   returns [3, 4].
%
%  digits_changed = which_digits_change(datenum(2011, 04, 11, 00, 00, 00), ...
%                                       datenum(2011, 04, 20, 00, 00, 00),
%                                       'yyyy-mm-dd');
%   Since style is now 'yyyy-mm-dd', digit 7&8 change and function
%   returns [7, 8].
%
% Inputs:
%   date_1, date_2      Date/time in datenum format
%   style               Optional text. Default: 'mm-dd-yyyy'
%
% Output:
%   digits_changed      Vector of which digits have changed.

% Kevin J. Delaney, BMT Scientific Marine Services
% April 11, 2011

digits_changed = [];
default_style = 'mm-dd-yyyy';

if ~exist('date_1', 'var')
    help(mfilename);
    return
end

if isempty(date_1) || ~isnumeric(date_1)
    errordlg('Input "date_1" is empty or non-numeric.', mfilename);
    return
end

if ~isdatenum(date_1)
    errordlg('Input "date_1" is not a valid datenum.', mfilename);
    return
end

if ~exist('date_2', 'var') || isempty(date_2) || ~isnumeric(date_2)
    errordlg('Input "date_2" is missing, empty or non-numeric.', mfilename);
    return
end

if ~isdatenum(date_2)
    errordlg('Input "date_2" is not a valid datenum.', mfilename);
    return
end

if ~exist('style', 'var') || isempty(style) || ~ischar(style)
    style = default_style;
end

[year_1, month_1, day_1] = datevec(date_1);
[year_2, month_2, day_2] = datevec(date_2);

year_digits_1 = break_into_digits(year_1, 4);
year_digits_2 = break_into_digits(year_2, 4);
year_digits_change_syndrome = (year_digits_1 ~= year_digits_2);

month_digits_1 = break_into_digits(month_1, 2);
month_digits_2 = break_into_digits(month_2, 2);
month_digits_change_syndrome = (month_digits_1 ~= month_digits_2);

day_digits_1 = break_into_digits(day_1, 2);
day_digits_2 = break_into_digits(day_2, 2);
day_digits_change_syndrome = (day_digits_1 ~= day_digits_2);

switch lower(style)

    case {'mm-dd-yyyy', 'mm/dd/yyyy'}
        month_digits = 1:2;
        day_digits   = 3:4;
        year_digits  = 5:8;

    case {'mmm-dd-yyyy', 'mmm/dd/yyyy'}
        month_digits = 1;
        day_digits   = 2:3;
        year_digits  = 4:7;
        month_digits_change_syndrome = any(month_digits_change_syndrome);
        
    case {'dd-mm-yyyy', 'dd/mm/yyyy'}
        day_digits   = 1:2;
        month_digits = 3:4;
        year_digits  = 5:8;

    case {'yyyy-mm-dd', 'yyyy/mm/dd'}
        year_digits  = 1:4;
        month_digits = 5:6;
        day_digits   = 7:8;
        
    otherwise
        errordlg(['Unknown "style": "', style, '".'], mfilename);
        return
end

digits_changed = sort([month_digits(month_digits_change_syndrome), ...
                       day_digits(day_digits_change_syndrome), ...
                       year_digits(year_digits_change_syndrome)]);

Contact us