1 ## Copyright (C) 2000-2012 Paul Kienzle
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {[@var{n}, @var{s}] =} weekday (@var{d})
21 ## @deftypefnx {Function File} {[@var{n}, @var{s}] =} weekday (@var{d}, @var{format})
22 ## Return the day of the week as a number in @var{n} and as a string in @var{s}.
23 ## The days of the week are numbered 1--7 with the first day being Sunday.
25 ## @var{d} is a serial date number or a date string.
27 ## If the string @var{format} is not present or is equal to "short" then
28 ## @var{s} will contain the abbreviated name of the weekday. If @var{format}
29 ## is "long" then @var{s} will contain the full name.
31 ## Table of return values based on @var{format}:
33 ## @multitable @columnfractions .06 .13 .16
34 ## @headitem @var{n} @tab "short" @tab "long"
35 ## @item 1 @tab Sun @tab Sunday
36 ## @item 2 @tab Mon @tab Monday
37 ## @item 3 @tab Tue @tab Tuesday
38 ## @item 4 @tab Wed @tab Wednesday
39 ## @item 5 @tab Thu @tab Thursday
40 ## @item 6 @tab Fri @tab Friday
41 ## @item 7 @tab Sat @tab Saturday
44 ## @seealso{eomday, is_leap_year, calendar, datenum, datevec}
47 ## Author: pkienzle <pkienzle@users.sf.net>
48 ## Created: 10 October 2001 (CVS)
49 ## Adapted-By: William Poetra Yoga Hadisoeseno <williampoetra@gmail.com>
51 function [d, s] = weekday (d, format = "short")
53 if (nargin < 1 || nargin > 2)
57 if (iscellstr (d) || isnumeric (d))
60 endsize = [rows(d), 1];
62 if (ischar (d) || iscellstr (d))
63 ## Make sure the date is numeric
66 ## Find the offset from a known Sunday (2008-Jan-6), mod 7.
67 d = floor (reshape (mod (d - 733048, 7), endsize));
68 ## Make Saturdays a 7 and not a 0.
72 if (strcmpi (format, "long"))
73 names = {"Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" ...
76 names = {"Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat"};
78 s = strvcat (names(d));
86 %! [n, s] = weekday (now ())
88 %! ## Weekday from datenum input
89 %! [n, s] = weekday (728647)
91 %! ## Weekday of new millennium from datestr input
92 %! [n, s] = weekday ('1-Jan-2000')
95 %!assert (weekday (728647), 2)
96 ## Test vector inputs for both directions
97 %!assert (weekday ([728647 728648]), [2 3])
98 %!assert (weekday ([728647;728648]), [2;3])
99 ## Test a full week before our reference day
100 %!assert (weekday ("19-Dec-1994"), 2)
101 %!assert (weekday ("20-Dec-1994"), 3)
102 %!assert (weekday ("21-Dec-1994"), 4)
103 %!assert (weekday ("22-Dec-1994"), 5)
104 %!assert (weekday ("23-Dec-1994"), 6)
105 %!assert (weekday ("24-Dec-1994"), 7)
106 %!assert (weekday ("25-Dec-1994"), 1)
107 ## Test our reference day
108 %!assert (weekday ("6-Jan-2008"), 1)
109 ## Test a full week after our reference day
110 %!assert (weekday ("1-Feb-2008"), 6)
111 %!assert (weekday ("2-Feb-2008"), 7)
112 %!assert (weekday ("3-Feb-2008"), 1)
113 %!assert (weekday ("4-Feb-2008"), 2)
114 %!assert (weekday ("5-Feb-2008"), 3)
115 %!assert (weekday ("6-Feb-2008"), 4)
116 %!assert (weekday ("7-Feb-2008"), 5)
117 ## Test fractional dates
118 %!assert (weekday (728647.1), 2)
119 ## Test "long" option
121 %! [n, s] = weekday ("25-Dec-1994", "long");
123 %! assert (s, "Sunday");