octave-maintainers
[Top][All Lists]
Advanced

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

Re: Many Date Functions


From: Bill Denney
Subject: Re: Many Date Functions
Date: Thu, 31 Jan 2008 22:53:11 -0500
User-agent: Thunderbird 2.0.0.9 (Windows/20071031)

John W. Eaton wrote:
On 29-Jan-2008, Bill Denney wrote:

| Bill Denney wrote:
| > Attached are many date functions and a diff that updates a few of the | > current date functions. I'm separating them into several messages | > which will be replies to this one because I had trouble sending | > everything in one message.
| More date functions.

I think these functions could be useful, but I'm not sure that they
belong in the core Octave distribution.  Perhaps a better place for
them would be a separate date/time package?  What do other people
think?

If they go in a separate package, then the copyright notice should be
changed from "This file is part of Octave" and other instances of
"Octave" in the copyright notice should be replace with "this
software".

Thanks,

jwe
That's fair enough. I will incorporate them into the octave-forge time package (which I had forgotten that I authored previously). I had a couple of fixes to functions already in Octave that I've had trouble getting onto the list. Attached are the changes.

Have a good day,

Bill

scripts/ChangeLog:

2008-01-24  Bill Denney  <address@hidden>

* time/weekday.m: allow vector inputs and speed up
* time/eomday.m: fix error with eomday([2004;2005], [2;2]) returning a row instead of a column
? Makefile
? autom4te.cache
? config.log
? config.status
? configure
? audio/Makefile
? control/Makefile
? control/base/Makefile
? control/hinf/Makefile
? control/obsolete/Makefile
? control/system/Makefile
? control/util/Makefile
? deprecated/Makefile
? elfun/Makefile
? finance/Makefile
? general/Makefile
? geometry/Makefile
? image/Makefile
? io/Makefile
? linear-algebra/Makefile
? miscellaneous/Makefile
? optimization/Makefile
? path/Makefile
? pkg/Makefile
? plot/Makefile
? polynomial/Makefile
? quaternion/Makefile
? set/Makefile
? signal/Makefile
? sparse/Makefile
? specfun/Makefile
? special-matrix/Makefile
? startup/Makefile
? statistics/Makefile
? statistics/base/Makefile
? statistics/distributions/Makefile
? statistics/models/Makefile
? statistics/tests/Makefile
? strings/Makefile
? testfun/Makefile
? time/Makefile
? time/datefind.m
? time/day.m
? time/easter.m
? time/eomdate.m
? time/holidays.m
? time/hour.m
? time/lweekdate.m
? time/m2xdate.m
? time/minute.m
? time/month.m
? time/months.m
? time/nweekdate.m
? time/second.m
? time/today.m
? time/x2mdate.m
? time/year.m
? time/yeardays.m
Index: time/eomday.m
===================================================================
RCS file: /cvs/octave/scripts/time/eomday.m,v
retrieving revision 1.4
diff -u -r1.4 eomday.m
--- time/eomday.m       12 Oct 2007 21:27:29 -0000      1.4
+++ time/eomday.m       24 Jan 2008 05:15:06 -0000
@@ -19,7 +19,7 @@
 ## -*- texinfo -*-
 ## @deftypefn {Function File} address@hidden =} eomday (@var{y}, @var{m})
 ## Return the last day of the month @var{m} for the year @var{y}.
-## @seealso{datenum, datevec, weekday}
+## @seealso{datenum, datevec, weekday, eomdate}
 ## @end deftypefn
 
 ## Author: pkienzle <address@hidden>
@@ -33,7 +33,7 @@
   endif
 
   eom = [31, 28, 31, 30 ,31, 30, 31, 31, 30, 31, 30, 31];
-  e = eom(m);
+  e = reshape (eom(m), size (m));
   e += (m == 2) & (mod (y, 4) == 0 & (mod (y, 100) != 0 | mod (y, 400) == 0));
 
 endfunction
@@ -49,6 +49,7 @@
 %!assert(eomday(1:3,1:3),[31,28,31])
 %!assert(eomday(1:2000,2)',datevec(datenum(1:2000,3,0))(:,3))
 
%!assert([1900:1999](find(eomday(1900:1999,2*ones(1,100))==29)),[1904,1908,1912,1916,1920,1924,1928,1932,1936,1940,1944,1948,1952,1956,1960,1964,1968,1972,1976,1980,1984,1988,1992,1996])
+%!assert(eomday([2004;2005], [2;2]), [29;28])
 # demos
 %!demo
 %! y = 1900:1999;
Index: time/weekday.m
===================================================================
RCS file: /cvs/octave/scripts/time/weekday.m,v
retrieving revision 1.4
diff -u -r1.4 weekday.m
--- time/weekday.m      12 Oct 2007 21:27:29 -0000      1.4
+++ time/weekday.m      24 Jan 2008 05:15:06 -0000
@@ -1,4 +1,4 @@
-## Copyright (C) 2000, 2001, 2004, 2005, 2006, 2007 Paul Kienzle
+## Copyright (C) 2000, 2001, 2004, 2005, 2006, 2007, 2008 Paul Kienzle
 ##
 ## This file is part of Octave.
 ##
@@ -34,7 +34,7 @@
 ## Created: 10 October 2001 (CVS)
 ## Adapted-By: William Poetra Yoga Hadisoeseno <address@hidden>
 
-function [n, s] = weekday (d, form)
+function [d, s] = weekday (d, form)
 
   if (nargin < 1 || nargin > 2)
     print_usage ();
@@ -44,24 +44,57 @@
     form = "short";
   endif
 
-  v = datevec (d);
-  t = strptime (sprintf ("%d-%d-%d", v(3), v(2), v(1)), "%d-%m-%Y");
-
-  n = t.wday + 1;
+  if iscell (d) || isnumeric (d)
+       endsize = size (d);
+  elseif ischar (d)
+       endsize = [size(d, 1), 1];
+  end
+  if ischar (d) || iscell (d)
+       ## Make sure the date is numeric
+       d = datenum (d);
+  endif
+  ## Find the offset from a known Sunday (2008-Jan-6), mod 7
+  d = floor (reshape (mod(d - 733048, 7), endsize));
+  ## make Saturdays a 7 and not a 0
+  d(~d) = 7;
 
   if (nargout > 1)
     if (strcmpi (form, "long"))
-      s = strftime ("%A", t);
+         names = {"Sunday" "Monday" "Tuesday" "Wednesday" "Thursday"
+                          "Friday" "Saturday"};
     else
-      s = strftime ("%a", t);
+         names = {"Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat"};
     endif
+       s = strvcat (names(d));
   endif
 
 endfunction
 
 # tests
 %!assert(weekday(728647),2)
+## Test vector inputs for both directions
+%!assert(weekday([728647 728648]), [2 3])
+%!assert(weekday([728647;728648]), [2;3])
+## Test a full week before our reference day
 %!assert(weekday('19-Dec-1994'),2)
+%!assert(weekday('20-Dec-1994'),3)
+%!assert(weekday('21-Dec-1994'),4)
+%!assert(weekday('22-Dec-1994'),5)
+%!assert(weekday('23-Dec-1994'),6)
+%!assert(weekday('24-Dec-1994'),7)
+%!assert(weekday('25-Dec-1994'),1)
+## Test our reference day
+%!assert(weekday('6-Jan-2008'),1)
+## Test a full week after our reference day
+%!assert(weekday('1-Feb-2008'),6)
+%!assert(weekday('2-Feb-2008'),7)
+%!assert(weekday('3-Feb-2008'),1)
+%!assert(weekday('4-Feb-2008'),2)
+%!assert(weekday('5-Feb-2008'),3)
+%!assert(weekday('6-Feb-2008'),4)
+%!assert(weekday('7-Feb-2008'),5)
+## Test fractional dates
+%!assert(weekday(728647.1),2)
 # demos
 %!demo
 %! [n, s] = weekday (now ())


reply via email to

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