打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
c语言之date_format与strptime在windows平台上的实现
http://blog.csdn.net/earbao/article/details/51786724

  1. #include <time.h>  
  2. #include <string.h>  
  3. #include <stdlib.h>  
  4. #include <stdio.h>  
  5. #include <ctype.h>  
  6.   
  7. #define TM_YEAR_BASE 1900  
  8.   
  9. /* 
  10.  * We do not implement alternate representations. However, we always 
  11.  * check whether a given modifier is allowed for a certain conversion. 
  12.  */  
  13. #define ALT_E     0x01  
  14. #define ALT_O     0x02  
  15. #define LEGAL_ALT(x)    { if (alt_format & ~(x)) return (0); }  
  16.   
  17.   
  18. static  int conv_num(const char **, int *, int, int);  
  19.   
  20. static const char *day[7] = {  
  21.   "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",  
  22.   "Friday", "Saturday"  
  23. };  
  24. static const char *abday[7] = {  
  25.   "Sun","Mon","Tue","Wed","Thu","Fri","Sat"  
  26. };  
  27. static const char *mon[12] = {  
  28.   "January", "February", "March", "April", "May", "June", "July",  
  29.   "August", "September", "October", "November", "December"  
  30. };  
  31. static const char *abmon[12] = {  
  32.   "Jan", "Feb", "Mar", "Apr", "May", "Jun",  
  33.   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"  
  34. };  
  35. static const char *am_pm[2] = {  
  36.   "AM", "PM"  
  37. };  
  38.   
  39.   
  40. //window上自己实现strptime函数,linux已经提供strptime  
  41. //strptime函数windows平台上实现  
  42. char *  
  43. strptime(const char *buf, const char *fmt, struct tm *tm)  
  44. {  
  45.   char c;  
  46.   const char *bp;  
  47.   size_t len = 0;  
  48.   int alt_format, i, split_year = 0;  
  49.   
  50.   bp = buf;  
  51.   
  52.   while ((c = *fmt) != '\0') {  
  53.     /* Clear `alternate' modifier prior to new conversion. */  
  54.     alt_format = 0;  
  55.   
  56.     /* Eat up white-space. */  
  57.     if (isspace(c)) {  
  58.       while (isspace(*bp))  
  59.         bp++;  
  60.   
  61.       fmt++;  
  62.       continue;  
  63.     }  
  64.           
  65.     if ((c = *fmt++) != '%')  
  66.       goto literal;  
  67.   
  68.   
  69. again:    switch (c = *fmt++) {  
  70.     case '%': /* "%%" is converted to "%". */  
  71. literal:  
  72.       if (c != *bp++)  
  73.         return (0);  
  74.       break;  
  75.   
  76.     /* 
  77.      * "Alternative" modifiers. Just set the appropriate flag 
  78.      * and start over again. 
  79.      */  
  80.     case 'E': /* "%E?" alternative conversion modifier. */  
  81.       LEGAL_ALT(0);  
  82.       alt_format |= ALT_E;  
  83.       goto again;  
  84.   
  85.     case 'O': /* "%O?" alternative conversion modifier. */  
  86.       LEGAL_ALT(0);  
  87.       alt_format |= ALT_O;  
  88.       goto again;  
  89.         
  90.     /* 
  91.      * "Complex" conversion rules, implemented through recursion. 
  92.      */  
  93.     case 'c': /* Date and time, using the locale's format. */  
  94.       LEGAL_ALT(ALT_E);  
  95.       if (!(bp = strptime(bp, "%x %X", tm)))  
  96.         return (0);  
  97.       break;  
  98.   
  99.     case 'D': /* The date as "%m/%d/%y". */  
  100.       LEGAL_ALT(0);  
  101.       if (!(bp = strptime(bp, "%m/%d/%y", tm)))  
  102.         return (0);  
  103.       break;  
  104.   
  105.     case 'R': /* The time as "%H:%M". */  
  106.       LEGAL_ALT(0);  
  107.       if (!(bp = strptime(bp, "%H:%M", tm)))  
  108.         return (0);  
  109.       break;  
  110.   
  111.     case 'r': /* The time in 12-hour clock representation. */  
  112.       LEGAL_ALT(0);  
  113.       if (!(bp = strptime(bp, "%I:%M:%S %p", tm)))  
  114.         return (0);  
  115.       break;  
  116.   
  117.     case 'T': /* The time as "%H:%M:%S". */  
  118.       LEGAL_ALT(0);  
  119.       if (!(bp = strptime(bp, "%H:%M:%S", tm)))  
  120.         return (0);  
  121.       break;  
  122.   
  123.     case 'X': /* The time, using the locale's format. */  
  124.       LEGAL_ALT(ALT_E);  
  125.       if (!(bp = strptime(bp, "%H:%M:%S", tm)))  
  126.         return (0);  
  127.       break;  
  128.   
  129.     case 'x': /* The date, using the locale's format. */  
  130.       LEGAL_ALT(ALT_E);  
  131.       if (!(bp = strptime(bp, "%m/%d/%y", tm)))  
  132.         return (0);  
  133.       break;  
  134.   
  135.     /* 
  136.      * "Elementary" conversion rules. 
  137.      */  
  138.     case 'A': /* The day of week, using the locale's form. */  
  139.     case 'a':  
  140.       LEGAL_ALT(0);  
  141.       for (i = 0; i < 7; i++) {  
  142.         /* Full name. */  
  143.         len = strlen(day[i]);  
  144.         if (strncmp(day[i], bp, len) == 0)  
  145.           break;  
  146.   
  147.         /* Abbreviated name. */  
  148.         len = strlen(abday[i]);  
  149.         if (strncmp(abday[i], bp, len) == 0)  
  150.           break;  
  151.       }  
  152.   
  153.       /* Nothing matched. */  
  154.       if (i == 7)  
  155.         return (0);  
  156.   
  157.       tm->tm_wday = i;  
  158.       bp += len;  
  159.       break;  
  160.   
  161.     case 'B': /* The month, using the locale's form. */  
  162.     case 'b':  
  163.     case 'h':  
  164.       LEGAL_ALT(0);  
  165.       for (i = 0; i < 12; i++) {  
  166.         /* Full name. */  
  167.         len = strlen(mon[i]);  
  168.         if (strncmp(mon[i], bp, len) == 0)  
  169.           break;  
  170.   
  171.         /* Abbreviated name. */  
  172.         len = strlen(abmon[i]);  
  173.         if (strncmp(abmon[i], bp, len) == 0)  
  174.           break;  
  175.       }  
  176.   
  177.       /* Nothing matched. */  
  178.       if (i == 12)  
  179.         return (0);  
  180.   
  181.       tm->tm_mon = i;  
  182.       bp += len;  
  183.       break;  
  184.   
  185.     case 'C': /* The century number. */  
  186.       LEGAL_ALT(ALT_E);  
  187.       if (!(conv_num(&bp, &i, 0, 99)))  
  188.         return (0);  
  189.   
  190.       if (split_year) {  
  191.         tm->tm_year = (tm->tm_year % 100) + (i * 100);  
  192.       } else {  
  193.         tm->tm_year = i * 100;  
  194.         split_year = 1;  
  195.       }  
  196.       break;  
  197.   
  198.     case 'd': /* The day of month. */  
  199.     case 'e':  
  200.       LEGAL_ALT(ALT_O);  
  201.       if (!(conv_num(&bp, &tm->tm_mday, 1, 31)))  
  202.         return (0);  
  203.       break;  
  204.   
  205.     case 'k': /* The hour (24-hour clock representation). */  
  206.       LEGAL_ALT(0);  
  207.       /* FALLTHROUGH */  
  208.     case 'H':  
  209.       LEGAL_ALT(ALT_O);  
  210.       if (!(conv_num(&bp, &tm->tm_hour, 0, 23)))  
  211.         return (0);  
  212.       break;  
  213.   
  214.     case 'l': /* The hour (12-hour clock representation). */  
  215.       LEGAL_ALT(0);  
  216.       /* FALLTHROUGH */  
  217.     case 'I':  
  218.       LEGAL_ALT(ALT_O);  
  219.       if (!(conv_num(&bp, &tm->tm_hour, 1, 12)))  
  220.         return (0);  
  221.       if (tm->tm_hour == 12)  
  222.         tm->tm_hour = 0;  
  223.       break;  
  224.   
  225.     case 'j': /* The day of year. */  
  226.       LEGAL_ALT(0);  
  227.       if (!(conv_num(&bp, &i, 1, 366)))  
  228.         return (0);  
  229.       tm->tm_yday = i - 1;  
  230.       break;  
  231.   
  232.     case 'M': /* The minute. */  
  233.       LEGAL_ALT(ALT_O);  
  234.       if (!(conv_num(&bp, &tm->tm_min, 0, 59)))  
  235.         return (0);  
  236.       break;  
  237.   
  238.     case 'm': /* The month. */  
  239.       LEGAL_ALT(ALT_O);  
  240.       if (!(conv_num(&bp, &i, 1, 12)))  
  241.         return (0);  
  242.       tm->tm_mon = i - 1;  
  243.       break;  
  244.   
  245.     case 'p': /* The locale's equivalent of AM/PM. */  
  246.       LEGAL_ALT(0);  
  247.       /* AM? */  
  248.       if (strcmp(am_pm[0], bp) == 0) {  
  249.         if (tm->tm_hour > 11)  
  250.           return (0);  
  251.   
  252.         bp += strlen(am_pm[0]);  
  253.         break;  
  254.       }  
  255.       /* PM? */  
  256.       else if (strcmp(am_pm[1], bp) == 0) {  
  257.         if (tm->tm_hour > 11)  
  258.           return (0);  
  259.   
  260.         tm->tm_hour += 12;  
  261.         bp += strlen(am_pm[1]);  
  262.         break;  
  263.       }  
  264.   
  265.       /* Nothing matched. */  
  266.       return (0);  
  267.   
  268.     case 'S': /* The seconds. */  
  269.       LEGAL_ALT(ALT_O);  
  270.       if (!(conv_num(&bp, &tm->tm_sec, 0, 61)))  
  271.         return (0);  
  272.       break;  
  273.   
  274.     case 'U': /* The week of year, beginning on sunday. */  
  275.     case 'W': /* The week of year, beginning on monday. */  
  276.       LEGAL_ALT(ALT_O);  
  277.       /* 
  278.        * XXX This is bogus, as we can not assume any valid 
  279.        * information present in the tm structure at this 
  280.        * point to calculate a real value, so just check the 
  281.        * range for now. 
  282.        */  
  283.        if (!(conv_num(&bp, &i, 0, 53)))  
  284.         return (0);  
  285.        break;  
  286.   
  287.     case 'w': /* The day of week, beginning on sunday. */  
  288.       LEGAL_ALT(ALT_O);  
  289.       if (!(conv_num(&bp, &tm->tm_wday, 0, 6)))  
  290.         return (0);  
  291.       break;  
  292.   
  293.     case 'Y': /* The year. */  
  294.       LEGAL_ALT(ALT_E);  
  295.       if (!(conv_num(&bp, &i, 0, 9999)))  
  296.         return (0);  
  297.   
  298.       tm->tm_year = i - TM_YEAR_BASE;  
  299.       break;  
  300.   
  301.     case 'y': /* The year within 100 years of the epoch. */  
  302.       LEGAL_ALT(ALT_E | ALT_O);  
  303.       if (!(conv_num(&bp, &i, 0, 99)))  
  304.         return (0);  
  305.   
  306.       if (split_year) {  
  307.         tm->tm_year = ((tm->tm_year / 100) * 100) + i;  
  308.         break;  
  309.       }  
  310.       split_year = 1;  
  311.       if (i <= 68)  
  312.         tm->tm_year = i + 2000 - TM_YEAR_BASE;  
  313.       else  
  314.         tm->tm_year = i + 1900 - TM_YEAR_BASE;  
  315.       break;  
  316.   
  317.     /* 
  318.      * Miscellaneous conversions. 
  319.      */  
  320.     case 'n': /* Any kind of white-space. */  
  321.     case 't':  
  322.       LEGAL_ALT(0);  
  323.       while (isspace(*bp))  
  324.         bp++;  
  325.       break;  
  326.   
  327.   
  328.     default:  /* Unknown/unsupported conversion. */  
  329.       return (0);  
  330.     }  
  331.   
  332.   
  333.   }  
  334.   
  335.   /* LINTED functional specification */  
  336.   return ((char *)bp);  
  337. }  
  338.   
  339.   
  340. static int conv_num(const char **buf, int *dest, int llim, int ulim)  
  341. {  
  342.   int result = 0;  
  343.   
  344.   /* The limit also determines the number of valid digits. */  
  345.   int rulim = ulim;  
  346.   
  347.   if (**buf < '0' || **buf > '9')  
  348.     return (0);  
  349.   
  350.   do {  
  351.     result *= 10;  
  352.     result += *(*buf)++ - '0';  
  353.     rulim /= 10;  
  354.   } while ((result * 10 <= ulim) && rulim && **buf >= '0' && **buf <= '9');  
  355.   
  356.   if (result < llim || result > ulim)  
  357.     return (0);  
  358.   
  359.   *dest = result;  
  360.   return (1);  
  361. }  
  362.   
  363.   
  364. #define ARDRONE_DATE_MAXSIZE    32  
  365. #define ARDRONE_FILE_DATE_FORMAT  "%Y%m%d_%H%M%S"  
  366. #define ARDRONE_DEFAULT_DATE        "19700101_000000"  
  367.   
  368. void yunshouhu_time2date(time_t time, const char *format, char *date)  
  369. {  
  370.   if(date)  
  371.     {  
  372.       struct tm *tm = localtime(&time);  
  373.       strcpy(date, ARDRONE_DEFAULT_DATE);  
  374.       if(tm != NULL)  
  375.         strftime(date, ARDRONE_DATE_MAXSIZE, format ? format : ARDRONE_FILE_DATE_FORMAT, tm);  
  376.     }  
  377. }  
  378.   
  379. void yunshouhu_date2time(char *date, const char *format, time_t *time)  
  380. {  
  381.   struct tm tm;  
  382.   
  383.   if(date != NULL)  
  384.     {  
  385.       *time = 0;  
  386.       if(strptime(date, (NULL != format) ? format : ARDRONE_FILE_DATE_FORMAT, &tm) != NULL)  
  387.         *time = mktime(&tm);  
  388.     }  
  389. }  
  390.   
  391. //https://github.com/phdesign/pebble-phd/blob/f72313800357fd509def6abdde379067438fb3c1/src/utils.c  
  392. char* format_date_time(time_t datetime,char *format) {  
  393.   struct tm *temp_time = localtime(&datetime);  
  394.   static char str_time[512]={0};  
  395.   strftime(str_time, sizeof(str_time), format, temp_time);  
  396.   return str_time;  
  397. }  
  398.   
  399. int main(int argc, char const *argv[])  
  400. {  
  401.   time_t nowTime = time(NULL);  
  402.   char line[1024]={0};  
  403.   char* format="%Y-%m-%d %H:%M:%S";  
  404.   yunshouhu_time2date(nowTime,format,line);  
  405.   printf("%s\n", line);  
  406.   
  407.   time_t secondTime;  
  408.   strcpy(line,"2018-06-29 23:20:30");  
  409.   yunshouhu_date2time(line,format,&secondTime);  
  410.   
  411.   printf("ctime is %s\n",ctime(&secondTime));//得到日历时间  
  412.   
  413.   yunshouhu_time2date(nowTime,NULL,line);  
  414.   printf("%s\n", line);  
  415.   
  416.   char* data=format_date_time(time(NULL),"%Y-%m-%d %H:%M:%S");  
  417.   printf("%s\n", data);  
  418.   
  419.   char *format2="%Y year %m month %d date %H hour %M minute %S seconds ";//还不支持中文 要使用unicode编码的中文  
  420.   data=format_date_time(time(NULL),format2);  
  421.   printf("%s\n", data);  
  422.   
  423.   format2="%Y\u5e74%m\u6708%d\u65e5 %H\u65f6%M\u5206%S\u79d2";//还不支持中文 要使用unicode编码的中文  
  424.   data=format_date_time(time(NULL),format2);  
  425.   printf("%s\n", data);  
  426.   
  427.   
  428.   return 0;  
  429. }  

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
C++博客 - christanxw的专栏 - 变态之MEMCPY
Windows 获取当前系统时间函数总结
[分享] LuaJIT的FFI调用WINDOWS API功能示例
HT66F50 C程序框架及注意事项
c语言中时间的获取
时间time_t和string(char*)格式互转
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服