Skip to content

Commit be17e67

Browse files
committed
add datevec() for duration and calendarDuration; add multi-argout form for datetime.datevec() (#153)
Also factor out duration's fixed size year size to a duration.FixedSizeYearDays constant property.
1 parent 37ff4d8 commit be17e67

4 files changed

Lines changed: 87 additions & 4 deletions

File tree

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Tablicious Changelog
55
------------------
66

77
* Date/time fixes
8+
* Add datevec() methods for duration and calendarDuration; add multi-argout form for datetime.datevec(). ([#153](https://github.com/apjanke/octave-tablicious/issues/153))
89
* Add duration.hms() method.
910

1011
0.4.6 (2026-02-16)

inst/@calendarDuration/calendarDuration.m

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,36 @@
163163
out = this.Months;
164164
endfunction
165165

166+
## -*- texinfo -*-
167+
## @node calendarDuration.datevec
168+
## @deftypefn {Method} {[@var{dvec}] =} datevec (@var{obj})
169+
## @deftypefnx {Method} {[@var{Y}, @var{M}, @var{D}, @var{H}, @var{MN}, @var{S}] =} datevec (@var{obj})
170+
##
171+
## Convert a @code{calendarDuration} to a datevec whose elements represent its
172+
## components of. The components are the year, month, day, hour, minute, and
173+
## second quantities of this @code{calendarDuration}.
174+
##
175+
## When nargout is 0 or 1, returns a double array of size n-by-6, where n is
176+
## @code{numel(obj)}. When nargout is 2 or more, returns double arrays the
177+
## same size as @var{obj}.
178+
##
179+
## @end deftypefn
180+
function [out, M, D, H, MN, S] = datevec (this)
181+
yrs = this.Months / 12;
182+
Y = fix (yrs);
183+
M = this.Months - (Y * 12);
184+
D = this.Days;
185+
x = this.Time;
186+
[H, x] = deal (fix (x * 24), rem (x * 24, 1));
187+
[MN, x] = deal (fix (x * 60), rem (x * 60, 1));
188+
S = x * 60;
189+
if nargout < 2
190+
out = [Y(:) M(:) D(:) H(:) MN(:) S(:)];
191+
else
192+
out = Y;
193+
end
194+
endfunction
195+
166196
# Arithmetic
167197

168198
## -*- texinfo -*-

inst/@datetime/datetime.m

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -792,15 +792,22 @@ function disp (this)
792792
## -*- texinfo -*-
793793
## @node datetime.datevec
794794
## @deftypefn {Method} {@var{out} =} datevec (@var{obj})
795+
## @deftypefnx {Method} {[@var{Y}, @var{M}, @var{D}, @var{H}, @var{MN}, @var{S}] =} datevec (@var{obj})
795796
##
796797
## Convert this to a datevec that represent the same local wall time.
797798
##
798-
## Returns double array of size [numel(obj) 6].
799+
## When nargout is 0 or 1, returns a double array of size n-by-6, where n is
800+
## @code{numel(obj)}. When nargout is 2 or more, returns double arrays the
801+
## same size as @var{obj}.
799802
##
800803
## @end deftypefn
801-
function out = datevec (this)
804+
function [out, M, D, H, MN, S] = datevec (this)
802805
dn = datenum (this);
803-
out = datevec (dn(:));
806+
if nargout < 2
807+
out = datevec (this.dnum(:));
808+
else
809+
[out, M, D, H, MN, S] = datevec (this.dnum);
810+
endif
804811
endfunction
805812

806813
## -*- texinfo -*-

inst/@duration/duration.m

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@
5858
# Display format (currently unsupported)
5959
Format = 'hh:mm:ss'
6060
endproperties
61+
properties (Constant)
62+
# Size of fixed-size years, as a number of days, including a fractional part.
63+
# Equal to 365.2425.
64+
FixedSizeYearDays = 365.2425
65+
endproperties
6166

6267
methods (Static)
6368
## -*- texinfo -*-
@@ -171,7 +176,7 @@
171176
## @end deftypefn
172177
function out = years (this)
173178
#YEARS Number of fixed-length years equivalent to this.
174-
out = this.days / 365.2425;
179+
out = this.days / duration.FixedSizeYearDays;
175180
endfunction
176181

177182
# Can't have a days() function as well as a days property or it will cause Octave to crash
@@ -271,6 +276,46 @@
271276
s = rem(minutes, 1) * 60;
272277
endfunction
273278

279+
## -*- texinfo -*-
280+
## @node duration.datevec
281+
## @deftypefn {Method} {[@var{dvec}] =} datevec (@var{obj})
282+
## @deftypefnx {Method} {[@var{Y}, @var{M}, @var{D}, @var{H}, @var{MN}, @var{S}] =} datevec (@var{obj})
283+
##
284+
## Convert @var{obj} to a datevec whose elements represent the components of
285+
## this duration. The components are the year, month, days, hour, minute, and
286+
## second quantities of this duration. The years are in terms of fixed size
287+
## years that are 365.2425 days long, and days are fixed size 24-hour days.
288+
## The fixed size year size can be found in the FixedSizeYearDays constant
289+
## class property on the duration class. The month component is always zero,
290+
## since months are variable lengths and there is no equivalent fixed size
291+
## month. Thus, the days component may be more than 31.
292+
##
293+
## When nargout is 0 or 1, returns a double array of size n-by-6, where n is
294+
## @code{numel(obj)}. When nargout is 2 or more, returns double arrays the
295+
## same size as @var{obj}.
296+
##
297+
## @end deftypefn
298+
function [out, M, D, H, MN, S] = datevec (this)
299+
x = this.days;
300+
x = x / duration.FixedSizeYearDays;
301+
[Y, x] = deal (fix (x), rem (x, 1));
302+
x = x * duration.FixedSizeYearDays;
303+
M = zeros (size (this));
304+
[D, x] = deal (fix (x), rem (x, 1));
305+
x = x * 24;
306+
[H, x] = deal (fix (x), rem (x, 1));
307+
x = x * 60;
308+
[MN, x] = deal (fix (x), rem (x, 1));
309+
x = x * 60;
310+
S = x;
311+
if nargout < 2
312+
out = [Y(:) M(:) D(:) H(:) MN(:) S(:)];
313+
else
314+
out = Y;
315+
% Other argouts are already in correct format
316+
endif
317+
endfunction
318+
274319
function [keysA, keysB] = proxyKeys (a, b)
275320
#PROXYKEYS Proxy key values for sorting and set operations
276321
keysA = a.days(:);

0 commit comments

Comments
 (0)