summaryrefslogtreecommitdiff
path: root/Source/Core/Common/TimeUtil.cpp
blob: fa47e2350348449dee5b7a98956fe914443ef24a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Copyright 2024 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "Common/TimeUtil.h"

#include <cstring>
#include <ctime>
#include <optional>

#include "Common/Logging/Log.h"

namespace Common
{
std::optional<std::tm> LocalTime(std::time_t time)
{
  std::tm local_time;
#ifdef _MSC_VER
  if (localtime_s(&local_time, &time) != 0)
#else
  if (localtime_r(&time, &local_time) == nullptr)
#endif
  {
    ERROR_LOG_FMT(COMMON, "Failed to convert time to local time: {}", std::strerror(errno));
    return std::nullopt;
  }
  return local_time;
}
}  // Namespace Common