Browse Source

Handle nullptr localtime

`localtime` can return `nullptr` on some platforms (e.g. NXDK)

Improve portability by handling the `nullptr` result.
pull/4902/head
Gleb Mazovetskiy 4 years ago
parent
commit
d07ac14440
  1. 5
      Source/objects.cpp
  2. 15
      Source/qol/chatlog.cpp

5
Source/objects.cpp

@ -3232,8 +3232,9 @@ void OperateShrineSolar(Player &player)
if (&player != MyPlayer)
return;
time_t tm = time(nullptr);
int hour = localtime(&tm)->tm_hour;
time_t timeResult = time(nullptr);
const std::tm *localtimeResult = localtime(&timeResult);
int hour = localtimeResult != nullptr ? localtimeResult->tm_hour : 20;
if (hour >= 20 || hour < 4) {
InitDiabloMsg(EMSG_SHRINE_SOLAR4);
ModifyPlrVit(player, 2);

15
Source/qol/chatlog.cpp

@ -116,8 +116,10 @@ void ToggleChatLog()
void AddMessageToChatLog(string_view message, Player *player, UiFlags flags)
{
MessageCounter++;
time_t tm = time(nullptr);
std::string timestamp = fmt::format("[#{:d}] {:02}:{:02}:{:02}", MessageCounter, localtime(&tm)->tm_hour, localtime(&tm)->tm_min, localtime(&tm)->tm_sec);
time_t timeResult = time(nullptr);
const std::tm *localtimeResult = localtime(&timeResult);
std::string timestamp = localtimeResult != nullptr ? fmt::format("[#{:d}] {:02}:{:02}:{:02}", MessageCounter, localtimeResult->tm_hour, localtimeResult->tm_min, localtimeResult->tm_sec)
: fmt::format("[#{:d}] ", MessageCounter);
int oldSize = ChatLogLines.size();
ChatLogLines.emplace_back(MultiColoredText { "", { {} } });
if (player == nullptr) {
@ -156,9 +158,12 @@ void DrawChatLog(const Surface &out)
{ { sx, sy + PaddingTop + blankLineHeight }, { ContentTextWidth, lineHeight } },
(UnreadFlag ? UiFlags::ColorRed : UiFlags::ColorWhitegold) | UiFlags::AlignCenter);
time_t tm = time(nullptr);
std::string timestamp = fmt::format("{:02}:{:02}:{:02}", localtime(&tm)->tm_hour, localtime(&tm)->tm_min, localtime(&tm)->tm_sec);
DrawString(out, timestamp, { { sx, sy + PaddingTop + blankLineHeight }, { ContentTextWidth, lineHeight } }, UiFlags::ColorWhitegold);
time_t timeResult = time(nullptr);
const std::tm *localtimeResult = localtime(&timeResult);
if (localtimeResult != nullptr) {
std::string timestamp = fmt::format("{:02}:{:02}:{:02}", localtimeResult->tm_hour, localtimeResult->tm_min, localtimeResult->tm_sec);
DrawString(out, timestamp, { { sx, sy + PaddingTop + blankLineHeight }, { ContentTextWidth, lineHeight } }, UiFlags::ColorWhitegold);
}
const int titleBottom = sy + HeaderHeight();
DrawSLine(out, titleBottom);

Loading…
Cancel
Save