5687
|
1 ## Copyright (C) 2000, 2001, 2004, 2005 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 2, or (at your option) |
|
8 ## 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, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {[@var{n}, @var{s}] =} weekday (@var{d}, [@var{form}]) |
|
22 ## Return the day of week as a number in @var{n} and a string in @var{s}, |
|
23 ## for example @code{[1, "Sun"]}, @code{[2, "Mon"]}, @dots{}, or |
|
24 ## @code{[7, "Sat"]}. |
|
25 ## |
|
26 ## @var{d} is a serial date number or a date string. |
|
27 ## |
|
28 ## If the string @var{form} is given and is @code{"long"}, @var{s} will |
|
29 ## contain the full name of the weekday; otherwise (or if @var{form} is |
|
30 ## @code{"short"}), @var{s} will contain the abbreviated name of the weekday. |
|
31 ## @seealso{datenum, datevec, eomday} |
|
32 ## @end deftypefn |
|
33 |
|
34 ## Author: pkienzle <pkienzle@users.sf.net> |
|
35 ## Created: 10 October 2001 (CVS) |
|
36 ## Adapted-By: William Poetra Yoga Hadisoeseno <williampoetra@gmail.com> |
|
37 |
|
38 function [n, s] = weekday (d, form) |
|
39 |
|
40 if (nargin < 1 || nargin > 2) |
|
41 usage("[n, s] = weekday (d, [form])"); |
|
42 endif |
|
43 |
|
44 if (nargin < 2) |
|
45 form = "short"; |
|
46 endif |
|
47 |
|
48 v = datevec (d); |
|
49 t = strptime (sprintf ("%d-%d-%d", v(3), v(2), v(1)), "%d-%m-%Y"); |
|
50 |
|
51 n = t.wday + 1; |
|
52 |
|
53 if (nargout > 1) |
|
54 if (strcmpi (form, "long")) |
|
55 s = strftime ("%A", t); |
|
56 else |
|
57 s = strftime ("%a", t); |
|
58 endif |
|
59 endif |
|
60 |
|
61 endfunction |
|
62 |
|
63 # tests |
|
64 %!assert(weekday(728647),2) |
|
65 %!assert(weekday('19-Dec-1994'),2) |
|
66 # demos |
|
67 %!demo |
|
68 %! [n, s] = weekday (now ()) |
|
69 %!demo |
|
70 %! [n, s] = weekday (728647) |
|
71 %!demo |
|
72 %! [n, s] = weekday ('19-Dec-1994') |