Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1184571
  • 博文数量: 573
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 66
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-28 16:21
文章分类

全部博文(573)

文章存档

2018年(3)

2016年(48)

2015年(522)

分类: LINUX

2015-12-02 15:14:02


点击(此处)折叠或打开

  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <sys/time.h>
  6. #include <sys/timeb.h>

  7. char * StrTrim(char * pstr);
  8. int    GetDateTime(char *    cDate, char *    cTime);

  9. int main(int argc, char ** argv)
  10. {
  11.     char    date[32];
  12.     char    time[32];
  13.     char    curtime[128];
  14.     char year[32];
  15.     char month[32];
  16.     char day[32];
  17.     char hour[32];
  18.     char min[32];
  19.     char sec[32];
  20.     
  21.     while(1)
  22.     {
  23.         memset(date,0,sizeof(date));
  24.         memset(time,0,sizeof(time));
  25.         
  26.         GetDateTime(date,time);
  27.         StrTrim(date);
  28.         StrTrim(time);
  29.         
  30.         /*printf("date=[%s], time=[%s]\n", date, time);*/
  31.         
  32.         memset(year, 0, sizeof(year));
  33.         memset(month, 0, sizeof(month));
  34.         memset(day, 0, sizeof(day));
  35.         memset(hour, 0, sizeof(hour));
  36.         memset(min, 0, sizeof(min));
  37.         memset(sec, 0, sizeof(sec));
  38.         strncpy(year, date, sizeof(char)*4);
  39.         strncpy(month, date+sizeof(char)*4, sizeof(char)*2);
  40.         strncpy(day, date+sizeof(char)*6, sizeof(char)*2);
  41.         strncpy(hour, time, sizeof(char)*2);
  42.         strncpy(min, time+sizeof(char)*2, sizeof(char)*2);
  43.         strncpy(sec, time+sizeof(char)*4, sizeof(char)*2);
  44.         
  45.         memset(curtime, 0, sizeof(curtime));
  46.         sprintf(curtime, "当前时间=[%s年%s月%s日 %s:%s:%s]", year, month, day, hour, min, sec);
  47.         printf("%s\r", curtime);
  48.         fflush(stdout);
  49.     }
  50.     
  51.     return 0;
  52. }

  53. /***********************************************************
  54. * 函 数 名:StrTrim()
  55. * 功能描述: 裁减字符串首尾空格
  56. * 输入参数:
  57. * cString    待裁减的字符串
  58. * 输出参数:
  59. * cString
  60. * 返 回:
  61. * 流程描述:
  62. * 说明:
  63. * 修改记录:
  64. * [修改人] [日期] - [描述]
  65. ***********************************************************/
  66. char * StrTrim(char * pstr)
  67. {
  68.     if(NULL == pstr)
  69.     {
  70.         return NULL;
  71.     }
  72.     char * phead = pstr;
  73.     char * ptail = pstr + strlen(pstr) - 1;
  74.     while((*phead == ' ')&&(phead < ptail)) phead++;
  75.     while((*ptail == ' ')&&(ptail > phead)) ptail--;
  76.     *(ptail + 1) = '\0';
  77.     memcpy(pstr, phead, sizeof(char)*(ptail - phead + 2));
  78.     return pstr;
  79. }


  80. /***********************************************************
  81. * 函 数 名:GetDateTime()
  82. * 功能描述: 分别获取系统日期和时间
  83. * 输入参数:cDate-日期接收缓冲区;cTime-时间接收缓冲区
  84. * 输出参数:cDate,cTime
  85. * 返 回:0 成功
  86. * 流程描述:
  87. * 说明: 返回日期格式:YYYYMMDD
  88. * 返回时间格式:HHmmssccc
  89. * 修改记录:
  90. * [修改人] [日期] - [描述]
  91. ***********************************************************/
  92. int    GetDateTime(char *    cDate, char *    cTime)
  93. {
  94.     struct tm *cur;
  95.     time_t timep;
  96.     struct timeb tbp;
  97.     unsigned short msec; /* Milliseconds */
  98.     
  99.     ftime(&tbp);
  100.     timep = tbp.time;
  101.     msec = tbp.millitm;
  102.     cur= localtime(&timep);
  103.     
  104.     sprintf(cDate,"%04d%02d%02d",
  105.         cur->tm_year+1900,cur->tm_mon+1,cur->tm_mday);
  106.     cDate[8]=0;
  107.     
  108.     sprintf(cTime,"%02d%02d%02d%03d",
  109.         cur->tm_hour,cur->tm_min,cur->tm_sec,msec);

  110.     cTime[9]=0;
  111.     return 0;
  112. }
获取前一天,后一天日期的函数

点击(此处)折叠或打开

  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <time.h>
  5. #include <sys/time.h>
  6. #include <sys/types.h>

  7. int DateAddSub(char * oldDate, int AddSubValue, char * newDate);
  8. int DateAddSub2(char * oldDate, int AddSubValue, char * newDate);

  9. int main(int argc, char ** argv)
  10. {    
  11.     if(argc < 3)
  12.     {
  13.         printf("参数个数不对,应为3个\n");
  14.         return -1;
  15.     }
  16.     
  17.     printf("*****************************************\n");
  18.     time_t sec = time(NULL);
  19.     char buf[128];
  20.     memset(buf, 0, sizeof(buf));
  21.     strcpy(buf, ctime(&sec));
  22.     printf("当前时间=[%s]\n", buf);
  23.     
  24.     printf("*****************************************\n");
  25.     time_t sec2 = time(NULL);
  26.     printf("当前时间=[%s]\n", asctime(localtime(&sec2)));
  27.     
  28.     char oldDate[9];
  29.     memset(oldDate, 0, sizeof(oldDate));
  30.     strncpy(oldDate, argv[1], sizeof(char)*8);
  31.     int value = atoi(argv[2]);
  32.     printf("*****************************************\n");
  33.     char newDate[9];
  34.     memset(newDate, 0, sizeof(newDate));
  35.     DateAddSub(oldDate, value, newDate);
  36.     printf("[oldDate] + [value] = [newDate]\n");
  37.     printf("[%s] + [%d] = [%s]\n", oldDate, value, newDate);
  38.     printf("*****************************************\n");
  39.     memset(newDate, 0, sizeof(newDate));
  40.     DateAddSub2(oldDate, value, newDate);
  41.     printf("oldDate + value = newDate\n");
  42.     printf("[%s] + [%d] = [%s]\n", oldDate, value, newDate);
  43.     printf("*****************************************\n");
  44.     
  45.     return 0;
  46. }


  47. /*************************************************************
  48.  * 函 数 名:DateAddSub2()
  49.  * 功能描述: 将xml文件解析成数据总线格式
  50.  * 输入参数:
  51.  *             oldDate - 需要更改的日期
  52.  *             AddSubValue - 日期加减的天数
  53.  * 输出参数:
  54.  *          newDate     - 改后的日期
  55.  * 返 回:0 - 成功 ;-1 - 失败
  56.  * 说 明:
  57.  ************************************************************/
  58. int DateAddSub2(char * oldDate, int AddSubValue, char * newDate)
  59. {    
  60.     char sOldYear[5];
  61.     memset(sOldYear, 0, sizeof(sOldYear));
  62.     strncpy(sOldYear, oldDate, sizeof(char)*4);
  63.     
  64.     char sOldMon[3];
  65.     memset(sOldMon, 0, sizeof(sOldMon));
  66.     strncpy(sOldMon, oldDate+4, sizeof(char)*2);
  67.     
  68.     char sOldDay[3];
  69.     memset(sOldDay, 0, sizeof(sOldDay));
  70.     strncpy(sOldDay, oldDate+6, sizeof(char)*2);
  71.     
  72.     time_t sec = time(NULL);
  73.     struct tm * cur = localtime(&sec);
  74.     cur->tm_year = atoi(sOldYear) - 1900;
  75.     cur->tm_mon = atoi(sOldMon) - 1;
  76.     cur->tm_mday = atoi(sOldDay);

  77.     sec = mktime(cur) + 60*60*24*AddSubValue;
  78.     cur = localtime(&sec);
  79.     sprintf(newDate, "%04d%02d%02d", cur->tm_year+1900, cur->tm_mon+1, cur->tm_mday);
  80.     newDate[8]=0;
  81.     
  82.     return 0;
  83. }



  84. /*************************************************************
  85.  * 函 数 名:DateAddSub()
  86.  * 功能描述: 将xml文件解析成数据总线格式
  87.  * 输入参数:
  88.  *             oldDate - 需要更改的日期
  89.  *             AddSubValue - 日期加减的天数
  90.  * 输出参数:
  91.  *          newDate     - 改后的日期
  92.  * 返 回:0 - 成功 ;-1 - 失败
  93.  * 说 明:
  94.  ************************************************************/
  95. int DateAddSub(char * oldDate, int AddSubValue, char * newDate)
  96. {    
  97.     char sCurYear[5];
  98.     int iCurYear = -1;
  99.     char sOldYear[5];
  100.     int iOldYear = -1;
  101.     int iDiffYear = -1;
  102.     memset(sOldYear, 0, sizeof(sOldYear));
  103.     strncpy(sOldYear, oldDate, sizeof(char)*4);
  104.     iOldYear = atoi(sOldYear);
  105.     
  106.     time_t cur_sec;
  107.     time(&cur_sec);
  108.     
  109.     struct tm * cur;
  110.     char cur_date[9];
  111.     int iRet = -1;
  112.     while(1)
  113.     {
  114.         cur = localtime(&cur_sec);
  115.         memset(cur_date, 0, sizeof(cur_date));
  116.         sprintf(cur_date, "%04d%02d%02d", cur->tm_year+1900, cur->tm_mon+1, cur->tm_mday);
  117.         cur_date[8]=0;
  118.         
  119.         memset(sCurYear, 0, sizeof(sCurYear));
  120.         strncpy(sCurYear, cur_date, sizeof(char)*4);
  121.         iCurYear = atoi(sCurYear);
  122.         iDiffYear = iCurYear-iOldYear;
  123.         if(iDiffYear >= 2)
  124.         {
  125.             cur_sec = cur_sec - 24*60*60*365;
  126.             continue;
  127.         }
  128.         else if(iDiffYear <= -2)
  129.         {
  130.             cur_sec = cur_sec + 24*60*60*365;
  131.             continue;
  132.         }
  133.         
  134.         iRet = strncmp(cur_date, oldDate, strlen(cur_date));
  135.         if(iRet == 0)
  136.         {
  137.             break;
  138.         }
  139.         else if(iRet > 0)
  140.         {
  141.             cur_sec = cur_sec - 24*60*60;
  142.         }
  143.         else if(iRet < 0)
  144.         {
  145.             cur_sec = cur_sec + 24*60*60;
  146.         }
  147.     }
  148.     
  149.     cur_sec = cur_sec + 60*60*24*AddSubValue;
  150.     cur = localtime(&cur_sec);
  151.     sprintf(newDate, "%04d%02d%02d", cur->tm_year+1900, cur->tm_mon+1, cur->tm_mday);
  152.     newDate[8]=0;
  153.     
  154.     return 0;
  155. }

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

上一篇:全线索二叉树

下一篇:popen用法

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