Mercurial > hg > octave-lyh
diff scripts/time/weekday.m @ 7434:600a3af7963e
[project @ 2008-02-01 06:32:06 by jwe]
author | jwe |
---|---|
date | Fri, 01 Feb 2008 06:34:12 +0000 |
parents | a1dbe9d80eee |
children | 95c3e38098bf |
line wrap: on
line diff
--- a/scripts/time/weekday.m +++ b/scripts/time/weekday.m @@ -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 ())