日期加减算法

    自定义的时间格式,与C语言中的struct tm有点区别。此代码仅供参考。如果发现代码中的错误,请留言或是给我电子邮件,不胜感谢!

  1. /*
  2.  *  Already be tested in linux with gcc 4.1.2.
  3.  *  Already be tested in windows with VC2005.
  4.  *  Please contact me with any bugs in these two functions.
  5.  *  email: ka-bar_strider@hotmail.com
  6.  *                                   ka-bar, 2008-12-16
  7.  */
  8. #include <stdio.h>
  9. typedef struct tagdate 
  10. {
  11.     int year;
  12.     int mon;
  13.     int day;
  14.     int hour;
  15.     int min;
  16.     int sec;
  17. }mathdate,*mathdateptr;
  18. void date_add(mathdateptr orig_date, int days)
  19. {
  20.     int monthdays[2][13] = {{0,31,28,31,30,31,30,31,31,30,31,30,31},
  21.                         {0,31,29,31,30,31,30,31,31,30,31,30,31}};
  22.     for ( ; ; orig_date->mon = 1, orig_date->year++)  //for year
  23.     {
  24.         int leapyear = orig_date->year%4 == 0 ? 1:0;
  25.         for ( ; ; orig_date->mon++) //for month
  26.         {
  27.             int cur_mon_days = monthdays[leapyear][orig_date->mon];
  28.             if ((days -= (cur_mon_days - orig_date->day)) > 0)
  29.             {
  30.                 orig_date->day = 0;
  31.                 if (orig_date->mon >= 12)
  32.                     break;
  33.             }
  34.             else
  35.             {
  36.                 orig_date->day = cur_mon_days + days;
  37.                 return;
  38.             }
  39.         }//end for month
  40.     }//end for year
  41. }
  42. void date_sub(mathdateptr orig_date, int days)
  43. {
  44.     int monthdays[2][13] = {{0,31,28,31,30,31,30,31,31,30,31,30,31},
  45.                             {0,31,29,31,30,31,30,31,31,30,31,30,31}};
  46.     do 
  47.     {
  48.         if ((days -= orig_date->day) <= 0)
  49.         {
  50.             orig_date->day = 0 - days;
  51.             return;
  52.         }
  53.         else
  54.         {
  55.             int leapyear = 0;
  56.             if (--orig_date->mon < 1)
  57.             {
  58.                 orig_date->year--;
  59.                 orig_date->mon = 12;
  60.                 leapyear = orig_date->year%4 == 0 ? 1:0;
  61.             }
  62.             orig_date->day = monthdays[leapyear][orig_date->mon];
  63.         }
  64.     } while(1);// month
  65. }
  66. int main()
  67. {
  68.     mathdate orig_date = {2007,1,26,0,0,0};
  69.     printf("%04d%02d%02d-%02d:%02d:%02d/n",
  70.         orig_date.year,orig_date.mon,orig_date.day,orig_date.hour,orig_date.min,orig_date.sec);
  71.     date_sub(&orig_date,712);
  72.     printf("%04d%02d%02d-%02d:%02d:%02d/n",
  73.         orig_date.year,orig_date.mon,orig_date.day,orig_date.hour,orig_date.min,orig_date.sec);
  74.     date_add(&orig_date,712);
  75.     printf("%04d%02d%02d-%02d:%02d:%02d/n",
  76.         orig_date.year,orig_date.mon,orig_date.day,orig_date.hour,orig_date.min,orig_date.sec);
  77.     getchar();
  78.     return 0;
  79. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值