Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mixed-type duration math: addition of calyears or calmonths with days, hours, etc. #136

Open
Tracked by #137
jgpallero opened this issue Jul 7, 2024 · 6 comments
Assignees
Labels
bug Something isn't working chrono The date/time stuff
Milestone

Comments

@jgpallero
Copy link

I can see that duration objects and calendarDuration objects can't be added or substracted

>> calyears(8)+hours(8)
error: Invalid input: B must be numeric or calendarDuration; got a duration
error: called from
    plus at line 248 column 9

In Matlab this kind of operation works and the result can be used as increment with the colon operator in order to create sequences. Is this a bug in tablicious or a feature?

@apjanke
Copy link
Owner

apjanke commented Jul 7, 2024

In Matlab this kind of operation works and the result can be used as increment with the colon operator in order to create sequences. Is this a bug in tablicious or a feature?

It's a bug. Tablicious's date/time support is only like 60% complete, with respect to Matlab's full functionality. :)

I'll prioritize adding this in, now that I know someone actually wants to use it. Can probably have it done and out in a new Tablicious 0.4.4 release by, say, Thursday?

Diagnosis

I think the issue here is that addition is defined for the duration and calendarDuration classes, but not mixed-type addition between them.

>> calyears(2) + calyears(2)
ans =
4y
>> calyears(2) + calmonths(3)
ans =
2y 3mo
>> class(hours(3))
ans = duration
>> hours(3) + hours(2)
ans =
05:00:00
>> hours(3) + minutes(2)
ans =
03:02:00

And calendarDuration has a times method, but I missed the mtimes variant.

>> calyears(3) * 3
error: mtimes method not defined for calendarDuration class
>> calyears(3) .* 3
ans =
9y
>> hours(3) * 3
ans =
09:00:00
>> hours(3) .* 3
ans =
09:00:00
>>

Both of these fixes should be pretty straightforward. Might be complicated a bit by the need to support mixed signs between the different fields, and handle how calendar years and months get normalized between each other.

@apjanke apjanke self-assigned this Jul 7, 2024
@apjanke apjanke added bug Something isn't working chrono The date/time stuff labels Jul 7, 2024
@github-project-automation github-project-automation bot moved this to Needs triage in Octave-Tablicious Jul 7, 2024
@apjanke apjanke moved this from Needs triage to High priority in Octave-Tablicious Jul 7, 2024
@apjanke apjanke added this to the 0.4.4 milestone Jul 7, 2024
@apjanke apjanke changed the title [Question] Addition of calyears or calmonths with days, hours, etc. Mixed-type duration addition of calyears or calmonths with days, hours, etc. Jul 7, 2024
@apjanke apjanke changed the title Mixed-type duration addition of calyears or calmonths with days, hours, etc. Mixed-type duration math: addition of calyears or calmonths with days, hours, etc. Jul 7, 2024
@apjanke apjanke mentioned this issue Jul 7, 2024
5 tasks
@jgpallero
Copy link
Author

For completeness, in calendarDuration class there is not defined the mtimes method in order to multiply calyears and calmonths times a scalar. This operation is defined in Matlab

@apjanke
Copy link
Owner

apjanke commented Jul 10, 2024

That's in now, in 3f5fbc7 on branch caldur-math-fixes.

>> calyears(2) * 3
ans =
6y
>> calmonths(2) * 3
ans =
6mo
>>

@apjanke
Copy link
Owner

apjanke commented Jul 11, 2024

Here's a fix for mixed-type duration + calendarDuration arithmetic, I think: b8e3745

>> calmonths(1) + days(3.4)
ans =
1mo 3 days 09:36:00
>> days(3.4) + calmonths(1)
ans =
1mo 3 days 09:36:00
>>

@jgpallero
Copy link
Author

Thank you very much for your (date)time :)

@apjanke
Copy link
Owner

apjanke commented Jul 11, 2024

LOL. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working chrono The date/time stuff
Projects
Status: High priority
Development

No branches or pull requests

2 participants