c程序获取编译时间

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

typedef struct {
  unsigned int Year;
  unsigned char Month;
  unsigned char Date;
  unsigned char Hours;
  unsigned char Minutes;
  unsigned char Seconds;
} buildDateTime_t;

typedef enum { false = 0, true } bool;

bool GetCompileDateTime(buildDateTime_t *times) {
  const char *pMonth[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
                          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  const char Date[12] = __DATE__;  //  __DATE__是当前编译的文件的编译日期 格式是Mmm:dd:yyyy 是字符串
  const char Time[9] = __TIME__; // __TIME__是当前编译的文件的编译时间 格式是hh:mm:ss 是字符串 
  unsigned char i;
  for (i = 0; i < 12; i++) {
    if (memcmp(Date, pMonth[i], 3) == 0) {
      times->Month = i + 1, i = 12;
    }
  }
  times->Year = (unsigned int)atoi(Date + 7);  // 完整年份2003 返回 2003
//times->Year = (unsigned int)atoi(Date + 7);  // 两位年份 比如 2003 返回03
  times->Date = (unsigned char)atoi(Date + 4);

  times->Hours = (unsigned char)atoi(Time + 0);
  times->Minutes = (unsigned char)atoi(Time + 4);
  times->Seconds = (unsigned char)atoi(Time + 6);

  return true;
}

int main() {
  buildDateTime_t MyBuildDateTime;

  GetCompileDateTime(&MyBuildDateTime);

  printf("year:%d\n", MyBuildDateTime.Year);
  printf("Month:%d\n", MyBuildDateTime.Month);
  printf("Date:%d\n", MyBuildDateTime.Date);
  printf("Hours:%d\n", MyBuildDateTime.Hours);
  printf("Minutes:%d\n", MyBuildDateTime.Minutes);
  printf("Seconds:%d\n", MyBuildDateTime.Seconds);

  getchar();
  return 0;
}

运行结果:

参考文章:

https://blog.csdn.net/bandaoyu/article/details/114704021?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-1&spm=1001.2101.3001.4242

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值