Skip to content

calendarDuration colon operator bad results: over hi bound, wrong month-end normalization, etc. #135

Description

@jgpallero

See this example:

>> fecha1 = datetime([2024 2 28 21 0 15]);
>> fecha2 = datetime([2024 3 2 21 0 15]);
>> vec2 = fecha1:2:fecha2
vec2 =
28-Feb-2024 21:00:15   01-Mar-2024 21:00:15
>> incd = days(2);
>> vec2 = fecha1:incd:fecha2
vec2 =
28-Feb-2024 21:00:15   01-Mar-2024 21:00:15

When I create a sequential vector of dates using the colon operator all works apparently well as the vector stops in an element lower than the last date.

But when I use years the result is not correct:

>> fecha1 = datetime([2020 2 29 21 0 15]);
>> fecha2 = datetime([2025 1 1 0 0 0]);
>> incy = calyears(2);
>> vec1 = fecha1:incy:fecha2
vec1 =
29-Feb-2020 21:00:15   01-Mar-2022 21:00:15   01-Mar-2024 21:00:15   01-Mar-2026 21:00:15

in Matlab the result is

>> fecha1 = datetime([2020 2 29 21 0 15]);
>> fecha2 = datetime([2025 1 1 0 0 0]);
>> incy = calyears(2);
>> vec1 = fecha1:incy:fecha2
vec1 = 
  1×3 datetime array
   29-Feb-2020 21:00:15   28-Feb-2022 21:00:15   29-Feb-2024 21:00:15

the last element is always <= the greater limit. The bug is also present using calmonths()

I don't know if this bug is related with #134


Andrew's notes

Concise repro code:

datetime([2020 2 29 21 0 15]):calyears(2):datetime([2025 1 1 0 0 0])

TODO

  • Handle inverse hi:inc:lo case for negative inc values. Currently just returns an empty array.
  • Handle case where lo:inc:hi produces a descending sequence of values, and vice versa for hi:inc:lo giving ascending values.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingchronoThe date/time stuff

Projects

Status
High priority

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions