Changeset - r28343:242bc14cc675
[Not reviewed]
master
0 16 0
Peter Nelson - 11 months ago 2023-12-28 21:34:08
peter1138@openttd.org
Codechange: Simplify ConvertDateToYMD by returning YearMonthDay instead of outputting to a pointer. (#11637)
16 files changed with 31 insertions and 51 deletions:
0 comments (0 inline, 0 general)
src/build_vehicle_gui.cpp
Show inline comments
 
@@ -917,14 +917,13 @@ void TestedEngineDetails::FillDefaultCap
 
 * @param engine_number the engine of which to draw the info of
 
 * @return y after drawing all the text
 
 */
 
int DrawVehiclePurchaseInfo(int left, int right, int y, EngineID engine_number, TestedEngineDetails &te)
 
{
 
	const Engine *e = Engine::Get(engine_number);
 
	TimerGameCalendar::YearMonthDay ymd;
 
	TimerGameCalendar::ConvertDateToYMD(e->intro_date, &ymd);
 
	TimerGameCalendar::YearMonthDay ymd = TimerGameCalendar::ConvertDateToYMD(e->intro_date);
 
	bool refittable = IsArticulatedVehicleRefittable(engine_number);
 
	bool articulated_cargo = false;
 

	
 
	switch (e->type) {
 
		default: NOT_REACHED();
 
		case VEH_TRAIN:
src/cheat_gui.cpp
Show inline comments
 
@@ -101,14 +101,13 @@ extern void EnginesMonthlyLoop();
 
static int32_t ClickChangeDateCheat(int32_t new_value, int32_t)
 
{
 
	/* Don't allow changing to an invalid year, or the current year. */
 
	auto new_year = Clamp(TimerGameCalendar::Year(new_value), CalendarTime::MIN_YEAR, CalendarTime::MAX_YEAR);
 
	if (new_year == TimerGameCalendar::year) return TimerGameCalendar::year.base();
 

	
 
	TimerGameCalendar::YearMonthDay ymd;
 
	TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::date, &ymd);
 
	TimerGameCalendar::YearMonthDay ymd = TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::date);
 
	TimerGameCalendar::Date new_date = TimerGameCalendar::ConvertYMDToDate(new_year, ymd.month, ymd.day);
 

	
 
	/* Shift cached dates before we change the date. */
 
	for (auto v : Vehicle::Iterate()) v->ShiftDates(new_date - TimerGameCalendar::date);
 
	LinkGraphSchedule::instance.ShiftDates(new_date - TimerGameCalendar::date);
 

	
src/console_cmds.cpp
Show inline comments
 
@@ -1439,14 +1439,13 @@ DEF_CONSOLE_CMD(ConGetDate)
 
{
 
	if (argc == 0) {
 
		IConsolePrint(CC_HELP, "Returns the current date (year-month-day) of the game. Usage: 'getdate'.");
 
		return true;
 
	}
 

	
 
	TimerGameCalendar::YearMonthDay ymd;
 
	TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::date, &ymd);
 
	TimerGameCalendar::YearMonthDay ymd = TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::date);
 
	IConsolePrint(CC_DEFAULT, "Date: {:04d}-{:02d}-{:02d}", ymd.year, ymd.month + 1, ymd.day);
 
	return true;
 
}
 

	
 
DEF_CONSOLE_CMD(ConGetSysDate)
 
{
src/crashlog.cpp
Show inline comments
 
@@ -54,14 +54,13 @@ static void SurveyGamelog(nlohmann::json
 
static void SurveyRecentNews(nlohmann::json &json)
 
{
 
	json = nlohmann::json::array();
 

	
 
	int i = 0;
 
	for (NewsItem *news = _latest_news; i < 32 && news != nullptr; news = news->prev, i++) {
 
		TimerGameCalendar::YearMonthDay ymd;
 
		TimerGameCalendar::ConvertDateToYMD(news->date, &ymd);
 
		TimerGameCalendar::YearMonthDay ymd = TimerGameCalendar::ConvertDateToYMD(news->date);
 
		json.push_back(fmt::format("({}-{:02}-{:02}) StringID: {}, Type: {}, Ref1: {}, {}, Ref2: {}, {}",
 
		               ymd.year, ymd.month + 1, ymd.day, news->string_id, news->type,
 
		               news->reftype1, news->ref1, news->reftype2, news->ref2));
 
	}
 
}
 

	
src/date_gui.cpp
Show inline comments
 
@@ -48,13 +48,13 @@ struct SetDateWindow : Window {
 
	{
 
		assert(this->min_year <= this->max_year);
 
		this->parent = parent;
 
		this->InitNested(window_number);
 

	
 
		if (initial_date == 0) initial_date = TimerGameCalendar::date;
 
		TimerGameCalendar::ConvertDateToYMD(initial_date, &this->date);
 
		this->date = TimerGameCalendar::ConvertDateToYMD(initial_date);
 
		this->date.year = Clamp(this->date.year, min_year, max_year);
 
	}
 

	
 
	Point OnInitialPosition([[maybe_unused]] int16_t sm_width, [[maybe_unused]] int16_t sm_height, [[maybe_unused]] int window_number) override
 
	{
 
		Point pt = { this->parent->left + this->parent->width / 2 - sm_width / 2, this->parent->top + this->parent->height / 2 - sm_height / 2 };
src/engine.cpp
Show inline comments
 
@@ -660,14 +660,13 @@ void SetYearEngineAgingStops()
 

	
 
		/* Exclude certain engines */
 
		if (!HasBit(ei->climates, _settings_game.game_creation.landscape)) continue;
 
		if (e->type == VEH_TRAIN && e->u.rail.railveh_type == RAILVEH_WAGON) continue;
 

	
 
		/* Base year ending date on half the model life */
 
		TimerGameCalendar::YearMonthDay ymd;
 
		TimerGameCalendar::ConvertDateToYMD(ei->base_intro + (ei->lifelength.base() * CalendarTime::DAYS_IN_LEAP_YEAR) / 2, &ymd);
 
		TimerGameCalendar::YearMonthDay ymd = TimerGameCalendar::ConvertDateToYMD(ei->base_intro + (ei->lifelength.base() * CalendarTime::DAYS_IN_LEAP_YEAR) / 2);
 

	
 
		_year_engine_aging_stops = std::max(_year_engine_aging_stops, ymd.year);
 
	}
 
}
 

	
 
/**
src/landscape.cpp
Show inline comments
 
@@ -606,14 +606,13 @@ void SetSnowLine(byte table[SNOW_LINE_MO
 
 * @ingroup SnowLineGroup
 
 */
 
byte GetSnowLine()
 
{
 
	if (_snow_line == nullptr) return _settings_game.game_creation.snow_line_height;
 

	
 
	TimerGameCalendar::YearMonthDay ymd;
 
	TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::date, &ymd);
 
	TimerGameCalendar::YearMonthDay ymd = TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::date);
 
	return _snow_line->table[ymd.month][ymd.day];
 
}
 

	
 
/**
 
 * Get the highest possible snow line height, either variable or static.
 
 * @return the highest snow line height.
src/network/network_gui.cpp
Show inline comments
 
@@ -378,24 +378,22 @@ protected:
 
				DrawString(mapsize.left, mapsize.right, y + text_y_offset, STR_NETWORK_SERVER_LIST_MAP_SIZE_SHORT, TC_FROMSTRING, SA_HOR_CENTER);
 
			}
 

	
 
			if (const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(WID_NG_DATE); nwid->current_x != 0) {
 
				/* current date */
 
				Rect date = nwid->GetCurrentRect();
 
				TimerGameCalendar::YearMonthDay ymd;
 
				TimerGameCalendar::ConvertDateToYMD(cur_item->info.game_date, &ymd);
 
				TimerGameCalendar::YearMonthDay ymd = TimerGameCalendar::ConvertDateToYMD(cur_item->info.game_date);
 
				SetDParam(0, ymd.year);
 
				DrawString(date.left, date.right, y + text_y_offset, STR_JUST_INT, TC_BLACK, SA_HOR_CENTER);
 
			}
 

	
 
			if (const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(WID_NG_YEARS); nwid->current_x != 0) {
 
				/* number of years the game is running */
 
				Rect years = nwid->GetCurrentRect();
 
				TimerGameCalendar::YearMonthDay ymd_cur, ymd_start;
 
				TimerGameCalendar::ConvertDateToYMD(cur_item->info.game_date, &ymd_cur);
 
				TimerGameCalendar::ConvertDateToYMD(cur_item->info.start_date, &ymd_start);
 
				TimerGameCalendar::YearMonthDay ymd_cur = TimerGameCalendar::ConvertDateToYMD(cur_item->info.game_date);
 
				TimerGameCalendar::YearMonthDay ymd_start = TimerGameCalendar::ConvertDateToYMD(cur_item->info.start_date);
 
				SetDParam(0, ymd_cur.year - ymd_start.year);
 
				DrawString(years.left, years.right, y + text_y_offset, STR_JUST_INT, TC_BLACK, SA_HOR_CENTER);
 
			}
 

	
 
			/* draw a lock if the server is password protected */
 
			if (cur_item->info.use_password) DrawSprite(SPR_LOCK, PAL_NONE, info.left + this->lock_offset, y + lock_y_offset);
src/newgrf.cpp
Show inline comments
 
@@ -6517,14 +6517,13 @@ bool GetGlobalVariable(byte param, uint3
 

	
 
		case 0x01: // current year
 
			*value = (Clamp(TimerGameCalendar::year, CalendarTime::ORIGINAL_BASE_YEAR, CalendarTime::ORIGINAL_MAX_YEAR) - CalendarTime::ORIGINAL_BASE_YEAR).base();
 
			return true;
 

	
 
		case 0x02: { // detailed date information: month of year (bit 0-7), day of month (bit 8-12), leap year (bit 15), day of year (bit 16-24)
 
			TimerGameCalendar::YearMonthDay ymd;
 
			TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::date, &ymd);
 
			TimerGameCalendar::YearMonthDay ymd = TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::date);
 
			TimerGameCalendar::Date start_of_year = TimerGameCalendar::ConvertYMDToDate(ymd.year, 0, 1);
 
			*value = ymd.month | (ymd.day - 1) << 8 | (TimerGameCalendar::IsLeapYear(ymd.year) ? 1 << 15 : 0) | (TimerGameCalendar::date - start_of_year).base() << 16;
 
			return true;
 
		}
 

	
 
		case 0x03: // current climate, 0=temp, 1=arctic, 2=trop, 3=toyland
src/saveload/oldloader_sl.cpp
Show inline comments
 
@@ -841,14 +841,13 @@ static bool LoadOldIndustry(LoadgameStat
 
		i->town = Town::Get(RemapTownIndex(_old_town_index));
 

	
 
		if (_savegame_type == SGT_TTO) {
 
			if (i->type > 0x06) i->type++; // Printing Works were added
 
			if (i->type == 0x0A) i->type = 0x12; // Iron Ore Mine has different ID
 

	
 
			TimerGameCalendar::YearMonthDay ymd;
 
			TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::date, &ymd);
 
			TimerGameCalendar::YearMonthDay ymd = TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::date);
 
			i->last_prod_year = ymd.year;
 

	
 
			i->random_colour = RemapTTOColour(i->random_colour);
 
		}
 

	
 
		Industry::IncIndustryTypeCount(i->type);
src/script/api/script_date.cpp
Show inline comments
 
@@ -26,32 +26,29 @@
 
}
 

	
 
/* static */ SQInteger ScriptDate::GetYear(ScriptDate::Date date)
 
{
 
	if (date < 0) return DATE_INVALID;
 

	
 
	::TimerGameCalendar::YearMonthDay ymd;
 
	::TimerGameCalendar::ConvertDateToYMD(date, &ymd);
 
	::TimerGameCalendar::YearMonthDay ymd = ::TimerGameCalendar::ConvertDateToYMD(date);
 
	return ymd.year.base();
 
}
 

	
 
/* static */ SQInteger ScriptDate::GetMonth(ScriptDate::Date date)
 
{
 
	if (date < 0) return DATE_INVALID;
 

	
 
	::TimerGameCalendar::YearMonthDay ymd;
 
	::TimerGameCalendar::ConvertDateToYMD(date, &ymd);
 
	::TimerGameCalendar::YearMonthDay ymd = ::TimerGameCalendar::ConvertDateToYMD(date);
 
	return ymd.month + 1;
 
}
 

	
 
/* static */ SQInteger ScriptDate::GetDayOfMonth(ScriptDate::Date date)
 
{
 
	if (date < 0) return DATE_INVALID;
 

	
 
	::TimerGameCalendar::YearMonthDay ymd;
 
	::TimerGameCalendar::ConvertDateToYMD(date, &ymd);
 
	::TimerGameCalendar::YearMonthDay ymd = ::TimerGameCalendar::ConvertDateToYMD(date);
 
	return ymd.day;
 
}
 

	
 
/* static */ ScriptDate::Date ScriptDate::GetDate(SQInteger year, SQInteger month, SQInteger day_of_month)
 
{
 
	if (month < 1 || month > 12) return DATE_INVALID;
src/strings.cpp
Show inline comments
 
@@ -480,32 +480,29 @@ static void FormatBytes(StringBuilder &b
 
	assert(id < lengthof(iec_prefixes));
 
	fmt::format_to(builder, NBSP "{}B", iec_prefixes[id]);
 
}
 

	
 
static void FormatYmdString(StringBuilder &builder, TimerGameCalendar::Date date, uint case_index)
 
{
 
	TimerGameCalendar::YearMonthDay ymd;
 
	TimerGameCalendar::ConvertDateToYMD(date, &ymd);
 
	TimerGameCalendar::YearMonthDay ymd = TimerGameCalendar::ConvertDateToYMD(date);
 

	
 
	auto tmp_params = MakeParameters(ymd.day + STR_DAY_NUMBER_1ST - 1, STR_MONTH_ABBREV_JAN + ymd.month, ymd.year);
 
	FormatString(builder, GetStringPtr(STR_FORMAT_DATE_LONG), tmp_params, case_index);
 
}
 

	
 
static void FormatMonthAndYear(StringBuilder &builder, TimerGameCalendar::Date date, uint case_index)
 
{
 
	TimerGameCalendar::YearMonthDay ymd;
 
	TimerGameCalendar::ConvertDateToYMD(date, &ymd);
 
	TimerGameCalendar::YearMonthDay ymd = TimerGameCalendar::ConvertDateToYMD(date);
 

	
 
	auto tmp_params = MakeParameters(STR_MONTH_JAN + ymd.month, ymd.year);
 
	FormatString(builder, GetStringPtr(STR_FORMAT_DATE_SHORT), tmp_params, case_index);
 
}
 

	
 
static void FormatTinyOrISODate(StringBuilder &builder, TimerGameCalendar::Date date, StringID str)
 
{
 
	TimerGameCalendar::YearMonthDay ymd;
 
	TimerGameCalendar::ConvertDateToYMD(date, &ymd);
 
	TimerGameCalendar::YearMonthDay ymd = TimerGameCalendar::ConvertDateToYMD(date);
 

	
 
	/* Day and month are zero-padded with ZEROFILL_NUM, hence the two 2s. */
 
	auto tmp_params = MakeParameters(ymd.day, 2, ymd.month + 1, 2, ymd.year);
 
	FormatString(builder, GetStringPtr(str), tmp_params);
 
}
 

	
src/subsidy_gui.cpp
Show inline comments
 
@@ -139,14 +139,13 @@ struct SubsidyListWindow : Window {
 
	}
 

	
 
	void DrawWidget(const Rect &r, int widget) const override
 
	{
 
		if (widget != WID_SUL_PANEL) return;
 

	
 
		TimerGameCalendar::YearMonthDay ymd;
 
		TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::date, &ymd);
 
		TimerGameCalendar::YearMonthDay ymd = TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::date);
 

	
 
		Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
 

	
 
		int pos = -this->vscroll->GetPosition();
 
		const int cap = this->vscroll->GetCapacity();
 

	
src/survey.cpp
Show inline comments
 
@@ -308,14 +308,13 @@ void SurveyCompanies(nlohmann::json &sur
 
 */
 
void SurveyTimers(nlohmann::json &survey)
 
{
 
	survey["ticks"] = TimerGameTick::counter;
 
	survey["seconds"] = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - _switch_mode_time).count();
 

	
 
	TimerGameCalendar::YearMonthDay ymd;
 
	TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::date, &ymd);
 
	TimerGameCalendar::YearMonthDay ymd = TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::date);
 
	survey["calendar"] = fmt::format("{:04}-{:02}-{:02} ({})", ymd.year, ymd.month + 1, ymd.day, TimerGameCalendar::date_fract);
 
}
 

	
 
/**
 
 * Convert GRF information to JSON.
 
 *
src/timer/timer_game_calendar.cpp
Show inline comments
 
@@ -63,24 +63,23 @@ static const uint16_t _accum_days_for_mo
 
	ACCUM_SEP, ACCUM_OCT, ACCUM_NOV, ACCUM_DEC,
 
};
 

	
 
/**
 
 * Converts a Date to a Year, Month & Day.
 
 * @param date the date to convert from
 
 * @param ymd  the year, month and day to write to
 
 * @returns YearMonthDay representation of the Date.
 
 */
 
/* static */ void TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::Date date, TimerGameCalendar::YearMonthDay *ymd)
 
/* static */ TimerGameCalendar::YearMonthDay TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::Date date)
 
{
 
	/* Year determination in multiple steps to account for leap
 
	 * years. First do the large steps, then the smaller ones.
 
	 */
 

	
 
	/* There are 97 leap years in 400 years */
 
	TimerGameCalendar::Year yr = 400 * (date.base() / (CalendarTime::DAYS_IN_YEAR * 400 + 97));
 
	int rem = date.base() % (CalendarTime::DAYS_IN_YEAR * 400 + 97);
 
	uint16_t x;
 

	
 
	if (rem >= CalendarTime::DAYS_IN_YEAR * 100 + 25) {
 
		/* There are 25 leap years in the first 100 years after
 
		 * every 400th year, as every 400th year is a leap year */
 
		yr += 100;
 
		rem -= CalendarTime::DAYS_IN_YEAR * 100 + 25;
 
@@ -107,17 +106,19 @@ static const uint16_t _accum_days_for_mo
 
		yr++;
 
	}
 

	
 
	/* Skip the 29th of February in non-leap years */
 
	if (!TimerGameCalendar::IsLeapYear(yr) && rem >= ACCUM_MAR - 1) rem++;
 

	
 
	ymd->year = yr;
 
	uint16_t x = _month_date_from_year_day[rem];
 

	
 
	x = _month_date_from_year_day[rem];
 
	ymd->month = x >> 5;
 
	ymd->day = x & 0x1F;
 
	YearMonthDay ymd;
 
	ymd.year = yr;
 
	ymd.month = x >> 5;
 
	ymd.day = x & 0x1F;
 
	return ymd;
 
}
 

	
 
/**
 
 * Converts a tuple of Year, Month and Day to a Date.
 
 * @param year  is a number between 0..MAX_YEAR
 
 * @param month is a number between 0..11
 
@@ -150,17 +151,15 @@ static const uint16_t _accum_days_for_mo
 
 * @param fract The number of ticks that have passed on this date.
 
 */
 
/* static */ void TimerGameCalendar::SetDate(TimerGameCalendar::Date date, TimerGameCalendar::DateFract fract)
 
{
 
	assert(fract < Ticks::DAY_TICKS);
 

	
 
	TimerGameCalendar::YearMonthDay ymd;
 

	
 
	TimerGameCalendar::date = date;
 
	TimerGameCalendar::date_fract = fract;
 
	TimerGameCalendar::ConvertDateToYMD(date, &ymd);
 
	TimerGameCalendar::YearMonthDay ymd = TimerGameCalendar::ConvertDateToYMD(date);
 
	TimerGameCalendar::year = ymd.year;
 
	TimerGameCalendar::month = ymd.month;
 
}
 

	
 
template<>
 
void IntervalTimer<TimerGameCalendar>::Elapsed(TimerGameCalendar::TElapsed trigger)
 
@@ -192,14 +191,13 @@ void TimerManager<TimerGameCalendar>::El
 
	if (TimerGameCalendar::date_fract < Ticks::DAY_TICKS) return;
 
	TimerGameCalendar::date_fract = 0;
 

	
 
	/* increase day counter */
 
	TimerGameCalendar::date++;
 

	
 
	TimerGameCalendar::YearMonthDay ymd;
 
	TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::date, &ymd);
 
	TimerGameCalendar::YearMonthDay ymd = TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::date);
 

	
 
	/* check if we entered a new month? */
 
	bool new_month = ymd.month != TimerGameCalendar::month;
 

	
 
	/* check if we entered a new year? */
 
	bool new_year = ymd.year != TimerGameCalendar::year;
src/timer/timer_game_calendar.h
Show inline comments
 
@@ -98,13 +98,13 @@ public:
 

	
 
	using TElapsed = uint;
 
	struct TStorage {
 
	};
 

	
 
	static bool IsLeapYear(Year yr);
 
	static void ConvertDateToYMD(Date date, YearMonthDay * ymd);
 
	static YearMonthDay ConvertDateToYMD(Date date);
 
	static Date ConvertYMDToDate(Year year, Month month, Day day);
 
	static void SetDate(Date date, DateFract fract);
 

	
 
	/**
 
	 * Calculate the year of a given date.
 
	 * @param date The date to consider.
0 comments (0 inline, 0 general)