在CodeBlocks的Windows 8上的cygwin64下使用strptime [英] Using strptime under cygwin64 on Windows 8 in CodeBlocks
问题描述
我正在尝试在Windows计算机上编译一些最初用linux编写的代码。我已经安装并设置了Cygwin以便在CodeBlocks中使用,并且大多数情况下都可以使用。除了对strptime的调用以外,所有其他调用都向我招呼错误:在此范围内,'strptime'没有得到关注。我已经搜寻了一段时间,但无济于事,请有人可以解释出什么问题吗?我尝试过加入time.h,但没有运气。
I am trying to compile some code originally written in linux on my Windows machine. I have Cygwin installed and setup for use within CodeBlocks, and it works mostly. All except a call to strptime, which greets me with "error: 'strptime' was not delcared in this scope." I've been googling for a while now to no avail, please could someone explain what might be wrong? I've tried including time.h but no luck.
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE
#endif
#include <ctime>
#include <cstring>
class Date {
private:
struct tm _tm;
string strform;
static void set_zero(struct tm &_tm) {
memset(&_tm, '\0', sizeof(struct tm));
// _tm.hour = 0;
}
time_t make_time() {
return mktime(&_tm);
}
public:
Date() {
time_t current = time(NULL);
_tm = *gmtime(¤t);
char buffer[1024];
strftime(buffer, 1024, "%b %e %Y", &_tm);
strform = buffer;
}
Date(time_t current) {
_tm = *gmtime(¤t);
char buffer[1024];
strftime(buffer, 1024, "%b %e %Y", &_tm);
strform = buffer;
}
Date(string _strform) {
strform = _strform;
set_zero(_tm);
char *result = strptime(strform.c_str(), "%b %e %Y", &_tm);
assert(result);
char buffer[1024];
strftime(buffer, 1024, "%b %e %Y", &_tm);
strform = buffer;
//cout << "strform = " << strform << endl;
// cout << "length = " << result - strform.c_str() << endl;
// cout << "day = " << _tm.tm_mday << endl;
// cout << "month = " << _tm.tm_mon << endl;
// cout << "year = " << _tm.tm_year << endl;
// char buffer[1024];
// strftime(buffer, 1024, "%b %e %Y %H:%M:%S", &_tm);
// cout << "buffer = " << buffer << endl;
}
Date operator-(int days) {
return Date(make_time() - days*86400 );
}
time_t operator-(Date &other) {
return (make_time() - other.make_time())/86400.0;
}
bool operator<=(Date &other) {
return make_time() <= other.make_time();
}
bool operator<(Date &other) {
return make_time() < other.make_time();
}
bool operator>=(Date &other) {
return make_time() >= other.make_time();
}
time_t to_seconds() {
return make_time();
}
friend ostream &operator<< (ostream &out, const Date &d) {
return out << d.strform;
}
static Date today() {
return Date();
}
string to_string() const {
return strform;
}
const char *to_cstring() const {
return strform.c_str();
}
};
推荐答案
在XOPEN_SOURCE 上查看此答案。通过在我的CMakeLists.txt中添加 add_definitions(-D_XOPEN_SOURCE = 700)
来获取strptime,我有些运气。通过打开标头中的功能,最终使编译器可以找到strptime。
See this answer on XOPEN_SOURCE. I had some luck getting strptime to compile by adding add_definitions(-D_XOPEN_SOURCE=700)
to my CMakeLists.txt. That ended letting strptime be found by the compiler, by turning features on in the header.
这篇关于在CodeBlocks的Windows 8上的cygwin64下使用strptime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!