Chinaunix首页 | 论坛 | 博客
  • 博客访问: 398927
  • 博文数量: 75
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 645
  • 用 户 组: 普通用户
  • 注册时间: 2015-06-03 18:24
文章分类

全部博文(75)

文章存档

2019年(1)

2018年(20)

2017年(14)

2016年(10)

2015年(30)

分类: LINUX

2018-01-08 21:36:03


点击(此处)折叠或打开

  1. #include <stdio.h>

  2. /**/
  3. typedef struct _date_time_{
  4.     unsigned char year;/*[0,99]*/
  5.     unsigned char mon;/*[1,12]*/
  6.     unsigned char mday;/*[1,31]*/
  7.     unsigned char hour;/*[0,23]*/
  8.     unsigned char min;/*[0,59]*/
  9.     unsigned char sec;/*[0,59]*/
  10.     unsigned char week;/*[0,6]*/
  11. }date_time_t;


  12. /*
  13.     这个函数将时间转换成从1970年1月1日0小时0分0秒到你输入的时间所经过的秒数,溢出时间为2106-02-07 06:28:16。
  14.     若你要计算2000-02-07 06:28:16 到2000-02-09 06:28:16 所经过的秒数:
  15.     unsigned long time1 = Dt_mktime_abs(2000, 2, 7, 6, 28, 16) - Dt_mktime_abs(2000, 2, 9, 6, 28, 16)
  16. */
  17. unsigned long Dt_mktime_abs(date_time_t *pTm)
  18. {
  19.     int year/*[2000, 2099]*/ = pTm->tm_year/*[00, 99]*/ + 2000;
  20.     int mon/*[1, 12]*/ = pTm->tm_mon/*[1, 12]*/;
  21.     int day/*[1, 31]*/ = pTm->tm_mday/*[1, 31]*/;
  22.     int hour = pTm->tm_hour/*[0, 23]*/;
  23.     int min = pTm->tm_min/*[0, 59]*/;
  24.     int sec = pTm->tm_sec/*[0, 59]*/;

  25.     /**/
  26.     if(((int)(mon -= 2)) <= 0)
  27.     {
  28.         mon += 12;/* Puts Feb last since it has leap day */
  29.         year -= 1;
  30.     }

  31.     return ((((unsigned long)(year / 4 - year / 100 + year / 400 + 367 * mon / 12 + day) + year * 365 - 719499) * 24
  32.                     + hour /* now have hours */) * 60 + min /* now have minutes */)*60 + sec; /* finally seconds */
  33. }


  34. /**/
  35. int Calc_week_day( unsigned int year/*[2000, 2099]*/, unsigned int mon/*[1, 12]*/, unsigned int day/*[1, 31]*/)
  36. {
  37.     int week = 0;
  38.     unsigned int y = 0;
  39.     unsigned int c = 0;
  40.     unsigned int m = 0;
  41.     unsigned int d = 0;
  42.   
  43.     if((mon == 1) || (mon == 2))
  44.     {
  45.         c = ( year - 1 ) / 100;
  46.         y = ( year - 1 ) % 100;
  47.         m = mon + 12;
  48.         d = day;
  49.     }
  50.     else
  51.     {
  52.         c = year / 100;
  53.         y = year % 100;
  54.         m = mon;
  55.         d = day;
  56.     }
  57.       
  58.     week = y + y / 4 + c / 4 - 2 * c + 26 * ( m + 1 ) / 10 + d - 1; //蔡勒公式
  59.     week = week >= 0 ? ( week % 7 ) : ( week % 7 + 7 ); //week为负时取模
  60.   
  61.     return week;
  62. }


  63. /*
  64.     1:leap year
  65.     0:not leap year
  66. */
  67. int Is_leap_year(unsigned int year)
  68. {
  69.     if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
  70.     {
  71.         return 1;
  72.     }
  73.     else
  74.     {
  75.         return 0;
  76.     }
  77. }

  78. /*
  79.  The number of days in the month.
  80. */
  81. unsigned char Get_days_in_month(unsigned int year, unsigned int mon/*[1,12]*/)
  82. {
  83.     unsigned char day_table[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  84.     if((mon == 2) && (is_leap_year(year) == 1))
  85.     {
  86.         return 29;
  87.     }
  88.     else
  89.     {
  90.         return day_table[mon];
  91.     }
  92. }

  93. /*
  94.  * Convert seconds since 01-01-1970 00:00:00 to date.
  95.  */
  96. void Dt_time_to_tm(unsigned long time, date_time_t *pTm)
  97. {
  98.     unsigned int month = 0;
  99.     unsigned int year = 0;
  100.     int days = 0;
  101.     int tm_year = 0;
  102.     int tm_mon = 0;
  103.     int tm_mday = 0;
  104.     int tm_hour = 0;
  105.     int tm_min = 0;
  106.     int tm_sec = 0;
  107.     int newdays = 0;

  108.     /**/
  109. #define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400)

  110.     /**/
  111.     days = time / 86400;
  112.     time -= (unsigned int)days * 86400;
  113.     year = 1970 + days / 365;
  114.     days -= (year - 1970) * 365 + LEAPS_THRU_END_OF(year - 1) - LEAPS_THRU_END_OF(1970 - 1);
  115.     if (days < 0)
  116.     {
  117.         year -= 1;
  118.         days += 365 + is_leap_year(year);
  119.     }
  120.     tm_year = year - 1900;
  121.     for (month = 0; month < 11; month++)
  122.     {
  123.         newdays = days - rtc_month_days(month, year);
  124.         if (newdays < 0)
  125.         {
  126.             break;
  127.         }
  128.         days = newdays;
  129.     }
  130.     tm_mon = month;
  131.     tm_mday = days + 1;
  132.     tm_hour = time / 3600;
  133.     time -= tm_hour * 3600;
  134.     tm_min = time / 60;
  135.     tm_sec = time - tm_min * 60;

  136.     /**/
  137.     pTm->year = (unsigned char)(tm_year - 100);
  138.     pTm->mon = (unsigned char)(tm_mon + 1);
  139.     pTm->mday = (unsigned char)(tm_mday);
  140.     pTm->hour = (unsigned char)(tm_hour);
  141.     pTm->min = (unsigned char)(tm_min);
  142.     pTm->sec = (unsigned char)(tm_sec);

  143.     /**/
  144.     pTm->week = (unsigned char)Calc_week_day(pTm->year + 2000, pTm->mon, pTm->mday);    
  145. }


  146. /**/
  147. int Dt_get_day_by_week(unsigned char year/*[0, 99]*/, unsigned char month/*[1, 12]*/,
  148.                                 unsigned char week_index/*>= 1*/, unsigned char week_day/*[0, 6]*/)
  149. {
  150.     /**/
  151.     unsigned char week_day_ = 0;
  152.     unsigned char day = 0;

  153.     /**/
  154.     if(!((year <= 99) && ((month >= 1) && (month <= 12)) && (week_index >= 1) &&
  155.                                 (week_day <= 6)))
  156.     {
  157.         printf("Invalid parameter(%d, %d, %d, %d)\r\n", year, month, week_index, week_day);
  158.         return -1;
  159.     }
  160.     /*计算这个月的1号是星期几*/
  161.     week_day_ = Calc_week_day(year + 2000, month, 1);
  162.     /**/
  163.     if(week_day >= week_day_)
  164.     {
  165.         day = week_day - week_day_ + 1;
  166.     }
  167.     else
  168.     {
  169.         day = 8 - week_day_ + week_day;
  170.     }
  171.     /**/
  172.     day += (week_index - 1) * 7;

  173.     /**/
  174.     if(day > Get_days_in_month(year, month))
  175.     {
  176.         printf("Invalid result(%d, %d, %d, %d, %d)\r\n", year, month, week_index, week_day, day);
  177.         return -1;
  178.     }

  179.     /**/
  180.     return day;
  181. }

阅读(964) | 评论(0) | 转发(0) |
0

上一篇:写log日志文件

下一篇:mktime代码的实现

给主人留下些什么吧!~~