Luanti 5.11.0-dev
 
Loading...
Searching...
No Matches
gettime.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include <ctime>
8#include <string>
9#include <mutex>
10
11inline struct tm mt_localtime()
12{
13 // initialize the time zone on first invocation
14 static std::once_flag tz_init;
15 std::call_once(tz_init, [] {
16#ifdef _WIN32
17 _tzset();
18#else
19 tzset();
20#endif
21 });
22
23 struct tm ret;
24 time_t t = time(NULL);
25 // TODO we should check if the function returns NULL, which would mean error
26#ifdef _WIN32
27 localtime_s(&ret, &t);
28#else
29 localtime_r(&t, &ret);
30#endif
31 return ret;
32}
33
34
35inline std::string getTimestamp()
36{
37 const struct tm tm = mt_localtime();
38 char cs[20]; // YYYY-MM-DD HH:MM:SS + '\0'
39 strftime(cs, 20, "%Y-%m-%d %H:%M:%S", &tm);
40 return cs;
41}
std::string getTimestamp()
Definition gettime.h:35
struct tm mt_localtime()
Definition gettime.h:11