从现在到现在处理差异的最简单方法是从文件中读取日期并将它们解析为月、日和年 (m, d, y)。如果您不打算对每次转换进行完整验证,那么分隔 4 位数年份和 2 位数月份和日期的简单方法是使用 fscanf 和适当的 field-width 将转换限制为所需位数的修饰符,例如
while (fscanf (fp, "%4d%2d%2d", &y, &m, &d) == 3) {
然后在循环中所需要的就是用年、月和日值填充struct tm(记得从年中减去1900,并将小时、分钟和秒中的每个成员设置为零和夏令时成员-1)。一个简单的函数可以做到这一点,并在调用mktime后返回time_t,例如
time_t fill_broken_down_time (int y, int m, int d)
{ /* initialize struct members */
struct tm bdt = { .tm_sec=0, .tm_min=0, .tm_hour=0, .tm_mday=d,
.tm_mon=m>0?m-1:0, .tm_year=y-1900, .tm_isdst=-1 };
return mktime(&bdt); /* return mktime conversion to time_t */
}
最后,您只需要获取time_t 值并调用difftime 以获取当前时间与从文件中读取的时间之间的秒差作为double 值。在main() 中继续循环,例如
while (fscanf (fp, "%4d%2d%2d", &y, &m, &d) == 3) {
time_t now = time(NULL),
then = fill_broken_down_time (y, m, d);
printf ("date[%d] %d/%d/%d is %.2f seconds from now.\n",
n++, m, d, y, difftime (now, then));
}
总而言之,您可以执行以下操作:
#include <stdio.h>
#include <time.h>
time_t fill_broken_down_time (int y, int m, int d)
{ /* initialize struct members */
struct tm bdt = { .tm_sec=0, .tm_min=0, .tm_hour=0, .tm_mday=d,
.tm_mon=m>0?m-1:0, .tm_year=y-1900, .tm_isdst=-1 };
return mktime(&bdt); /* return mktime conversion to time_t */
}
int main (int argc, char **argv) {
/* use filename provided as 1st argument (stdin by default) */
FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;
int y, m, d, n = 1;
if (!fp) { /* validate file open for reading */
perror ("file open failed");
return 1;
}
while (fscanf (fp, "%4d%2d%2d", &y, &m, &d) == 3) {
time_t now = time(NULL),
then = fill_broken_down_time (y, m, d);
printf ("date[%d] %d/%d/%d is %.2f seconds from now.\n",
n++, m, d, y, difftime (now, then));
}
if (fp != stdin) fclose (fp); /* close file if not stdin */
return 0;
}
(注意:程序期望文件名从作为程序的第一个参数读取日期(或者如果没有给出参数,程序默认从stdin读取))
输入文件示例
$ cat dat/3dates.txt
20190101
20190304
20180922
使用/输出示例
$ ./bin/time_from_now dat/3dates.txt
date[1] 1/1/2019 is 6300212.00 seconds from now.
date[2] 3/4/2019 is 943412.00 seconds from now.
date[3] 9/22/2018 is 15030212.00 seconds from now.
编辑每条评论修改输入文件格式
如果您的数据文件实际上与您最初随问题发布的三行日期不同,并且它包含日期信息之前的标题行,那么您需要在处理日期行之前阅读、识别和处理这些行.由于您希望以天而不是秒为单位输出,您只需将秒数除以86400 即可获得以天为单位的时间差。
要读取句柄标题行,只需将您的读取调整为一次将整行读取到足够大小的缓冲区中。声明一个足够大的常数以确保您的缓冲区足够大,例如
#define MAXC 1024u /* if you need a constant, #define one (or more) */
...
int main (int argc, char **argv) {
...
char buf[MAXC]; /* buffer to hold each line read from file */
然后您将简单地使用sscanf 而不是fscanf 对每一行的信息进行完全相同的解析。如果行格式不满足 yyyymmdd 格式,您知道它不是日期行 - 以任何您想要的方式处理这些行(它们只是在下面的示例中以前缀 "non-date line: " 输出。
将自文件中的时间以来的秒数除以每天86400 秒,您的新读取循环将类似于:
while (fgets (buf, MAXC, fp)) { /* read each line in file */
/* if line isn't a date line, just output line as non-date line */
if (sscanf (buf, "%4d%2d%2d", &y, &m, &d) != 3) {
printf ("non-date line: %s", buf);
continue;
}
time_t now = time(NULL),
then = fill_broken_down_time (y, m, d);
double secs = difftime (now, then); /* get seconds between dates */
printf ("date[%d] %02d/%02d/%04d is %11.2f sec (%g days) from now.\n",
n++, m, d, y, secs, secs / 86400.0);
}
你说:
"I am not able to open the file"
程序希望您提供要读取的文件名作为程序的第一个参数,否则程序默认从stdin 读取。这意味着您必须向程序提供文件名,例如
./yourprogram your_date_file
或者您必须在stdin 上提供该数据,方法是将该信息从某个其他程序的输出传送到该程序,或者简单地将文件重定向为stdin 上的输入,例如
some_utility_making_dates | ./yourprogram
或
./yourprogram < your_date_file
结合所有更改,您的程序将如下所示:
#include <stdio.h>
#include <time.h>
#define MAXC 1024u /* if you need a constant, #define one (or more) */
time_t fill_broken_down_time (int y, int m, int d)
{ /* initialize struct members */
struct tm bdt = { .tm_sec=0, .tm_min=0, .tm_hour=0, .tm_mday=d,
.tm_mon=m>0?m-1:0, .tm_year=y-1900, .tm_isdst=-1 };
return mktime(&bdt); /* return mktime conversion to time_t */
}
int main (int argc, char **argv) {
/* use filename provided as 1st argument (stdin by default) */
FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;
int y, m, d, n = 1;
char buf[MAXC]; /* buffer to hold each line read from file */
if (!fp) { /* validate file open for reading */
perror ("file open failed");
return 1;
}
while (fgets (buf, MAXC, fp)) { /* read each line in file */
/* if line isn't a date line, just output line as non-date line */
if (sscanf (buf, "%4d%2d%2d", &y, &m, &d) != 3) {
printf ("non-date line: %s", buf);
continue;
}
time_t now = time(NULL),
then = fill_broken_down_time (y, m, d);
double secs = difftime (now, then); /* get seconds between dates */
printf ("date[%d] %02d/%02d/%04d is %11.2f sec (%g days) from now.\n",
n++, m, d, y, secs, secs / 86400.0);
}
if (fp != stdin) fclose (fp); /* close file if not stdin */
return 0;
}
带标题的示例输入文件
$ cat dat/3dates-w-headers.txt
This file contains dates to read and convert to days.
The file also contains this description and dates in the format:
yyyymmdd
20190101
20190304
20180922
使用/输出示例
$ ./bin/time_from_now2 dat/3dates-w-headers.txt
non-date line: This file contains dates to read and convert to days.
non-date line: The file also contains this description and dates in the format:
non-date line:
non-date line: yyyymmdd
date[1] 01/01/2019 is 6348645.00 sec (73.4797 days) from now.
date[2] 03/04/2019 is 991845.00 sec (11.4797 days) from now.
date[3] 09/22/2018 is 15078645.00 sec (174.521 days) from now.
检查一下,如果您还有其他问题,请告诉我。