C++ strptime() 在 Windows 上等价?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/321849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
strptime() equivalent on Windows?
提问by An???drew
Is there a good equivalent implementation of strptime()
available for Windows? Unfortunately, this POSIX function does not appear to be available.
是否有strptime()
适用于 Windows的良好等效实现?不幸的是,这个 POSIX 函数似乎不可用。
Open Group description of strptime- summary: it converts a text string such as "MM-DD-YYYY HH:MM:SS"
into a tm struct
, the opposite of strftime()
.
strptime-summary 的Open Group 描述:它将文本字符串如"MM-DD-YYYY HH:MM:SS"
转换为 a tm struct
,与strftime()
.
采纳答案by Adam Rosenfield
An open-source version (BSD license) of strptime()
can be found here: http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/time/strptime.c?rev=HEAD
strptime()
可以在此处找到开源版本(BSD 许可证):http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/time/strptime.c?rev=HEAD
You'll need to add the following declaration to use it:
您需要添加以下声明才能使用它:
char *strptime(const char * __restrict, const char * __restrict, struct tm * __restrict);
回答by amwinter
If you don't want to port any code or condemn your project to boost, you can do this:
如果你不想移植任何代码或谴责你的项目来提升,你可以这样做:
- parse the date using
sscanf
- then copy the integers into a
struct tm
(subtract 1 from month and 1900 from year -- months are 0-11 and years start in 1900) - finally, use
mktime
to get a UTC epoch integer
- 使用解析日期
sscanf
- 然后将整数复制到一个
struct tm
(从月份中减去 1,从年份中减去 1900——月份是 0-11,年份从 1900 年开始) - 最后,用于
mktime
获取 UTC 纪元整数
Just remember to set the isdst
member of the struct tm
to -1, or else you'll have daylight savings issues.
请记住将isdst
成员设置struct tm
为 -1,否则您将遇到夏令时问题。
回答by Orvid King
Assuming you are using Visual Studio 2015 or above, you can use this as a drop-in replacement for strptime:
假设您使用的是 Visual Studio 2015 或更高版本,您可以使用它作为 strptime 的替代品:
#include <time.h>
#include <iomanip>
#include <sstream>
extern "C" char* strptime(const char* s,
const char* f,
struct tm* tm) {
// Isn't the C++ standard lib nice? std::get_time is defined such that its
// format parameters are the exact same as strptime. Of course, we have to
// create a string stream first, and imbue it with the current C locale, and
// we also have to make sure we return the right things if it fails, or
// if it succeeds, but this is still far simpler an implementation than any
// of the versions in any of the C standard libraries.
std::istringstream input(s);
input.imbue(std::locale(setlocale(LC_ALL, nullptr)));
input >> std::get_time(tm, f);
if (input.fail()) {
return nullptr;
}
return (char*)(s + input.tellg());
}
Just be aware that for cross platform applications, std::get_time
wasn't implemented until GCC 5.1, so switching to calling std::get_time
directly may not be an option.
请注意,对于跨平台应用程序,std::get_time
直到 GCC 5.1 才实现,因此切换到std::get_time
直接调用可能不是一种选择。
回答by ravenspoint
This does the job:
这可以完成以下工作:
#include "stdafx.h"
#include "boost/date_time/posix_time/posix_time.hpp"
using namespace boost::posix_time;
int _tmain(int argc, _TCHAR* argv[])
{
std::string ts("2002-01-20 23:59:59.000");
ptime t(time_from_string(ts));
tm pt_tm = to_tm( t );
Notice, however, that the input string is YYYY-MM-DD
但是请注意,输入字符串是 YYYY-MM-DD
回答by Pr0t0c0l78
One alternative is to use GetSystemTime
and send the time information to a function that parses it according to your format using vsnprintf_s
. In the
example below there is one function that creates a time string with milli second
precision. It then sends the string to a function that formats it according to the desired format:
一种替代方法是使用GetSystemTime
时间信息并将其发送到一个函数,该函数根据您的格式使用vsnprintf_s
. 在下面的示例中,有一个函数可以创建毫秒精度的时间字符串。然后它将字符串发送到一个函数,该函数根据所需的格式对其进行格式化:
#include <string>
#include <cstdio>
#include <cstdarg>
#include <atlstr.h>
std::string FormatToISO8601 (const std::string FmtS, ...) {
CStringA BufferString;
try {
va_list VaList;
va_start (VaList, FmtS);
BufferString.FormatV (FmtS.c_str(), VaList);
} catch (...) {}
return std::string (BufferString);
}
void CreateISO8601String () {
SYSTEMTIME st;
GetSystemTime(&st);
std::string MyISO8601String = FormatToISO8601 ("%4u-%02u-%02uT%02u:%02u:%02u.%03u", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
}