help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Problem with dates


From: David Bateman
Subject: Re: Problem with dates
Date: Tue, 24 Jul 2007 15:43:04 +0200
User-agent: Thunderbird 1.5.0.7 (X11/20060921)

Oscar Bayona Candel wrote:
> Hi all,
>
> I would like to know how can I to difference (rest) two dates in octave. 
> Similar to Matlab function Daysact(Date0,Date1)=Date1-Date0.
>
> I would like a way to do it. But I couldmnĀ“t find it.
>
> Thanks a lot.
>
>   
This is in the financial toolbox and matlab makes you pay for it. At its
most basic it is just

function days = daysact (d1, d2)
  days = datenum (d2) - datenum (d1);
end

with all its bells and whistles consider the attached file written in
about 5 minutes... How much again did you pay for this toolbox?

D.

-- 
David Bateman                                address@hidden
Motorola Labs - Paris                        +33 1 69 35 48 04 (Ph) 
Parc Les Algorithmes, Commune de St Aubin    +33 6 72 01 06 33 (Mob) 
91193 Gif-Sur-Yvette FRANCE                  +33 1 69 35 77 01 (Fax) 

The information contained in this communication has been classified as: 

[x] General Business Information 
[ ] Motorola Internal Use Only 
[ ] Motorola Confidential Proprietary

## Copyright (C) 2007 David Bateman
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  
USA

## -*- texinfo -*-
## @deftypefn {Function File} {} daysact (@var{d1})
## @deftypefnx {Function File} {} daysact (@var{d1}, @var{d2})
## Calculates the number of days between two dates. If the second date is not
## given, calculate the number of days since 1-Jan-0000. The variables @var{d1}
## and @var{d2} can either be strings or an @var{n}-row string matrix. If both
## @var{d1} and @var{d2} are string matrices, then the number of rows must 
## match. An example of the use of @code{daysact} is
##
## @example
## @group
## daysact ("01-Jan-2007", ["10-Jan-2007"; "23-Feb-2007"; "23-Jul-2007"])
## @result{}      9
##        53
##       203
## @end group
## @end example
## @seealso{datenum}
## @end deftypefn

function days = daysact (d1, d2)
 if (nargin == 1)
   nr = size (d1, 1);
   if (nr != 1)
     days = zeros (nr,1);
     for i = 1 : nr
       days (i) = datenum (d1 (i,:));
     endfor
   else
     days = datenum(d1);
   endif
 elseif (nargin == 2)
   nr1 = size (d1, 1);
   nr2 = size (d2, 1);   
   if (nr1 != nr2 && nr1 != 1 && nr2 != 1)
     error ("daysact: size mismatch");
   endif
   if (nr1 == 1 && nr2 == 1)
     days = datenum (d2) - datenum(d1);
   elseif (nr1 == 1)
     days = zeros (nr2, 1);
     for i = 1 : nr2
       days(i) = datenum (d2 (i,:)) - datenum (d1);
     endfor
   elseif (nr2 == 1)
     days = zeros (nr1, 1);
     for i = 1 : nr1
       days(i) = datenum (d2) - datenum (d1 (i,:));
     endfor
   else
     days = zeros (nr1, 1);
     for i = 1 : nr1
       days(i) = datenum (d2 (i, :)) - datenum (d1 (i,:));
     endfor
   endif
 else
   print_usage();
  endif
endfunction

%!assert (daysact ("01-Jan-2007", ["10-Jan-2007"; "23-Feb-2007"; 
"23-Jul-2007"]),[9;53;203])

reply via email to

[Prev in Thread] Current Thread [Next in Thread]