0

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

JAN
  • 21,236
  • 66
  • 181
  • 318

5 Answers5

1

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

Alan Curry
  • 14,255
  • 3
  • 32
  • 33
  • Alan please note that Eclipse shows an error this time (after adding the `e`) : ` Field 'st_mtime' could not be resolved main.c /ex4 line 342 Semantic Error ` – JAN Jul 26 '12 at 21:00
  • Eclipse is wrong :-) Seriously. Smash it in the head until it respects your authority. Or add the eclipse tag to this question, or ask a separate followup question... this is just an Eclipse bug report now. – Alan Curry Jul 26 '12 at 21:01
  • I wish :) but it won't compile with that lovely `e` at the end – JAN Jul 26 '12 at 21:02
  • I haven't used Eclipse so I don't know how hard it is to force it to just feed the damn file to gcc. How about saving the file and compiling it with gcc yourself? – Alan Curry Jul 26 '12 at 21:04
  • You're so right ! I makefile-d it terminal with `make` and it works great . No idea however why the eclipse is yelling ...:) thanks again – JAN Jul 26 '12 at 21:08
  • As a workaround, you can just use the seconds portion of the struct timespec: `st_mtim.tv_sec` which should be equivalent to `st_mtime` (in fact, `st_mtime` on my system is a macro that expands to `st_mtim.tv_sec`) – Alan Curry Jul 26 '12 at 21:09
1

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!

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • 1
    localtime *returns* a (pointer to) struct tm. Its argument is pointer to time_t. – Alan Curry Jul 26 '12 at 20:13
  • @paulsm4: Thanks but I tried that , my only conclusion is that the Eclipse platform doesn't like `st_mtime` . A simple `makefile` however , runs it like a pro. – JAN Jul 26 '12 at 21:14
  • 1
    @ron - It's not Eclipse per se ... but something in the Eclipse compilation environment is definitely "doing something". My guess is that "something" might be "_POSIX_C_SOURCE": http://stackoverflow.com/questions/8403963/c-stat-struct-does-not-have-st-ctime-field-but-only-st-ctim – paulsm4 Jul 27 '12 at 05:54
1

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).

Giel
  • 2,787
  • 2
  • 20
  • 25
  • :Thanks , but I've tried this already and got `Description Field 'st_mtime' could not be resolved main.c /ex4 line 340 Semantic Error ` . Looks like the type is not known , and I even tried the headers `#include #include #include ` and it still doesn't work – JAN Jul 26 '12 at 20:57
0

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"

Daniel
  • 3
  • 2
0
#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;
}
Abraham
  • 19
  • 4