Skip to content

Commit

Permalink
Increase DateTicks to 64 bits, fix overflow after year 79455
Browse files Browse the repository at this point in the history
  • Loading branch information
JGRennison committed Jan 8, 2024
1 parent bd35b0c commit 1965285
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/date_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ inline constexpr Year DateDeltaToYears(DateDelta date)

inline constexpr DateTicks DateToDateTicks(Date date, DateFract fract = 0)
{
return (date.base() * DAY_TICKS) + fract;
return ((int64_t)date.base() * DAY_TICKS) + fract;
}

inline constexpr DateTicksDelta DateDeltaToDateTicksDelta(DateDelta date, DateFract fract = 0)
{
return (date.base() * DAY_TICKS) + fract;
return ((int64_t)date.base() * DAY_TICKS) + fract;
}

inline DateTicks NowDateTicks()
Expand Down
4 changes: 2 additions & 2 deletions src/date_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ struct DateTicksOperations {
};

/* The type to store dates in when tick-precision is required */
using DateTicksDelta = StrongType::Typedef<int32_t, struct DateTicksDeltaTag, StrongType::Compare, StrongType::IntegerScalable>;
using DateTicks = StrongType::Typedef<int32_t, struct DateTicksTag, StrongType::Compare, StrongType::IntegerDelta<DateTicksDelta>, DateTicksOperations>;
using DateTicksDelta = StrongType::Typedef<int64_t, struct DateTicksDeltaTag, StrongType::Compare, StrongType::IntegerScalable>;
using DateTicks = StrongType::Typedef<int64_t, struct DateTicksTag, StrongType::Compare, StrongType::IntegerDelta<DateTicksDelta>, DateTicksOperations>;

/* Mixin for DateTicksScaledDelta */
struct DateTicksScaledDeltaOperations {
Expand Down
2 changes: 1 addition & 1 deletion src/linkgraph/linkgraphschedule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ void LinkGraphJobGroup::JoinThread()
DateTicks bucket_join_date = 0;
auto flush_bucket = [&]() {
if (!bucket_cost) return;
DEBUG(linkgraph, 2, "LinkGraphJobGroup::ExecuteJobSet: Creating Job Group: jobs: " PRINTF_SIZE ", cost: %u, join after: %d",
DEBUG(linkgraph, 2, "LinkGraphJobGroup::ExecuteJobSet: Creating Job Group: jobs: " PRINTF_SIZE ", cost: %u, join after: " OTTD_PRINTF64,
bucket.size(), bucket_cost, (bucket_join_date - NowDateTicks()).base());
auto group = std::make_shared<LinkGraphJobGroup>(constructor_token(), std::move(bucket));
group->SpawnThread();
Expand Down
2 changes: 1 addition & 1 deletion src/saveload/linkgraph_sl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ SaveLoadTable GetLinkGraphJobDesc()
SLE_VAR2(LinkGraphJob, "linkgraph.demand_size", settings.demand_size, SLE_UINT8),
SLE_VAR2(LinkGraphJob, "linkgraph.short_path_saturation", settings.short_path_saturation, SLE_UINT8),

SLE_VAR2(LinkGraphJob, "join_date", join_date_ticks, SLE_INT32),
SLE_VAR2(LinkGraphJob, "join_date", join_date_ticks, SLE_FILE_I32 | SLE_VAR_I64),
SLE_VAR(LinkGraphJob, link_graph.index, SLE_UINT16),
SLEG_STRUCT("linkgraph", SlLinkgraphJobProxy),
};
Expand Down
2 changes: 1 addition & 1 deletion src/sl/extended_ver_sl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const SlxiSubChunkInfo _sl_xv_sub_chunk_infos[] = {
{ XSLFI_EXTRA_LARGE_MAP, XSCF_NULL, 0, 1, "extra_large_map", nullptr, nullptr, nullptr },
{ XSLFI_REVERSE_AT_WAYPOINT, XSCF_NULL, 1, 1, "reverse_at_waypoint", nullptr, nullptr, nullptr },
{ XSLFI_VEH_LIFETIME_PROFIT, XSCF_NULL, 1, 1, "veh_lifetime_profit", nullptr, nullptr, nullptr },
{ XSLFI_LINKGRAPH_DAY_SCALE, XSCF_NULL, 4, 4, "linkgraph_day_scale", nullptr, nullptr, nullptr },
{ XSLFI_LINKGRAPH_DAY_SCALE, XSCF_NULL, 5, 5, "linkgraph_day_scale", nullptr, nullptr, nullptr },
{ XSLFI_TEMPLATE_REPLACEMENT, XSCF_NULL, 9, 9, "template_replacement", nullptr, nullptr, "TRPL,TMPL" },
{ XSLFI_MORE_RAIL_TYPES, XSCF_NULL, 0, 1, "more_rail_types", nullptr, nullptr, nullptr },
{ XSLFI_CARGO_TYPE_ORDERS, XSCF_NULL, 3, 3, "cargo_type_orders", nullptr, nullptr, "ORDX,VEOX" },
Expand Down
6 changes: 4 additions & 2 deletions src/sl/linkgraph_sl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ SaveLoadTable GetLinkGraphJobDesc()
}

const SaveLoad job_desc[] = {
SLE_VAR(LinkGraphJob, join_date_ticks, SLE_INT32),
SLE_CONDVAR_X(LinkGraphJob, start_date_ticks, SLE_INT32, SL_MIN_VERSION, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_LINKGRAPH_DAY_SCALE)),
SLE_CONDVAR_X(LinkGraphJob, join_date_ticks, SLE_FILE_I32 | SLE_VAR_I64, SL_MIN_VERSION, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_LINKGRAPH_DAY_SCALE, 0, 4)),
SLE_CONDVAR_X(LinkGraphJob, join_date_ticks, SLE_INT64, SL_MIN_VERSION, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_LINKGRAPH_DAY_SCALE, 5)),
SLE_CONDVAR_X(LinkGraphJob, start_date_ticks, SLE_FILE_I32 | SLE_VAR_I64, SL_MIN_VERSION, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_LINKGRAPH_DAY_SCALE, 1, 4)),
SLE_CONDVAR_X(LinkGraphJob, start_date_ticks, SLE_INT64, SL_MIN_VERSION, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_LINKGRAPH_DAY_SCALE, 5)),
SLE_VAR(LinkGraphJob, link_graph.index, SLE_UINT16),
};

Expand Down

0 comments on commit 1965285

Please sign in to comment.