Skip to content

Commit

Permalink
fix split for negative whole day deltas (#138)
Browse files Browse the repository at this point in the history
* fix split for negative whole day deltas

* mority's suggestion implemented
#138 (comment)
  • Loading branch information
Schischeck authored Oct 15, 2024
1 parent 3b66452 commit e02e54c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
11 changes: 2 additions & 9 deletions include/nigiri/common/delta_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,8 @@ inline std::pair<day_idx_t, minutes_after_midnight_t> split_day_mam(
day_idx_t const base, delta_t const x) {
assert(x != std::numeric_limits<delta_t>::min());
assert(x != std::numeric_limits<delta_t>::max());
if (x < 0) {
auto const t = -x / 1440 + 1;
auto const min = x + (t * 1440);
return {static_cast<day_idx_t>(static_cast<int>(to_idx(base)) - t),
minutes_after_midnight_t{min}};
} else {
return {static_cast<day_idx_t>(static_cast<int>(to_idx(base)) + x / 1440),
minutes_after_midnight_t{x % 1440}};
}
auto const minutes = base.v_ * 1440 + x;
return {day_idx_t{minutes / 1440}, minutes_after_midnight_t{minutes % 1440}};
}

} // namespace nigiri
33 changes: 33 additions & 0 deletions test/delta_t_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "gtest/gtest.h"

#include "nigiri/common/delta_t.h"

using namespace nigiri;

TEST(DeltaT, NegDeltaValue) {
{
auto [day, mam] = split_day_mam(day_idx_t{10}, delta_t{0});
EXPECT_EQ(day, day_idx_t{10});
EXPECT_EQ(mam.count(), 0);
}
{
auto [day, mam] = split_day_mam(day_idx_t{10}, delta_t{1441});
EXPECT_EQ(day, day_idx_t{11});
EXPECT_EQ(mam.count(), 1);
}
{
auto [day, mam] = split_day_mam(day_idx_t{10}, delta_t{1440});
EXPECT_EQ(day, day_idx_t{11});
EXPECT_EQ(mam.count(), 0);
}
{
auto [day, mam] = split_day_mam(day_idx_t{10}, delta_t{-1441});
EXPECT_EQ(day, day_idx_t{8});
EXPECT_EQ(mam.count(), 1439);
}
{
auto [day, mam] = split_day_mam(day_idx_t{10}, delta_t{-1440});
EXPECT_EQ(day, day_idx_t{9});
EXPECT_EQ(mam.count(), 0);
}
}

0 comments on commit e02e54c

Please sign in to comment.