需要帮助编程C在一起比较时间

时间:2010-01-27 13:42:05

标签: c datetime date comparison

我需要一些建议并帮助我为Nagios写的插件。

我正在用C语言编写插件,但是在尝试使这个插件工作的时候除了少量的语言之外没有该语言的经验。

基本上我想要做的是以下内容。

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

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

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

我的第二次尝试是生成两个Time Structs,其中一个包含当前时间,另一个包含我的“自制”时间并比较它们,但这也不起作用。

继承我的代码,无论我尝试从文件中的日期总是与当前系统时间相同,我知道必须将时间设置在顶部。

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 btw上完美编译)。

#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;

为帮助人员欢呼。

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

在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;
}

答案 3 :(得分:0)

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


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

您的代码出了什么问题:
您正在使用和修改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); 返回的指针,这两种情况恰好相同 因此,你的指针localtimetime_from_file实际上指向了同一个位置。任何一个修改都会修改另一个,最后你的差异为零。
(感谢gdb帮助我弄清楚!!:D)