从文件转换时间并与当前时间进行比较

Time convert from a file and compare to the current time

我之前问过类似的问题,但看起来,我从文件中获取的时间并没有转换为您从 localtime() 和 time() 中获取的相同类型的时间。

正是我想要的是:

我有一个包含以下信息的 txt 文件

order.txt

file1.txt;5;15:40

file2.txt;7;15:41

file1.txt;10;16:00

我的objective是获取hour:min(15:40)的时间然后,如果和currentTime一样,然后我显示file1.txt的内容。否则,如果 currentTime 低于 hour:min 时间 (15:40),它会等到 15: 40 并显示 file1.txt 内容。如果 currentTime 更高,则忽略第一个并转到下一个。

例如,如果当前时间15:39,它将等待1分钟 然后显示 file1.txt 内容。 如果当前时间15:40,则显示file1.txt内容,无需等待。 如果当前时间15:41,则直接转到下一个(file2.txt) 并再次检查。

使用变量

FILE* orderFile;
FILE* contentFile;
FILE* defaultFile; (This will be used when this code works)
char fileName[50];
char textContent[5000];
int seconds;
int hour, min;
int diff_time;
char timeValue[50];
time_t current;

包括

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

为此,我尝试了以下代码:

orderFile = fopen("order.txt","r");
defaultFile = fopen("default.txt","r");

while(fscanf(orderFile," %49[^;];%d; %49[^\n]",fileName,&seconds,timeValue)
{

  sscanf(timeValue,"%d:%d",&hour,&min);

  contentFile = fopen(fileName,"r");
            
  if(contentFile != NULL)
  {
     fseek (contentFile, 0, SEEK_END);
     size = ftell(contentFile);
     rewind(contentFile);
                    
     if(size == 0)
     {
       printf("\nEmpty file. (Scanned file: %s)\n", fileName);
       fclose(contentFile);
     }
     else
     {
                    
         time_t now;
                        time(&now);

                        struct tm file_time_tm;
                        file_time_tm = *localtime(&now);
                        file_time_tm.tm_hour = hour;
                        file_time_tm.tm_min = min;
                        file_time_tm.tm_sec = 0;
                        file_time_tm.tm_isdst = -1;
                        time_t fileTime = mktime(&file_time_tm);

                        double diff_time = difftime(fileTime, now);
                        
                        if(diff_time == 0) 
                        {
                            while(fscanf(contentFile,"%[^\t]",textContent) == 1)
                            {
                                printf("%s\n", textContent);
                            }
                            sleep(seconds);
                        }
                        else
                        {
                            if(diff_time > 0)
                            {
                                while(fscanf(defaultFile,"%[^\t]",defaultContent) == 1)
                                {
                                    printf("%s\n", defaultContent);
                                }
                                sleep(diff_time);
                                
                                while(fscanf(contentFile,"%[^\t]",textContent) == 1)
                                {
                                    printf("%s\n", textContent);
                                }
                                sleep(seconds);
                            }
                        }
                    
            fclose(defaultFile);      
            fclose(contentFile);
     }
   }
   else
   {
     if(contentFile == NULL)
     {
       printf("\nFile does not exist. (Scanned file: %s)\n", fileName);
       fclose(contentFile);
     }
   }
}

fclose(orderFile);
printf("\n");

多亏了 chux,它现在工作正常,但还没有完全工作。

如果我有以下情况:

当前时间: 15:00

order.txt

file1.txt;5;15:01

file2.txt;6;15:02

file3.txt;3;15:03

当我运行程序时,会出现以下情况:

DEFAULT MESSAGE (text inside default.txt)

WAITS DIFF_TIME (CORRECTLY DONE)

SHOWS CONTENT FROM THE file1.txt

WAITS 5 SECONDS

X - SHOWS CONTENT FROM THE file2.txt (This is wrong, it should check again and if the time is 15:02 it will show, not 15:01. Which I assume it's because it is still reading as 15:01 on the timeValue and not 15:02, any idea why?)

WAITS UNTILL IT'S 15:02 (wrong)

SHOWS file3.txt (wrong)

WAITS UNTILL IT'S 15:03

ENDS (wrong as it should not end after waiting, it should end when file3.txt shows and waits for 3 seconds)

我需要的输出:

DEFAULT MESSAGE

waits diff_time

file1.txt content

waits 5 seconds

DEFAULT MESSAGE

waits diff_time

file2.txt content

waits 7 seconds

DEFAULT MESSAGE

waits diff_time

file3.txt content

waits 3 seconds

ENDS

如果我同时有两个文件,它也不能正常工作,例如:

order.txt

file1.txt;5;15:01

file2.txt;6;15:01

file3.txt;3;15:02

它也打破了它。 知道如何正确执行此操作吗?

正如@M Oehm 上面的评论,很明显编译器警告没有完全启用。建议启用它们 - 它会节省您的时间。

问题出在时间比较和减法上

// Troublesome code
if(currentTime->tm_hour < hour && currentTime->tm_min < min) {
  diff_time = difftime(currentTime,timeValue);
  printf("%d - Or this?\n", diff_time);
  sleep(diff_time);
}
  1. 比较好奇。通常这种比较的形式是

    if (h1 < h2 || (h1 == h2 && m1 < m2))
    
  2. difftime 需要 2 个 time_t 参数,而不是 char timeValue[50] 和一个 struct tm.

  3. 传递给 double difftime(time_t time1, time_t time0) 的值向后显示。结果呢time1 - time0.

改为推荐

    int current_hm = currentTime->tm_hour * 60 + currentTime->tm_min;
    int file_hm = hour * 60 + min;

    if (current_hm < file_hm) {
      unsigned diff_time = (file_hm - current_hm)*60u;
      printf("%u - Or this?\n", diff_time);
      sleep(diff_time);
    }

[编辑] 简化方法。

    time_t now;
    time(&now);

    struct tm file_time_tm;
    file_time_tm = *localtime(&now);
    file_time_tm.tm_hour = hour;  // from file
    file_time_tm.tm_min = min; // from file
    file_time_tm.tm_sec = 0;
    file_time_tm.tm_isdst = -1;
    time_t filetime = mktime(&file_time_tm);

    double diff = difftime(filetime, now);  // filetime - now
    if (diff > 0) {
      sleep((unsigned) diff);
    }

Chux 的建议是用您自己的表示午夜后分钟数的整数进行比较,这在您的情况下可能更容易,但如果您想使用标准时间机制,您可以这样做。

但首先,请记住有两种时间表示,time_t,这是一个 "numerical" 时间,它存储纪元之后的秒数,struct tm,这是将时间分解为年、月、日等的人类可读时间。

使用 strptime, you can scan a string accoding to a format specification. It is the scanf for struct tm. This function does not initialise fields that are not specified, so that you can assign the current time with localtime 然后仅覆盖小时和分钟字段。

mktimestruct tm 转换为 time_t,您可以将其传递给 difftime。

以下截取的代码显示了这种比较的工作原理:

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

int main()
{
    char stamp[] = "12:53";
    char *p;
    struct tm now;
    time_t t = time(NULL);

    now = *localtime(&t);
    p = strptime(stamp, "%H:%M", &now);

    if (p == NULL || *p != '[=10=]') {
        printf("Illegal date.\n");
    } else {
        char buf[20];
        double dsec;
        int dmin;

        strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M\n", &now);
        puts(buf);

        dsec = difftime(t, mktime(&now));
        printf("Diff: %g sec\n", dsec);

        dmin = (int) dsec / 60;
        printf("Diff: %d min\n", dmin);
    }

    return 0;
}