Mercurial > hg > octave-lyh
annotate scripts/time/weekday.m @ 13856:d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
* addtodate.m: Add millisecond functionality. Update docstring and %!tests.
* calendar.m: Implement faster way to add '*' to day display. Update docstring.
* weekday.m: Use more modern coding stytle. Update docstring.
* asctime.m, clock.m, ctime.m, date.m, datenum.m, datestr.m, datevec.m,
eomday.m, etime.m, is_leap_year.m: Update docstring and/or use Octave formatting
spacing conventions for %!tests.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Thu, 10 Nov 2011 13:35:08 -0800 |
parents | 796dc1d75e06 |
children | 050bc580cb60 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2000-2011 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}) |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
22 ## Return the day of the week as a number in @var{n} and as a string in @var{s}. |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
23 ## The days of the week are numbered 1--7 with the first day being Sunday. |
5687 | 24 ## |
25 ## @var{d} is a serial date number or a date string. | |
26 ## | |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
27 ## If the string @var{format} is not present or is equal to "short" then |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
28 ## @var{s} will contain the abbreviated name of the weekday. If @var{format} |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
29 ## is "long" then @var{s} will contain the full name. |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
30 ## |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
31 ## Table of return values based on @var{format}: |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
32 ## |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
33 ## @multitable @columnfractions .06 .13 .13 |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
34 ## @headitem @var{n} @tab "short" @tab "long" |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
35 ## @item 1 @tab Sun @tab Sunday |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
36 ## @item 2 @tab Mon @tab Monday |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
37 ## @item 3 @tab Tue @tab Tuesday |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
38 ## @item 4 @tab Wed @tab Wednesday |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
39 ## @item 5 @tab Thu @tab Thursday |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
40 ## @item 6 @tab Fri @tab Friday |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
41 ## @item 7 @tab Sat @tab Saturday |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
42 ## @end multitable |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
43 ## |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
44 ## @seealso{eomday, is_leap_year, calendar, datenum, datevec} |
5687 | 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 | |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
51 function [d, s] = weekday (d, format = "short") |
5687 | 52 |
53 if (nargin < 1 || nargin > 2) | |
5823 | 54 print_usage (); |
5687 | 55 endif |
56 | |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
57 if (iscellstr (d) || isnumeric (d)) |
7434 | 58 endsize = size (d); |
59 elseif (ischar (d)) | |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
60 endsize = [rows(d), 1]; |
7434 | 61 endif |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
62 if (ischar (d) || iscellstr (d)) |
7434 | 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. | |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
67 d = floor (reshape (mod (d - 733048, 7), endsize)); |
7434 | 68 ## Make Saturdays a 7 and not a 0. |
69 d(!d) = 7; | |
5687 | 70 |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
71 if (isargout (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
|
72 if (strcmpi (format, "long")) |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
73 names = {"Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" ... |
10549 | 74 "Friday" "Saturday"}; |
5687 | 75 else |
7434 | 76 names = {"Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat"}; |
5687 | 77 endif |
7434 | 78 s = strvcat (names(d)); |
5687 | 79 endif |
80 | |
81 endfunction | |
82 | |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
83 |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
84 %!demo |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
85 %! ## Current weekday |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
86 %! [n, s] = weekday (now ()) |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
87 %!demo |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
88 %! ## Weekday from datenum input |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
89 %! [n, s] = weekday (728647) |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
90 %!demo |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
91 %! ## Weekday of new millennium from datestr input |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
92 %! [n, s] = weekday ('1-Jan-2000') |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
93 |
5687 | 94 # tests |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
95 %!assert (weekday (728647), 2) |
7434 | 96 ## Test vector inputs for both directions |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
97 %!assert (weekday ([728647 728648]), [2 3]) |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
98 %!assert (weekday ([728647;728648]), [2;3]) |
7434 | 99 ## Test a full week before our reference day |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
100 %!assert (weekday ("19-Dec-1994"), 2) |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
101 %!assert (weekday ("20-Dec-1994"), 3) |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
102 %!assert (weekday ("21-Dec-1994"), 4) |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
103 %!assert (weekday ("22-Dec-1994"), 5) |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
104 %!assert (weekday ("23-Dec-1994"), 6) |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
105 %!assert (weekday ("24-Dec-1994"), 7) |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
106 %!assert (weekday ("25-Dec-1994"), 1) |
7434 | 107 ## Test our reference day |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
108 %!assert (weekday ("6-Jan-2008"), 1) |
7434 | 109 ## Test a full week after our reference day |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
110 %!assert (weekday ("1-Feb-2008"), 6) |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
111 %!assert (weekday ("2-Feb-2008"), 7) |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
112 %!assert (weekday ("3-Feb-2008"), 1) |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
113 %!assert (weekday ("4-Feb-2008"), 2) |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
114 %!assert (weekday ("5-Feb-2008"), 3) |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
115 %!assert (weekday ("6-Feb-2008"), 4) |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
116 %!assert (weekday ("7-Feb-2008"), 5) |
7434 | 117 ## Test fractional dates |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
118 %!assert (weekday (728647.1), 2) |
13170
796dc1d75e06
Fix incorrect newline in weekday.m (bug #34347)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
11523
diff
changeset
|
119 ## Test "long" option |
796dc1d75e06
Fix incorrect newline in weekday.m (bug #34347)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
11523
diff
changeset
|
120 %!test |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
121 %! [n, s] = weekday ("25-Dec-1994", "long"); |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
122 %! assert (n, 1); |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13170
diff
changeset
|
123 %! assert (s, "Sunday"); |
13170
796dc1d75e06
Fix incorrect newline in weekday.m (bug #34347)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
11523
diff
changeset
|
124 |