Mercurial > hg > octave-lyh
annotate scripts/time/weekday.m @ 11477:a02d00dd3d5f
expm.m: new tests
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 10 Jan 2011 14:50:33 -0500 |
parents | c776f063fefe |
children | fd0a3ac60b0e |
rev | line source |
---|---|
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 -*- | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
20 ## @deftypefn {Function File} {[@var{n}, @var{s}] =} weekday (@var{d}) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
21 ## @deftypefnx {Function File} {[@var{n}, @var{s}] =} weekday (@var{d}, @var{format}) |
5687 | 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 ## | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
28 ## If the string @var{format} is given and is @code{"long"}, @var{s} will |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
29 ## contain the full name of the weekday; otherwise (or if @var{format} is |
5687 | 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 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
38 function [d, s] = weekday (d, format) |
5687 | 39 |
40 if (nargin < 1 || nargin > 2) | |
5823 | 41 print_usage (); |
5687 | 42 endif |
43 | |
44 if (nargin < 2) | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
45 format = "short"; |
5687 | 46 endif |
47 | |
7434 | 48 if (iscell (d) || isnumeric (d)) |
49 endsize = size (d); | |
50 elseif (ischar (d)) | |
51 endsize = [size(d, 1), 1]; | |
52 endif | |
53 if (ischar (d) || iscell (d)) | |
54 ## Make sure the date is numeric | |
55 d = datenum (d); | |
56 endif | |
57 ## Find the offset from a known Sunday (2008-Jan-6), mod 7. | |
58 d = floor (reshape (mod(d - 733048, 7), endsize)); | |
59 ## Make Saturdays a 7 and not a 0. | |
60 d(!d) = 7; | |
5687 | 61 |
62 if (nargout > 1) | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
63 if (strcmpi (format, "long")) |
7434 | 64 names = {"Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" |
10549 | 65 "Friday" "Saturday"}; |
5687 | 66 else |
7434 | 67 names = {"Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat"}; |
5687 | 68 endif |
7434 | 69 s = strvcat (names(d)); |
5687 | 70 endif |
71 | |
72 endfunction | |
73 | |
74 # tests | |
75 %!assert(weekday(728647),2) | |
7434 | 76 ## Test vector inputs for both directions |
77 %!assert(weekday([728647 728648]), [2 3]) | |
78 %!assert(weekday([728647;728648]), [2;3]) | |
79 ## Test a full week before our reference day | |
5687 | 80 %!assert(weekday('19-Dec-1994'),2) |
7434 | 81 %!assert(weekday('20-Dec-1994'),3) |
82 %!assert(weekday('21-Dec-1994'),4) | |
83 %!assert(weekday('22-Dec-1994'),5) | |
84 %!assert(weekday('23-Dec-1994'),6) | |
85 %!assert(weekday('24-Dec-1994'),7) | |
86 %!assert(weekday('25-Dec-1994'),1) | |
87 ## Test our reference day | |
88 %!assert(weekday('6-Jan-2008'),1) | |
89 ## Test a full week after our reference day | |
90 %!assert(weekday('1-Feb-2008'),6) | |
91 %!assert(weekday('2-Feb-2008'),7) | |
92 %!assert(weekday('3-Feb-2008'),1) | |
93 %!assert(weekday('4-Feb-2008'),2) | |
94 %!assert(weekday('5-Feb-2008'),3) | |
95 %!assert(weekday('6-Feb-2008'),4) | |
96 %!assert(weekday('7-Feb-2008'),5) | |
97 ## Test fractional dates | |
98 %!assert(weekday(728647.1),2) | |
5687 | 99 # demos |
100 %!demo | |
101 %! [n, s] = weekday (now ()) | |
102 %!demo | |
103 %! [n, s] = weekday (728647) | |
104 %!demo | |
105 %! [n, s] = weekday ('19-Dec-1994') |