JAN
JAN

Reputation: 21855

Any specific reason why localtime throws warning with struct tm* & stat* , in linux ?

I have this simple code (part of a project) :

void displayFileProperties(struct stat* file,char*  outputProperties , char * path)
{

    struct tm* time;

        // code 
        // code
        time = localtime(&file->st_mtim);


        // code 

}

Where eclipse keeps throwing me a warning :

passing argument 1 of ‘localtime’ from incompatible pointer type [enabled by default]   main.c  /ex4    line 340    C/C++ Problem

Any idea how to fix this ? thanks

Upvotes: 0

Views: 2018

Answers (5)

Abraham
Abraham

Reputation: 29

#include <malloc.h>
#include <time.h>
#include <stdio.h>

static struct tm* alarmTime(void);

int main(){
    printf("Hour :%i\n", alarmTime()->tm_hour);
    printf("Minute :%i\n", alarmTime()->tm_min);
    return 0;
}

static struct tm* alarmTime(void){
    time_t now = time(NULL);
    struct tm* ptm;
#ifdef HAVE_LOCALTIME_R
    struct tm tmbuf;
    ptm = localtime_r(&now, &tmbuf);
#else
    ptm = localtime(&now);
#endif
    return ptm;
}

Upvotes: 0

Daniel
Daniel

Reputation: 3

I had the same issue with Eclipse: Field st_mtime could not be resolved (semantic error)

Fixed the issue in Eclipse by right-clicking the project, choose Index->"Freshen All Files"

Upvotes: 0

paulsm4
paulsm4

Reputation: 121599

Completely changed answer:

SUGGESTIONS:

1) Make sure you #include these headers:

#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>

2) Cast your pointer to "const"

time = localtime((const time_t *)&file->st_mtime);

3) Post back what happens

=====================================================

ADDITIONAL SUGGESTIONS:

1) Please read these two links:

Since kernel 2.5.48, the stat structure supports nanosecond resolution for the three file timestamp fields. Glibc exposes the nanosecond component of each field using names of the form st_atim.tv_nsec if the _BSD_SOURCE or _SVID_SOURCE feature test macro is defined. These fields are specified in POSIX.1-2008, and, starting with version 2.12, glibc also exposes these field names if _POSIX_C_SOURCE is defined with the value 200809L or greater, or _XOPEN_SOURCE is defined with the value 700 or greater. If none of the aforementioned macros are defined, then the nanosecond values are exposed with names of the form st_atimensec. On file systems that do not support subsecond timestamps, the nanosecond fields are returned with the value 0.

2) Clearly, the makefile (that "works") has a #define that Eclipse doesn't, or vice versa.

Probably either/both _POSIX_C_SOURCE and/or _XOPEN_SOURCE.

Run this command to see what exists in the command line (makefile?) environment:

gcc -dM -E - < /dev/null | less

3) Please post back what you find!

Upvotes: 1

Giel
Giel

Reputation: 2875

You'll want to use this instead:

time = localtime(&file->st_mtime);

Note the added 'e' at the end. st_mtim is a timespec, with 'e' added it's a time_t (what you need).

Upvotes: 1

Alan Curry
Alan Curry

Reputation: 14711

st_mtim is a struct timespec (seconds and nanoseconds). You want st_mtime.

Upvotes: 1

Related Questions