[C] 日期 →→→→→进入此内容的聊天室

来自 , 2020-07-20, 写在 C, 查看 197 次.
URL http://www.code666.cn/view/a9365bd9
  1. /***************************************************************************************
  2. 建立头文件"date.h"
  3. ****************************************************************************************/
  4. #ifndef __DATE_H
  5. #define __DATE_H
  6.  
  7. #include "stm8_macro.h"
  8. #include "ds1302.h"
  9.  
  10. long DaysApart(u16 year1, u8 month1, u8 day1, u16 year2, u8 month2, u8 day2);
  11. u8   GetDayofWeek(u16 year, u8 month, u8 day);
  12. void DateStep(u16* year, u8* month, u8* day);
  13. void DateBack(u16* year, u8* month, u8* day);
  14.  
  15. #endif /* __TIME_DATE_H */
  16.  
  17. /***************************************************************************************
  18. 建立源文件"date.c"
  19. ****************************************************************************************/
  20. #include "date.h"
  21.  
  22. /*
  23.  @Function : 是否是闰年
  24.  @Input    : year 年份
  25.  @Output   : 1    闰年
  26.              0    平年
  27.  **/
  28. static u8 IsLeapYear(u16 year)
  29. {
  30.         if( (year%100 == 0 && year%400 == 0) ||
  31.             (year%100 != 0 && year%4   == 0)   )
  32.         {
  33.                 return 1;
  34.         }
  35.         else
  36.                 return 0;
  37. }
  38.  
  39. /*
  40.  @Function : 计算某个月份的天数
  41.  @Input    : year  年份
  42.              month 月份
  43.  @Output   : 天数
  44.  **/
  45. static u8 GetDaysInMonth(u16 year, u8 month)
  46. {
  47.         u8 days = 0;
  48.  
  49.         switch(month)
  50.         {
  51.                 case 2:
  52.                         if(IsLeapYear(year))
  53.                                 days = 29;
  54.                         else
  55.                                 days = 28;
  56.                         break;
  57.                 case 4:
  58.                 case 6:
  59.                 case 9:
  60.                 case 11:
  61.                         days = 30;
  62.                         break;
  63.                 default:
  64.                         days = 31;
  65.                         break;
  66.         }
  67.         return days;
  68. }
  69.  
  70. /*
  71.  @Function:  计算两个日期相隔天数,年份
  72.  @Input   :  year1,month1,day1  日期1
  73.                           year2, month2, day2  日期2
  74.  @Output  :  0xffff: 出错
  75.                       other : 两个日期相隔的天数,日期2 - 日期1
  76.                         < 0 : 表示日期1 > 日期2
  77.  **/
  78. long DaysApart(
  79.  u16 year1, u8 month1, u8 day1,
  80.  u16 year2, u8 month2, u8 day2)
  81. {
  82.         u16  i;
  83.         long res_days = 0;
  84.  
  85.         //日期格式错误,error
  86.         if( month1 >12 ||  month2 >12 ||
  87.             day1   >31 ||  day2   >31 ||
  88.            !year1      || !year2      ||
  89.            !month1     || !month2     ||
  90.            !day1       || !day2        )
  91.         {
  92.                 return 0xffff;
  93.         }
  94.  
  95.         //日期1比日期2后
  96.         if( ( year1 > year2) ||
  97.             ((year1 == year2) && (month1 > month2)) ||
  98.             ((year1 == year2) && (month1 == month2) && (day1 > day2)))
  99.         {
  100.                 res_days = -1 * DaysApart(year2, month2, day2,
  101.                                               year1, month1, day1);
  102.                 return res_days;
  103.         }
  104.  
  105.         //天数计算
  106.         if(year1 != year2)//不同年份
  107.         {
  108.                 //第一年的天数
  109.                 res_days = GetDaysInMonth(year1, month1) - day1;
  110.                 for(i=month1+1; i<=12; i++)
  111.                 {
  112.                         res_days += GetDaysInMonth(year1, (u8)i);
  113.                 }
  114.                 //最后一年的天数
  115.                 for(i=1; i<month2; i++)
  116.                 {
  117.                         res_days += GetDaysInMonth(year2, (u8)i);
  118.                 }
  119.                 res_days += day2;
  120.                 //中间年份天数
  121.                 for(i=year1+1; i<year2; i++)
  122.                 {
  123.                         if(IsLeapYear((u16)i))
  124.                                 res_days += 366;
  125.                         else
  126.                                 res_days += 365;
  127.                 }
  128.         }
  129.         else//相同年份
  130.         {
  131.                 if(month1 != month2)//不同月份
  132.                 {
  133.                         res_days = GetDaysInMonth(year1, month1) - day1 + day2;
  134.                         for(i=month1+1; i<month2; i++)
  135.                         {
  136.                                 res_days += GetDaysInMonth(year1, (u8)i);
  137.                         }
  138.                 }
  139.                 else//相同月份
  140.                 {
  141.                         res_days = day2 - day1;
  142.                 }
  143.         }
  144.         return res_days;
  145. }
  146.  
  147. /*
  148.  @Function: 获取星期几
  149.  @Input   : year month day,日期须大于公元1世纪.01.01
  150.  @Output  : 星期  1~7
  151.  **/
  152. u8 GetDayofWeek(u16 year, u8 month, u8 day)
  153. {
  154.         long days = 0;
  155.         u8   dayofweek;
  156.  
  157.         days = DaysApart(2001, 1,  1, //2001.01.01,星期一,以此作参照
  158.                                          year, month, day);
  159.         if(days >= 0)
  160.         {
  161.                 dayofweek = (u8)((days + 1)%7);
  162.                 if(dayofweek == 0)
  163.                         dayofweek = 7;
  164.         }
  165.         else
  166.         {
  167.                 dayofweek = (u8)((-1*days)%7);
  168.                 dayofweek = (u8)((dayofweek == 0)?(1):((8-dayofweek)));
  169.         }
  170.         return dayofweek;
  171. }
  172.  
  173. /*
  174.  @Function: 日期步进,日期前进一天
  175.  @Input   : year month day
  176.  @Output  : None
  177.  **/
  178. void DateStep(u16* year, u8* month, u8* day)
  179. {
  180.         u8 daysofMonth;
  181.  
  182.         daysofMonth = GetDaysInMonth(*year, *month);
  183.  
  184.         if(++(*day) > daysofMonth)
  185.         {
  186.                 *day = 1;
  187.                 if(++(*month) > 12)
  188.                 {
  189.                         *month = 1;
  190.                         ++(*year);
  191.                 }
  192.         }
  193. }
  194.  
  195. /*
  196.  @Function: 日期步进,日期倒退一天
  197.  @Input   : year month day
  198.  @Output  : None
  199.  **/
  200. void DateBack(u16* year, u8* month, u8* day)
  201. {
  202.         if(--(*day) == 0)
  203.         {
  204.                 if(--(*month) == 0)
  205.                 {
  206.                         *month = 12;
  207.                         --(*year);
  208.                 }
  209.                 *day = GetDaysInMonth(*year, *month);
  210.         }
  211. }
  212.  
  213.  
  214.  

回复 "日期"

这儿你可以回复上面这条便签

captcha