7434
|
1 ## Copyright (C) 2000, 2001, 2004, 2005, 2006, 2007, 2008 Paul Kienzle |
5687
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
5687
|
9 ## |
|
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. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
5687
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {[@var{n}, @var{s}] =} weekday (@var{d}, [@var{form}]) |
|
21 ## Return the day of week as a number in @var{n} and a string in @var{s}, |
|
22 ## for example @code{[1, "Sun"]}, @code{[2, "Mon"]}, @dots{}, or |
|
23 ## @code{[7, "Sat"]}. |
|
24 ## |
|
25 ## @var{d} is a serial date number or a date string. |
|
26 ## |
|
27 ## If the string @var{form} is given and is @code{"long"}, @var{s} will |
|
28 ## contain the full name of the weekday; otherwise (or if @var{form} is |
|
29 ## @code{"short"}), @var{s} will contain the abbreviated name of the weekday. |
|
30 ## @seealso{datenum, datevec, eomday} |
|
31 ## @end deftypefn |
|
32 |
|
33 ## Author: pkienzle <pkienzle@users.sf.net> |
|
34 ## Created: 10 October 2001 (CVS) |
|
35 ## Adapted-By: William Poetra Yoga Hadisoeseno <williampoetra@gmail.com> |
|
36 |
7434
|
37 function [d, s] = weekday (d, form) |
5687
|
38 |
|
39 if (nargin < 1 || nargin > 2) |
5823
|
40 print_usage (); |
5687
|
41 endif |
|
42 |
|
43 if (nargin < 2) |
|
44 form = "short"; |
|
45 endif |
|
46 |
7434
|
47 if (iscell (d) || isnumeric (d)) |
|
48 endsize = size (d); |
|
49 elseif (ischar (d)) |
|
50 endsize = [size(d, 1), 1]; |
|
51 endif |
|
52 if (ischar (d) || iscell (d)) |
|
53 ## Make sure the date is numeric |
|
54 d = datenum (d); |
|
55 endif |
|
56 ## Find the offset from a known Sunday (2008-Jan-6), mod 7. |
|
57 d = floor (reshape (mod(d - 733048, 7), endsize)); |
|
58 ## Make Saturdays a 7 and not a 0. |
|
59 d(!d) = 7; |
5687
|
60 |
|
61 if (nargout > 1) |
|
62 if (strcmpi (form, "long")) |
7434
|
63 names = {"Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" |
|
64 "Friday" "Saturday"}; |
5687
|
65 else |
7434
|
66 names = {"Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat"}; |
5687
|
67 endif |
7434
|
68 s = strvcat (names(d)); |
5687
|
69 endif |
|
70 |
|
71 endfunction |
|
72 |
|
73 # tests |
|
74 %!assert(weekday(728647),2) |
7434
|
75 ## Test vector inputs for both directions |
|
76 %!assert(weekday([728647 728648]), [2 3]) |
|
77 %!assert(weekday([728647;728648]), [2;3]) |
|
78 ## Test a full week before our reference day |
5687
|
79 %!assert(weekday('19-Dec-1994'),2) |
7434
|
80 %!assert(weekday('20-Dec-1994'),3) |
|
81 %!assert(weekday('21-Dec-1994'),4) |
|
82 %!assert(weekday('22-Dec-1994'),5) |
|
83 %!assert(weekday('23-Dec-1994'),6) |
|
84 %!assert(weekday('24-Dec-1994'),7) |
|
85 %!assert(weekday('25-Dec-1994'),1) |
|
86 ## Test our reference day |
|
87 %!assert(weekday('6-Jan-2008'),1) |
|
88 ## Test a full week after our reference day |
|
89 %!assert(weekday('1-Feb-2008'),6) |
|
90 %!assert(weekday('2-Feb-2008'),7) |
|
91 %!assert(weekday('3-Feb-2008'),1) |
|
92 %!assert(weekday('4-Feb-2008'),2) |
|
93 %!assert(weekday('5-Feb-2008'),3) |
|
94 %!assert(weekday('6-Feb-2008'),4) |
|
95 %!assert(weekday('7-Feb-2008'),5) |
|
96 ## Test fractional dates |
|
97 %!assert(weekday(728647.1),2) |
5687
|
98 # demos |
|
99 %!demo |
|
100 %! [n, s] = weekday (now ()) |
|
101 %!demo |
|
102 %! [n, s] = weekday (728647) |
|
103 %!demo |
|
104 %! [n, s] = weekday ('19-Dec-1994') |