用C语言格式化日期

用C语言格式化日期,c,date,C,Date,我有一些GPS字符串日期,如下所示: "181215" //11th Dec 2015 "211115" //21st Nov 2015 我可以使用这种格式,但当日期或月份为一位数时,它看起来像: "11215" //1st Dec 2015 "8515" //8th May 2015 用C语言将8515转换为080515的最佳方法是什么 我基本上需要把字符串转换成3个整数,我是这样做的: //date: 171115 char str[8]; sprintf(str, "%d",

我有一些GPS字符串日期,如下所示:

 "181215" //11th Dec 2015
 "211115" //21st Nov 2015
我可以使用这种格式,但当日期或月份为一位数时,它看起来像:

 "11215" //1st Dec 2015
 "8515" //8th May 2015
用C语言将
8515
转换为
080515
的最佳方法是什么

我基本上需要把字符串转换成3个整数,我是这样做的:

//date: 171115
char str[8];
sprintf(str, "%d", date);
char component[3];

sprintf(component,"%c%c",str[0],str[1]);
int day = strtof(component, NULL);

sprintf(component,"%c%c",str[2],str[3]);
int month = strtof(component, NULL);

sprintf(component,"%c%c",str[4],str[5]);
int year = strtof(component, NULL) + 2000;
char str[8];
sprintf(str, "%d", date);
char corr_str[8];

switch (strlen(str)) {
  case 6: // easiest way:
    strcpy(corr_str, str);
    break:
  case 4:
    corr_str[0] = '0';
    corr_str[1] = str[0];
    corr_str[2] = '0';
    // corr_str[3] = str[1];
    // corr_str[4] = str[2];
    // corr_str[5] = str[3];
    strcpy(corr_str + 3, str + 1);
    break;
  case 5: // Well, what now? Is the day shortened or the month?
    // Here we assume the day is shortened, so 11215 is 2015-12-01.
    corr_str[0] = '0';
    strcpy(corr_str + 1, str);
    break;
    // Here we assume the month is shortened, so 11215 is 2015-02-11.
    strcpy(corr_str, str);
    corr_str[2] = '0';
    strcpy(corr_str + 3, str + 2);
    break;
试着这样做:

//date: 171115
char str[8];
sprintf(str, "%d", date);
char component[3];

sprintf(component,"%c%c",str[0],str[1]);
int day = strtof(component, NULL);

sprintf(component,"%c%c",str[2],str[3]);
int month = strtof(component, NULL);

sprintf(component,"%c%c",str[4],str[5]);
int year = strtof(component, NULL) + 2000;
char str[8];
sprintf(str, "%d", date);
char corr_str[8];

switch (strlen(str)) {
  case 6: // easiest way:
    strcpy(corr_str, str);
    break:
  case 4:
    corr_str[0] = '0';
    corr_str[1] = str[0];
    corr_str[2] = '0';
    // corr_str[3] = str[1];
    // corr_str[4] = str[2];
    // corr_str[5] = str[3];
    strcpy(corr_str + 3, str + 1);
    break;
  case 5: // Well, what now? Is the day shortened or the month?
    // Here we assume the day is shortened, so 11215 is 2015-12-01.
    corr_str[0] = '0';
    strcpy(corr_str + 1, str);
    break;
    // Here we assume the month is shortened, so 11215 is 2015-02-11.
    strcpy(corr_str, str);
    corr_str[2] = '0';
    strcpy(corr_str + 3, str + 2);
    break;

然后像以前一样继续操作。

这里有一个解决方案,为不明确的日期提供可识别的错误输出:

#include <stdio.h>
#include <string.h>

/* N.B. returns a pointer to static storage - not for production use */
/* Returns "??????" on failure */
const char *reformat(const char *s)
{
    static char buf[9];

    int d, m, y;                /* day, month, year */
    switch (strlen(s)) {
    case 6:
        return s;
    case 5:
        if (s[1] == '1' && s[2] <= '2') {
            /* second digit may be part of day or month */
            if (s[0] <= '2' || s[0] <= '3' && s[2] == '1')
                return "??????";              /* ambiguous string */
            /* 1-digit day, 2-digit month */
            sscanf(s, "%1d%2d%2d", &d, &m, &y);
        break;
        }
        /* 2-digit day; 1-digit month */
        sscanf(s, "%2d%1d%2d", &d, &m, &y);
        break;
    case 4:
        sscanf(s, "%1d%1d%2d", &d, &m, &y);
        sprintf(buf, "%02d%02d%02d", d, m, y);
        return buf;
    default:
        return "??????";              /* invalid input length */
    };
    sprintf(buf, "%02d%02d%02d", d, m, y);
    return buf;
}
在我的测试用例中,它生成:

   111 -> ??????
  8515 -> 080515
181215 -> 181215
  1111 -> 010111
 11215 -> ??????
 31211 -> 031211
 29212 -> 290212
 21200 -> ??????
 21300 -> 210300
11111111 -> ??????

“1111”对你意味着什么?你如何判断11215应该是2015年2月11日还是2015年12月1日?你如何区分11.1和1.11?他们都是11115,如果没有额外的信息知道
11215
是2月11日还是12月1日,这是不可能的。例如,如果月份始终是当前月份,那么就有事情要做。否则,你永远不会知道,在上面提到的11215年是12月还是2月。当然,如果最初在日和月之间有一个空格。。。那更容易!如果警告是一个5位数的字符串,并且它与一个不明确的日期
[12]1[12]..
311..
(在regexp语法中)匹配,您也可以选择记录警告。@TobySpeight Right。我想它是在某个地方定义的,但OP不想告诉我们。无论哪种方式都是合适的。。。