2024-10-28 15:57:39 +01:00
|
|
|
// Luanti
|
|
|
|
// SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
2010-12-21 18:08:24 +02:00
|
|
|
|
2017-08-17 22:19:39 +02:00
|
|
|
#pragma once
|
2010-12-21 18:08:24 +02:00
|
|
|
|
2017-08-20 13:30:50 +02:00
|
|
|
#include <ctime>
|
2016-03-06 14:31:16 -05:00
|
|
|
#include <string>
|
2022-04-28 18:53:33 +02:00
|
|
|
#include <mutex>
|
2010-12-21 18:08:24 +02:00
|
|
|
|
2022-04-28 18:53:33 +02:00
|
|
|
inline struct tm mt_localtime()
|
2010-12-21 18:08:24 +02:00
|
|
|
{
|
2022-04-28 18:53:33 +02:00
|
|
|
// initialize the time zone on first invocation
|
|
|
|
static std::once_flag tz_init;
|
|
|
|
std::call_once(tz_init, [] {
|
|
|
|
#ifdef _WIN32
|
|
|
|
_tzset();
|
|
|
|
#else
|
|
|
|
tzset();
|
|
|
|
#endif
|
|
|
|
});
|
|
|
|
|
|
|
|
struct tm ret;
|
2010-12-21 18:08:24 +02:00
|
|
|
time_t t = time(NULL);
|
2022-04-28 18:53:33 +02:00
|
|
|
// TODO we should check if the function returns NULL, which would mean error
|
|
|
|
#ifdef _WIN32
|
|
|
|
localtime_s(&ret, &t);
|
|
|
|
#else
|
|
|
|
localtime_r(&t, &ret);
|
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline std::string getTimestamp()
|
|
|
|
{
|
|
|
|
const struct tm tm = mt_localtime();
|
2017-04-01 14:48:16 +02:00
|
|
|
char cs[20]; // YYYY-MM-DD HH:MM:SS + '\0'
|
2022-04-28 18:53:33 +02:00
|
|
|
strftime(cs, 20, "%Y-%m-%d %H:%M:%S", &tm);
|
2010-12-21 18:08:24 +02:00
|
|
|
return cs;
|
|
|
|
}
|