C语言中把 __DATE__ 转换成 yyyymmdd 格式的字符串

1、题目背景

C语言的预定义宏__DATE__:当前日期,一个以 “MMM DD YYYY” 格式表示的字符常量。

get_compiled_date_yyyy_mm_dd();
get_compiled_date_yyyymmdd();

2、PC环境,使用string.h头文件

编译环境:codeblock 17.04
运行在PC机中,使用了string.h头文件,利用里边的memcpy和memcmp函数

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

const char *get_compiled_date_yyyy_mm_dd(void)
{
    // __DATE__ result: Apr 23 2020
    // 2020-04-23\0 is 11 chars
    static char date_origin_format_buf[11] = {0};   // store __DATE__ chars
    int month = 0;               // store month number
    int i = 0;
    if (date_origin_format_buf[0] == 0) {          // can delete
        static const char *static_month_buf[] = {
        "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
        const char *cp_date = __DATE__;   // get origin format :month date year
        // store year to start of date_origin_format_buf
        memcpy(date_origin_format_buf, cp_date + 7, 4);
        // store date to start of date_origin_format_buf, but decade is ' ' when day little than 10
        // memcpy(date_origin_format_buf + 6, cp_date + 4, 2);
        for(i = 0; i < 12; i++) {
            // When static_month_buf[i] and cp_date store the same value in memory, month is i+1.
            if(memcmp(static_month_buf[i], cp_date, 3) == 0) {
                month = i+1;
                printf("%d\n", month);
                break;
            }
        }
        date_origin_format_buf[4] = '-';
        date_origin_format_buf[5] = month / 10 % 10 + '0';
        date_origin_format_buf[6] = month % 10 + '0';
        date_origin_format_buf[7] = '-';
        // judge day is little than 10
        if (cp_date[4] == ' ') {
            date_origin_format_buf[8] = '0';
        } else {
            date_origin_format_buf[8] = cp_date[4];
        }
        date_origin_format_buf[9] = cp_date[5];
    }
    return date_origin_format_buf;
}

const char *get_compiled_date_yyyymmdd(void)
{
    // __DATE__ result: Apr 23 2020, Apr232020 is 9 chars
    // 20200423\0 is 9 chars
    static char date_origin_format_buf[9] = {0};   // store __DATE__ chars
    int month = 0;               // store month number
    int i = 0;
    if (date_origin_format_buf[0] == 0) {          // can delete
        static const char *static_month_buf[] = {
        "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
        const char *cp_date = __DATE__;   // get origin format :month date year
        // store year to start of date_origin_format_buf
        memcpy(date_origin_format_buf, cp_date + 7, 4);
        // store date to start of date_origin_format_buf, but decade is ' ' when day little than 10
        // memcpy(date_origin_format_buf + 6, cp_date + 4, 2);
        for(i = 0; i < 12; i++) {
            // When static_month_buf[i] and cp_date store the same value in memory, month is i+1.
            if(memcmp(static_month_buf[i], cp_date, 3) == 0) {
                month = i+1;
                break;
            }
        }
        date_origin_format_buf[4] = month / 10 % 10 + '0';
        date_origin_format_buf[5] = month % 10 + '0';
        // judge day is little than 10
        if (cp_date[4] == ' ') {
            date_origin_format_buf[6] = '0';
        } else {
            date_origin_format_buf[6] = cp_date[4];
        }
        date_origin_format_buf[7] = cp_date[5];
    }
    return date_origin_format_buf;
}

int main()
{
    const char *date = get_compiled_date_yyyymmdd();

    // print __DATE__ origin format
    printf(__DATE__);
    printf("\n");

    printf("%s\n", date);

    // Print the converted date in two ways
    printf(date);
    printf("\n");

    printf(get_compiled_date_yyyymmdd());
    printf("\n");

    printf(get_compiled_date_yyyy_mm_dd());
    printf("\n");

    printf("Hello world!\n");
    return 0;
}

3、运行在单片机中,不使用string.h头文件

#include <stdio.h>
#include <stdlib.h>

const char *get_compiled_date_yyyy_mm_dd(void)
{
    // __DATE__ result: Apr 23 2020
    // 2020-04-23\0 is 11 chars
    static char date_origin_format_buf[11] = {0};   // store __DATE__ chars
    int month = 0;               // store month number
    int i = 0;
    if (date_origin_format_buf[0] == 0) {          // can delete
        static const char *static_month_buf[] = {
        "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
        const char *cp_date = __DATE__;   // get origin format :month date year
        // store year to start of date_origin_format_buf
        // memcpy(date_origin_format_buf, cp_date + 7, 4);
        for (i = 0; i < 4; i++) {
            date_origin_format_buf[i] = *(cp_date + 7 + i);
        }
        // store date to start of date_origin_format_buf, but decade is ' ' when day little than 10
        // memcpy(date_origin_format_buf + 6, cp_date + 4, 2);
//        for(i = 0; i < 12; i++) {
//            // When static_month_buf[i] and cp_date store the same value in memory, month is i+1.
//            if(memcmp(static_month_buf[i], cp_date, 3) == 0) {
//                month = i+1;
//                printf("%d\n", month);
//                break;
//            }
//        }
        for(i = 0; i < 12; i++) { //
            // When _month[i] and cp_date store the same value in memory, month is i+1.
                if((static_month_buf[i][0] == (cp_date[0])) &&
                   (static_month_buf[i][1] == (cp_date[1])) &&
                   (static_month_buf[i][2] == (cp_date[2]))) {
                    month = i+1;
                    break;
                }
        }
        date_origin_format_buf[4] = '-';
        date_origin_format_buf[5] = month / 10 % 10 + '0';
        date_origin_format_buf[6] = month % 10 + '0';
        date_origin_format_buf[7] = '-';
        // judge day is little than 10
        if (cp_date[4] == ' ') {
            date_origin_format_buf[8] = '0';
        } else {
            date_origin_format_buf[8] = cp_date[4];
        }
        date_origin_format_buf[9] = cp_date[5];
    }
    return date_origin_format_buf;
}

const char *get_compiled_date_yyyymmdd(void)
{
    // __DATE__ result: Apr 23 2020, Apr232020 is 9 chars
    // 20200423\0 is 9 chars
    static char date_origin_format_buf[9] = {0};   // store __DATE__ chars
    int month = 0;               // store month number
    int i = 0;
    if (date_origin_format_buf[0] == 0) {          // can delete
        static const char *static_month_buf[] = {
        "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
        const char *cp_date = __DATE__;   // get origin format :month date year
        // store year to start of date_origin_format_buf
        // memcpy(date_origin_format_buf, cp_date + 7, 4);
        for (i = 0; i < 4; i++) {
            date_origin_format_buf[i] = *(cp_date + 7 + i);
        }
        // store date to start of date_origin_format_buf, but decade is ' ' when day little than 10
        // memcpy(date_origin_format_buf + 6, cp_date + 4, 2);

//        for(i = 0; i < 12; i++) {
//            // When static_month_buf[i] and cp_date store the same value in memory, month is i+1.
//            if(memcmp(static_month_buf[i], cp_date, 3) == 0) {
//                month = i+1;
//                break;
//            }
//        }
        for(i = 0; i < 12; i++) { //
            // When _month[i] and cp_date store the same value in memory, month is i+1.
                if((static_month_buf[i][0] == (cp_date[0])) &&
                   (static_month_buf[i][1] == (cp_date[1])) &&
                   (static_month_buf[i][2] == (cp_date[2]))) {
                    month = i+1;
                    break;
                }
        }
        date_origin_format_buf[4] = month / 10 % 10 + '0';
        date_origin_format_buf[5] = month % 10 + '0';
        // judge day is little than 10
        if (cp_date[4] == ' ') {
            date_origin_format_buf[6] = '0';
        } else {
            date_origin_format_buf[6] = cp_date[4];
        }
        date_origin_format_buf[7] = cp_date[5];
    }
    return date_origin_format_buf;
}

int main()
{
    const char *date = get_compiled_date_yyyymmdd();

    // print __DATE__ origin format
    printf(__DATE__);
    printf("\n");

    printf("%s\n", date);

    // Print the converted date in two ways
    printf(date);
    printf("\n");

    printf(get_compiled_date_yyyymmdd());
    printf("\n");

    printf(get_compiled_date_yyyy_mm_dd());
    printf("\n");

    printf("Hello world!\n");
    return 0;
}
  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

点灯小能手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值