srcspider
srcspider

Reputation: 11205

When is getdate and strptime not included in time.h?

So the function getdate_r seems to be undefined for me; compiling the following doesn't work in either gcc or clang, (the man page program also doesn't work)

#include <time.h>

int main() {
    char timeString[] = "2015/01/01 10:30:50";
    struct tm res = {0};
    int err = getdate_r(timeString, &res);
    return err;
}

clang reports the following

test.c:6:12: warning: implicit declaration of function 'getdate_r' is invalid
      in C99 [-Wimplicit-function-declaration]
        int err = getdate_r(timeString, &res);
                  ^
1 warning generated.

Other functions from time.h such as getdate, strptime also don't work in a similar manner.

Anyone have an explanation on whats going on?

clang version information

Ubuntu clang version 3.6.0-2ubuntu1 (tags/RELEASE_360/final) (based on LLVM 3.6.0)
Target: x86_64-pc-linux-gnu
Thread model: posix

Upvotes: 1

Views: 1411

Answers (1)

Klas Lindb&#228;ck
Klas Lindb&#228;ck

Reputation: 33273

To make getdate_r available you need to:

#define _GNU_SOURCE 1

before including any include files. Doing so will provide declarations for various GNU extensions including getdate_r:

#define _GNU_SOURCE 1
#include <time.h>

int main(void) {
    char timeString[] = "2015/01/01 10:30:50";
    struct tm res = {0};
    int err = getdate_r(timeString, &res);
    return err;
}

Upvotes: 1

Related Questions