]> Creatis software - CreaPhase.git/blob - octave_packages/m/time/weekday.m
update packages
[CreaPhase.git] / octave_packages / m / time / weekday.m
1 ## Copyright (C) 2000-2012 Paul Kienzle
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
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
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
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
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.
24 ##
25 ## @var{d} is a serial date number or a date string.
26 ##
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.
30 ##
31 ## Table of return values based on @var{format}:
32 ##
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
42 ## @end multitable
43 ## 
44 ## @seealso{eomday, is_leap_year, calendar, datenum, datevec}
45 ## @end deftypefn
46
47 ## Author: pkienzle <pkienzle@users.sf.net>
48 ## Created: 10 October 2001 (CVS)
49 ## Adapted-By: William Poetra Yoga Hadisoeseno <williampoetra@gmail.com>
50
51 function [d, s] = weekday (d, format = "short")
52
53   if (nargin < 1 || nargin > 2)
54     print_usage ();
55   endif
56
57   if (iscellstr (d) || isnumeric (d))
58     endsize = size (d);
59   elseif (ischar (d))
60     endsize = [rows(d), 1];
61   endif
62   if (ischar (d) || iscellstr (d))
63     ## Make sure the date is numeric
64     d = datenum (d);
65   endif
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.
69   d(!d) = 7;
70
71   if (isargout (2))
72     if (strcmpi (format, "long"))
73       names = {"Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" ...
74                "Friday" "Saturday"};
75     else
76       names = {"Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat"};
77     endif
78     s = strvcat (names(d));
79   endif
80
81 endfunction
82
83
84 %!demo
85 %! ## Current weekday
86 %! [n, s] = weekday (now ())
87 %!demo
88 %! ## Weekday from datenum input
89 %! [n, s] = weekday (728647)
90 %!demo
91 %! ## Weekday of new millennium from datestr input
92 %! [n, s] = weekday ('1-Jan-2000')
93
94 # tests
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
120 %!test
121 %! [n, s] = weekday ("25-Dec-1994", "long");
122 %! assert (n, 1);
123 %! assert (s, "Sunday");
124