Using strptime under cygwin64 on Windows 8 in CodeBlocks

无人久伴 提交于 2020-03-16 07:05:47

问题


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(&current);
    char buffer[1024];
    strftime(buffer, 1024, "%b %e %Y", &_tm);
    strform = buffer;
  }

  Date(time_t current) {
    _tm = *gmtime(&current);
    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();
  }
};

回答1:


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.




回答2:


This error message means that function is not declared at the point it is found. It looks like strptime() is declared in the time.h file in Cygwin (at least in v1.7.25) so simply add:

 #include <time.h>

to your source file. Note that strptime() is not a standard C function.




回答3:


This only happens in C++11 and above. It looks like __STRICT_ANSI__ is being set and strptime() is not defined under that. A workaround is to undef __STRICT_ANSI__ as follows.

#ifdef __CYGWIN__
#undef __STRICT_ANSI__
#endif


来源:https://stackoverflow.com/questions/25632021/using-strptime-under-cygwin64-on-windows-8-in-codeblocks

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!