c/c++日期时间的计算程序

本文提供了一系列关于日期和时间处理的代码实例,包括显示当前日期时间、计算未来日期、比较文件修改时间及计算两日期间的时间间隔等。这些实例涵盖了基本日期时间函数的应用,有助于读者理解和掌握日期时间操作的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Listing 1 time1.c - 采用不同格式输出当前的日期和时间

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

#define BUFSIZE 128

main()
{
   time_t tval;
   struct tm *now;
   char buf[BUFSIZE];
   char *fancy_format =
     "Or getting really fancy:\n"
     "%A, %B %d, day %j of %Y.\n"
     "The time is %I:%M %p.";

   /* Get current date and time */
   tval = time(NULL);
   now = localtime(&tval);
   printf("The current date and time:\n"
         "%d/%02d/%02d %d:%02d:%02d\n\n",
     now->tm_mon+1, now->tm_mday, now->tm_year,
     now->tm_hour, now->tm_min, now->tm_sec);
   printf("Or in default system format:\n%s\n",
         ctime(&tval));
   strftime(buf,sizeof buf,fancy_format,now);
   puts(buf);

   return 0;
}

/*  Output
The current date and time:
10/06/92 12:58:00

Or in default system format:
Tue Oct 06 12:58:00 1992

Or getting really fancy:
Tuesday, October 06, day 280 of 1992.
The time is 12:58 PM.
*/

/* End of File */

Listing 2 time2.c -展示如何计算将来某一天的日期以及以秒为单位计算出的执行时间

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

main()
{
   time_t start, stop;
   struct tm *now;
   int ndays;

   /* Get current date and time */
   time(&start);
   now = localtime(&start);

   /* Enter an interval in days */
   fputs("How many days from now? ",stderr);
   if (scanf("%d",&ndays) !=1)
      return EXIT_FAILURE;
   now->tm_mday += ndays;
   if (mktime(now) != -1)
      printf("New date: %s",asctime(now));
   else
      puts("Sorry. Can't encode your date.");

   /* Calculate elapsed time */
   time(&stop);
   printf("Elapsed program time in seconds: %f\n",
     difftime(stop,start));

   return EXIT_SUCCESS;
}

/* Output
How many days from now? 45
New date: Fri Nov 20 12:40:32 1992
Elapsed program time in seconds: 1.000000
*/

/* End of File */

Listing 3 date.h - 一个简单的日期结构

struct Date
{
   int day;
   int month;
   int year;
};
typedef struct Date Date;

Date* date_interval(const Date *, const Date *);
/* End of File */

Listing 4 date_int.c - 计算两个日期的间隔

/* date_int.c: Compute duration between two dates */

#include "date.h"

#define isleap(y) \
 ((y)%4 == 0 && (y)%100 != 0 || (y)%400 == 0)

static int Dtab [2][13] =
{
  {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
  {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};

Date *date_interval(const Date *d1, const Date *d2)
{
   static Date result;
   int months, days, years, prev_month;

   /* Compute the interval - assume d1 precedes d2 */
   years = d2->year - d1->year;
   months = d2->month - d1->month;
   days = d2->day - d1->day;

   /* Do obvious corrections (days before months!)
    *
    * This is a loop in case the previous month is
    * February, and days < -28.
    */
   prev_month = d2->month - 1;
   while (days < 0)
   {
      /* Borrow from the previous month */
      if (prev_month == 0)
         prev_month = 12;
      --months;
      days += Dtab[isleap(d2->year)][prev_month--];
   }

   if (months < 0)
   {
      /* Borrow from the previous year */
      --years;
      months += 12;
   }

   /* Prepare output */
   result.month = months;
   result.day = days;
   result.year = years;
   return &result;
}
/* End of File */

Listing 5 tdate.c - 举例说明日期间隔函数的使用

/* tdate.c: Test date_interval() */

#include <stdio.h>
#include <stdlib.h>
#include "date.h"

main()
{
   Date d1, d2, *result;
   int nargs;

   /* Read in two dates - assume 1st precedes 2nd */
   fputs("Enter a date, MM/DD/YY> ",stderr);
   nargs = scanf("%d/%d/%d%*c", &d1.month,
     &d1.day, &d1.year);
   if (nargs != 3)
      return EXIT_FAILURE;

   fputs("Enter a later date, MM/DD/YY> ",stderr);
   nargs = scanf("%d/%d/%d%*c", &d2.month,
     &d2.day, &d2.year);
   if (nargs != 3)
      return EXIT_FAILURE;

   /* Compute interval in years, months, and days */
   result = date_interval(&d1, &d2);
   printf("years: %d, months: %d, days: %d\n",
      result->year, result->month, result->day);
   return EXIT_SUCCESS;

}
/* Sample Execution:
Enter a date, MM/DD/YY> 10/1/51
Enter a later date, MM/DD/YY> 10/6/92
years: 41, months: 0, days: 5 */
/* End of File */

Listing 6 ftime.c - 确定是否time1.c比time2.c更新

/* ftime.c: Compare file time stamps */

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>

main()
{
   struct stat fs1, fs2;

   if (stat("time1.c",&fs1) == 0 &&
      stat("time2.c",&fs2) == 0)
   {
      double interval =
        difftime(fs2.st_mtime,fs1.st_mtime);

      printf("time1.c %s newer than time2.c\n",
        (interval < 0.0) ? "is" : "is not");
      return EXIT_SUCCESS;
   }
   else
      return EXIT_FAILURE;
}
/* Output
time1.c is not newer than time2.c */
/* End of File */

Listing 7 touch.c -通过覆盖旧文件或者创建一个新的文件来更新时间戳

/* touch.c: Update a file's time stamp */

#include <stdio.h>

void touch(char *fname)
{
   FILE *f = fopen(fname,"r+b");
   if (f != NULL)
   {
      char c = getc(f);
      rewind(f);
      putc(c,f);
   }
   else
      fopen(fname,"wb");

   fclose(f);
}

/* End of File */

(1) 测试日期类成员函数,在主函数中列出菜单选项,可以完成日期的加减比较等测试功能。 (2) 完善程序功能,在日期相加的菜单选项中增加日期加天数,结果为新日期;日期家月份,结果为新日期,要考虑闰年情况。 (3) 完善程序功能,在日期相减的菜单选项中增加日期减天数,结果为新日期;日期减月份,结果为新日期,要考虑闰年情况。 (4) 显示日期时增加显示星期及英文形式的月份的功能。 (5) 增加输入的甄别功能,即输入非法数据(如负数、日期超过31天、时间超过24小时等情况)的识别显示功能。 (1) 仿照日期类编写时间类CTime_t,可以完成时间的设置、运算、比较等功能。 (2) 增加时间的输入功能,既可以选择输入格式,可以输入hh:mm:ss格式的信息。 (3) 增加时间的输出格式,可以输出12小时的时间格式。 (4) 编写时间和日期的派生类CDati,完成日期与时间的联合设置、运算、比较等功能,要求该派生类可以完成:日期时间加天数或时间等于新的日期时间日期时间减天数或等于新的日期时间日期时间相减等于天数或时间等工作,在程序中考虑闰年等具体情况,并重载各种运算符。 (5) 增加输入的甄别功能,即输入非法数据,即输入非法数据(如负数、日期超过31天、时间超过24小时等情况)的识别显示功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值