【问题标题】:Any specific reason why localtime throws warning with struct tm* & stat* , in linux ?localtime 在 linux 中使用 struct tm* & stat* 引发警告的任何具体原因?
【发布时间】:2012-07-26 19:48:11
【问题描述】:

我有这个简单的代码(项目的一部分):

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

    struct tm* time;

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


        // code 

}

eclipse 不断向我发出警告:

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

知道如何解决这个问题吗?谢谢

【问题讨论】:

    标签: c linux time stat localtime


    【解决方案1】:

    st_mtim 是一个结构时间规范(秒和纳秒)。你想要st_mtime。

    【讨论】:

    • 艾伦请注意,Eclipse 这次显示错误(添加e 后):` Field 'st_mtime' could not be resolved main.c /ex4 line 342 Semantic Error `
    • Eclipse 错了 :-) 说真的。把它砸在头上,直到它尊重你的权威。或者将 eclipse 标签添加到这个问题中,或者提出单独的后续问题......这只是一个 Eclipse 错误报告。
    • 我希望 :) 但它不会与最后那个可爱的 e 一起编译
    • 我没有使用过 Eclipse,所以我不知道强制它只将该死的文件提供给 gcc 有多难。保存文件并自己用 gcc 编译怎么样?
    • 你说的太对了!我用make makefile-d it 终端,效果很好。但是不知道为什么日食会大喊大叫...:) 再次感谢
    【解决方案2】:

    你会想用这个来代替:

    time = localtime(&file->st_mtime);
    

    注意末尾添加的“e”。 st_mtim 是一个 timespec,加上 'e' 它是一个 time_t(你需要什么)。

    【讨论】:

    • :谢谢,但我已经尝试过了,得到了Description Field 'st_mtime' could not be resolved main.c /ex4 line 340 Semantic Error 。看起来类型未知,我什至尝试了标题#include <sys/types.h> #include <sys/stat.h> #include <unistd.h> ,它仍然不起作用
    【解决方案3】:

    完全改变答案:

    建议:

    1) 确保您#include 这些标头:

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

    2) 将指针指向“const”

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

    3) 发回发生的事情

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

    其他建议:

    1) 请阅读这两个链接:

    自内核 2.5.48 起,stat 结构支持纳秒级分辨率 对于三个文件时间戳字段。 Glibc 暴露纳秒 每个字段的组件使用 st_atim.tv_nsec 形式的名称,如果 _BSD_SOURCE 或 _SVID_SOURCE 功能测试宏被定义。这些字段在 POSIX.1-2008 中指定,并且从版本 2.12 开始, 如果定义了 _POSIX_C_SOURCE,glibc 也会公开这些字段名 值为 200809L 或更大,或 _XOPEN_SOURCE 定义为 值 700 或更大。如果没有上述宏 定义,然后纳秒值以形式的名称公开 st_atimensec。在不支持亚秒级的文件系统上 时间戳,纳秒字段返回值为 0。

    2) 显然,makefile(“工作”)有一个 Eclipse 没有的#define,反之亦然。

    可能是 _POSIX_C_SOURCE 和/或 _XOPEN_SOURCE 之一/两者。

    运行此命令查看命令行(makefile?)环境中存在什么:

    gcc -dM -E - &lt; /dev/null | less

    3) 请发回您找到的内容!

    【讨论】:

    • localtime 返回一个(指向)struct tm。它的参数是指向 time_t 的指针。
    • @paulsm4:谢谢,但我试过了,我唯一的结论是 Eclipse 平台不喜欢 st_mtime。然而,一个简单的makefile 可以像专业人士一样运行它。
    • @ron - 它本身不是 Eclipse ......但 Eclipse 编译环境中的某些东西绝对是“做某事”。我的猜测是“某事”可能是“_POSIX_C_SOURCE”:stackoverflow.com/questions/8403963/…
    【解决方案4】:

    我在 Eclipse 上遇到了同样的问题: 字段 st_mtime 无法解析(语义错误)

    通过右键单击项目,选择索引->“刷新所有文件”修复了 Eclipse 中的问题

    【讨论】:

      【解决方案5】:
      #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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多