changeset 11782:35e6dbbb0d87 release-3-0-x

[project @ 2008-02-01 06:32:06 by jwe]
author jwe
date Fri, 01 Feb 2008 06:34:12 +0000
parents d590e5c06b4f
children 7d9711bd4298
files scripts/ChangeLog scripts/time/eomday.m scripts/time/weekday.m
diffstat 3 files changed, 49 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Tue Jun 03 07:16:58 2008 +0200
+++ b/scripts/ChangeLog	Fri Feb 01 06:34:12 2008 +0000
@@ -1,3 +1,8 @@
+2008-06-04  Bill Denney  <bill@denney.ws>
+
+	* time/weekday.m: Allow vector inputs and speed up.
+	* time/eomday.m: Return column vector for column vector inputs.
+
 2008-06-02  John W. Eaton  <jwe@octave.org>
 
 	* general/mod.m: Delete bogus test.
--- a/scripts/time/eomday.m	Tue Jun 03 07:16:58 2008 +0200
+++ b/scripts/time/eomday.m	Fri Feb 01 06:34:12 2008 +0000
@@ -19,7 +19,7 @@
 ## -*- texinfo -*-
 ## @deftypefn {Function File} {@var{e} =} 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 <pkienzle@users.sf.net>
@@ -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;
--- a/scripts/time/weekday.m	Tue Jun 03 07:16:58 2008 +0200
+++ b/scripts/time/weekday.m	Fri Feb 01 06:34:12 2008 +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 <williampoetra@gmail.com>
 
-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];
+  endif
+  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 ())