7017
|
1 ## Copyright (C) 2004, 2006, 2007 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} {} calendar (@dots{}) |
|
21 ## @deftypefnx {Function File} {@var{c} =} calendar () |
|
22 ## @deftypefnx {Function File} {@var{c} =} calendar (@var{d}) |
|
23 ## @deftypefnx {Function File} {@var{c} =} calendar (@var{y}, @var{m}) |
|
24 ## If called with no arguments, return the current monthly calendar in |
|
25 ## a 6x7 matrix. |
|
26 ## |
|
27 ## If @var{d} is specified, return the calendar for the month containing |
|
28 ## the day @var{d}, which must be a serial date number or a date string. |
|
29 ## |
|
30 ## If @var{y} and @var{m} are specified, return the calendar for year @var{y} |
|
31 ## and month @var{m}. |
|
32 ## |
|
33 ## If no output arguments are specified, print the calendar on the screen |
|
34 ## instead of returning a matrix. |
|
35 ## @seealso{datenum} |
|
36 ## @end deftypefn |
|
37 |
|
38 ## Author: pkienzle <pkienzle@users.sf.net> |
|
39 ## Created: 25 July 2004 |
|
40 ## Adapted-By: William Poetra Yoga Hadisoeseno <williampoetra@gmail.com> |
|
41 |
|
42 function varargout = calendar (varargin) |
|
43 |
|
44 switch (nargin) |
|
45 case 0 |
|
46 v = clock (); |
|
47 y = v(1); |
|
48 m = v(2); |
|
49 d = v(3); |
|
50 case 1 |
|
51 v = datevec (varargin{1}); |
|
52 y = v(1); |
|
53 m = v(2); |
|
54 d = v(3); |
|
55 case 2 |
|
56 y = varargin{1}; |
|
57 m = varargin{2}; |
|
58 d = []; |
|
59 otherwise |
6046
|
60 print_usage (); |
5687
|
61 endswitch |
|
62 |
|
63 c = zeros (7, 6); |
|
64 dayone = datenum (y, m, 1); |
|
65 ndays = eomday (y, m); |
|
66 c(weekday (dayone) - 1 + [1:ndays]) = 1:ndays; |
|
67 |
|
68 if (nargout > 0) |
|
69 varargout{1} = c'; |
|
70 else |
|
71 ## Layout the calendar days, 6 columns per day, 7 days per row. |
|
72 str = sprintf (" %2d %2d %2d %2d %2d %2d %2d\n", c); |
|
73 |
|
74 ## Print an asterisk before the specified date |
|
75 if (! isempty (d) && d >= 1 && d <= ndays) |
|
76 pos = weekday (dayone) + d - 1; |
|
77 idx = 6 * (pos - 1) + floor (pos / 7) + 1; |
|
78 while (str(idx) == " ") |
|
79 ++idx; |
|
80 endwhile |
|
81 str(--idx) = "*"; |
|
82 endif |
|
83 |
|
84 ## Display the calendar. |
|
85 s.year = y - 1900; |
|
86 s.mon = m - 1; |
|
87 puts (strftime (" %b %Y\n", s)); |
|
88 puts (" S M Tu W Th F S\n"); |
|
89 puts (str); |
|
90 endif |
|
91 |
|
92 endfunction |
|
93 |
|
94 # tests |
|
95 %!assert((calendar(2000,2))'(2:31),[0:29]); |
|
96 %!assert((calendar(1957,10))'(2:33),[0:31]); |
|
97 # demos |
|
98 %!demo |
|
99 %! calendar () |
|
100 %!demo |
|
101 %! calendar (1957, 10) |