minetest/src/gettime.h

65 lines
1.5 KiB
C
Raw Normal View History

2010-12-21 17:08:24 +01:00
/*
2013-02-24 18:40:43 +01:00
Minetest
2013-02-24 19:38:45 +01:00
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
2010-12-21 17:08:24 +01:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
2010-12-21 17:08:24 +01:00
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
2010-12-21 17:08:24 +01:00
You should have received a copy of the GNU Lesser General Public License along
2010-12-21 17:08:24 +01:00
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#pragma once
2010-12-21 17:08:24 +01:00
#include <ctime>
#include <string>
#include <mutex>
2010-12-21 17:08:24 +01:00
enum TimePrecision
{
PRECISION_SECONDS,
PRECISION_MILLI,
PRECISION_MICRO,
PRECISION_NANO
};
inline struct tm mt_localtime()
2010-12-21 17:08:24 +01: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 17:08:24 +01:00
time_t t = time(NULL);
// 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();
char cs[20]; // YYYY-MM-DD HH:MM:SS + '\0'
strftime(cs, 20, "%Y-%m-%d %H:%M:%S", &tm);
2010-12-21 17:08:24 +01:00
return cs;
}