【问题标题】:Need Help Programming in C comparing times together需要帮助 用 C 语言编程比较时间
【发布时间】:2010-01-27 13:42:04
【问题描述】:

我需要一些关于我为 Nagios 编写的插件的建议和帮助。

我正在用 C 语言编写插件,但在尝试让这个插件工作时,除了少量的经验外,没有以前的语言经验。

基本上我要做的是以下。

读取由我编写的应用程序在远程 PC 上生成的文本文件,该程序向文件中写入的字符不超过 5 个,前 4 个字符是 24 小时格式的时间。例如22:30 > 晚上 10:30

然后它需要将这 4 个字符转换成一个时间,并将其与当前系统时间进行比较(如果有 5 分钟的差异,则它会向 nagios 生成一个回复以标记警告)。

我尝试了很多不同的方法,我的第一次尝试是将字符转换为整数,然后将时间转换为整数并比较差异..这样做失败了。

我的第二次尝试是生成两个时间结构,一个是当前时间,另一个是我的“自制”时间,然后比较它们,但这也不起作用。

这是我的代码,无论我尝试什么,文件中的日期始终与当前系统时间相同,我知道它与必须在顶部设置时间有关。

t_of_file = time(NULL);
time_from_file = localtime(&t_of_file);

但如果我不这样做,我会遇到分段错误。

这是代码。

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#define COPYMODE 0644

int main(int argc, char *argv[])
{


    struct tm *time_from_file;
    struct tm *the_system_time;

    time_t t_of_file;
    time_t t_of_sys;

    t_of_sys = time(NULL);
    the_system_time = localtime(&t_of_sys);

    t_of_file = time(NULL);
    time_from_file = localtime(&t_of_file);



    time_from_file->tm_year = the_system_time->tm_year;
    time_from_file->tm_mon  = the_system_time->tm_mon;
    time_from_file->tm_mday = the_system_time->tm_mday;
    time_from_file->tm_hour = 10; //should be read in from file
    time_from_file->tm_min  = 30; //should be read in from file
    time_from_file->tm_sec  = the_system_time->tm_sec;
    time_from_file->tm_isdst = the_system_time->tm_isdst;

    t_of_file = mktime(time_from_file);
    printf("%s\n",ctime(&t_of_file));

    t_of_sys = mktime(the_system_time);
    printf("%s\n",ctime(&t_of_sys));

    double difference = difftime(t_of_file, t_of_sys );

    printf("%lf\n",(double)t_of_file);
    printf("%lf\n",(double)t_of_sys);

    if (difference >= 0.0) { //this should be 5 mins, not quite sure what to put here yet
        // second is later than first
        printf("later\n");
    }
    else if (difference < 0.0) {
        // second is earlier than first
        printf("earlier\n");
    }
    printf("%lf\n", difference);


    return 0;//STATE_OK;
}

您能提供的任何帮助将不胜感激。

根据我得到的答案,PACE 的答案是我想做的事情,现在我有一个更简单的代码,它非常适合我想要做的事情并且更容易理解。下面是修改后的代码(顺便说一句,它可以在 Linux 上完美编译)。

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

int main ()
{
  time_t filetime;
  time_t presenttime;

  struct tm * timeinfo;

  time ( &filetime );
  time ( &presenttime);

  timeinfo = localtime ( &filetime );
  timeinfo->tm_hour = 14; //this should be entered from file
  timeinfo->tm_min = 15; //this should be entered from file
  filetime = mktime ( timeinfo );

  printf("my time %s\n",ctime(&filetime));
  printf("pc time %s\n",ctime(&presenttime));


    double difference = difftime(filetime, presenttime );

    printf("%lf\n",(double)filetime);
    printf("%lf\n",(double)presenttime);

    if (difference > 300.0) {
        // second is later than first
        printf("later\n");
    }
    else if (difference < 0.0) {
        // second is earlier than first
        printf("earlier\n");
    }
    printf("%lf\n", difference);


  return 0;

为帮助的家伙干杯。

【问题讨论】:

    标签: c datetime date comparison


    【解决方案1】:

    有一个制作时间的例子here。然后您可以使用difftime 方法比较时间。

    【讨论】:

    • 他已经在他的代码中使用了这两种方法,方式与链接中描述的相同。
    • 为 Pace 的信息干杯,我已经修改了我的原始帖子以包含我正在尝试做的新工作代码,以防其他人遇到这个问题。
    【解决方案2】:

    如果是我,我会很想将 time() 的结果保存到文件中。这将在另一端节省大量的字符串解析工作。

    【讨论】:

    • 这并没有提供问题的答案。要批评或要求作者澄清,请在其帖子下方发表评论。
    • @DannyStaple - 我想这取决于您认为问题的本质是什么。已经5年了,但我认为我之前的化身试图指出他使用字符串给自己带来了很多麻烦,也许他的问题的解决方案就是不这样做。如果有人问最好的方法来敲螺丝,我认为告诉他改用螺丝刀是对这个问题的完全有效的答案。
    【解决方案3】:

    我修改了您的代码如下,它现在可以满足您的期望:

    
    struct tm time_from_file;
    struct tm the_system_time;
    
    
    time_t t_of_file;
    time_t t_of_sys;
    
    t_of_sys = time(NULL);
    memcpy(&the_system_time,localtime(&t_of_sys),sizeof(struct tm));
    
    t_of_file = time(NULL);
    memcpy(&time_from_file,localtime(&t_of_sys),sizeof(struct tm));
    
    time_from_file.tm_hour = 10; //should be read in from file
    time_from_file.tm_min  = 30; //should be read in from file
    
    t_of_file = mktime(&time_from_file);
    printf("%s\n",ctime(&t_of_file));
    printf("%s\n",ctime(&t_of_sys));
    
    double difference = difftime(t_of_file, t_of_sys );
    
    printf("%lf\n",(double)t_of_file);
    printf("%lf\n",(double)t_of_sys);
    
    printf("%lf\n", difference);
    

    您的代码出了什么问题:
    您正在使用和修改 localtime 返回的指针,这在两种情况下都是相同的。
    因此,您的指针 time_from_filethe_system_time 实际上指向同一个位置。对一个位置的任何修改也会修改另一个位置,最后您的差异为零。
    (感谢 gdb 帮我解决这个问题!!:D)

    【讨论】:

    • 嗨 Neeraj,感谢您的解决方案,我感觉这与指针和内存位置有关,但我的 c 知识非常基础,所以我不知道我需要做什么要解决这个问题。谢谢你的建议。
    【解决方案4】:

    在 C 中很容易调整格式。使用它从文件中读取时间组件:

    int hr, min; 
    fscanf (file, "%d:%d", &hr, &min);
    

    这是一个完整的解决方案:

    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    struct HrMin_ {
      int hr;
      int min;
    };
    typedef struct HrMin_ HrMin;
    
    static const double TIME_DIFFERENCE = (double) (5 * 1000);
    
    static HrMin read_time (FILE* file);
    
    int
    main (int argc, char **argv)
    {
      time_t sys_time_t = 0;
      struct tm *sys_time = NULL;
      struct tm *file_time = NULL;
      double d = 0.0f;
    
      if (argc != 2)
        {
          printf ("Usage: time_cmp <time_file>\n");
          return 1;
        }
    
      time (&sys_time_t);
    
      sys_time = localtime (&sys_time_t);
      file_time = malloc (sizeof (struct tm));
    
      file_time->tm_sec = sys_time->tm_sec;
      file_time->tm_min = sys_time->tm_min;
      file_time->tm_hour = sys_time->tm_hour;
      file_time->tm_mday = sys_time->tm_mday;
      file_time->tm_mon = sys_time->tm_mon;
      file_time->tm_year = sys_time->tm_year;
      file_time->tm_wday = sys_time->tm_wday;
      file_time->tm_yday = sys_time->tm_yday;
      file_time->tm_isdst = sys_time->tm_isdst;  
    
      FILE *file = fopen (argv[1], "r");
      if (file == NULL)
        {
          printf ("Failed to open file: %s\n", argv[1]);
          return 1;
        }
      HrMin hr_min = read_time (file);
      fclose (file);
    
      file_time->tm_hour = hr_min.hr;
      file_time->tm_min = hr_min.min;
    
      d = difftime (sys_time_t, mktime (file_time));
      free (file_time);
    
      if (d < 0) d *= -1;
      printf ("Diff: %f\n", d);
      if (d >= TIME_DIFFERENCE)
        printf ("WARN!\n");
    
      return 0;
    }
    
    static HrMin 
    read_time (FILE *file)
    {
      HrMin hr_min;
      hr_min.hr = 0;
      hr_min.min = 0;
      fscanf (file, "%d:%d", &hr_min.hr, &hr_min.min);
      return hr_min;
    }
    

    【讨论】:

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