我知道之前可能已经问过这个问题,但这是我之前发现的here问题之一。从其他问题中得出的独特之处在于,我正在寻找与我的编译器最新且相关的东西:Visual Studio 2017使用C ++ 17 - 最新草案标准。
我知道C ++ 20将会发布并可用于我期待的Visual Studio 2019。但是在当时;我仍然在Windows 7上使用Visual Studio 2017,而且我目前受限于C ++ 17。我发现这个Q/A类似于提供了很好的答案,但是当我开始使用<ctime>
或<time.h>
函数时,例如:
std::gmtime()
std::localtime()
我的编译器对我大吼大叫,这些函数被标记为不安全并且已弃用。我试图编写这样的函数:
-DateAndTime.h-
#pragma once
#include <ctime>
#include <iomanip>
#include <iostream>
#include <sstream>
namespace util {
enum class TimeLocale {
LOCAL = 0x01,
GMT = 0x02,
BOTH = (LOCAL | GMT)
};
inline TimeLocale operator|(TimeLocale a, TimeLocale b) {
return static_cast<TimeLocale>(static_cast<int>(a) | static_cast<int>(b));
}
#pragma warning( push )
#pragma warning( disable : 4996 )
inline void currentDateAndTime(std::stringstream& stream, TimeLocale locale) {
std::time_t t = std::time(nullptr);
if (locale == TimeLocale::GMT) {
stream << "UTC: " << std::put_time( std::gmtime(&t), "%c, %Z") << '\n';
}
if (locale == TimeLocale::LOCAL) {
stream << "LOCAL: " << std::put_time(std::localtime(&t), "%c, %Z") << '\n';
}
if (locale == TimeLocale::BOTH) {
stream << "UTC: " << std::put_time(std::gmtime(&t), "%c, %Z") << '\n'
<< "LOCAL: " << std::put_time(std::localtime(&t), "%c, %Z") << '\n';
}
}
#pragma warning( pop )
} // namespace util
-main.cpp-
#include "DateAndTime.h"
#include <iostream>
#include <sstream>
using namespace util;
int main() {
try {
std::stringstream stream1;
getCurrentTime(stream1, TimeLocale::GMT);
std::cout << stream1.str() << '\n';
std::stringstream stream2;
getCurrentTime(stream2, TimeLocale::LOCAL);
std::cout << stream2.str() << '\n';
std::stringstream stream3;
getCurrentTime(stream3, TimeLocale::BOTH);
std::cout << stream3.str() << '\n';
std::stringstream stream4;
getCurrentTime(stream4, TimeLocale::GMT | TimeLocale::LOCAL);
std::cout << stream4.str() << '\n';
// ExceptionHandler is one of my class's and can be ignored in this context
// You can replace this with std::exception, std::cerr, etc...
} catch ( ExceptionHandler& e ) {
std::cout << "Exception Thrown: " << e.getMessage() << std::endl;
return EXIT_FAILURE;
} catch (...) {
std::cout << __FUNCTION__ << " Caught Unknown Exception" << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
如果我使用#pragma warning( disable : 4996 )
,这可以正常工作
而且我得到了一个很好的输出:
UTC: 02/20/19 05:44:38, Eastern Standard Time
Local: 02/20/19 00:44:38, Eastern Standard Time
UTC: 02/20/19 05:44:38, Eastern Standard Time
Local: 02/20/19 00:44:38, Eastern Standard Time
UTC: 02/20/19 05:44:38, Eastern Standard Time
Local: 02/20/19 00:44:38, Eastern Standard Time
哪个看起来很棒。但是,我宁愿不使用任何标记为已弃用的函数,这些函数不是特定于平台的,它们是通用的,可移植的和跨平台的,目前在C ++中可用17。最好来自标准库的东西。我不想使用第三方库,也不想提升。 std::chrono
将是一个不错的选择,但他们的calendar
部分将在C ++ 20完全发布之前不可用。我剩下什么样的选择?
不推荐使用gmtime和localtime函数。
仅适用于Visual Studio,因为Visual Studio提供了替代gmtime_s和localtime_s,所以我将使用这些函数。在Unix下你有gmtime_r和localtime_r,如果你想要线程安全的话。另见this answer
在Windows下编写一个内联的gmtime_r和localtime_r来调用gmtime_s和localtime_s,你有一个几乎标准的交叉平台解决方案,直到C ++ 20。