C 获取编译日期函数

#include <stdio.h>
#include
<stdlib.h>
#include
<malloc.h>
#include
<string.h>
typedef
struct tagXDate
{
int year;
int month;
int day;
}XDate;
bool GetCompileDate(XDate* date)
{
bool succeed=true;
char* complieDate=(char*)malloc(strlen(__DATE__)+1);
strcpy_s(complieDate,strlen(__DATE__)
+1,__DATE__);
char* context;
char* month=strtok_s(complieDate," ",&context);
char* day=strtok_s(0," ",&context);
char* year=strtok_s(0," ",&context);
date
->day=atoi(day);
if (date->day==0)
{
succeed
=false;
goto cleanup;
}
date
->year=atoi(year);
if (date->year==0)
{
succeed
=false;
goto cleanup;
}
const char months[][4]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
date
->month=0;
for (int i=0;i<12;i++)
{
if (strcmp(month,months[i])==0)
{
date
->month=i+1;
break;
}
}
if (date->month==0)
{
succeed
=false;
goto cleanup;
}
cleanup:
free(complieDate);
return succeed;
}
int main()
{
XDate date;
if (!GetCompileDate(&date))
{
return -1;
}
printf(
"%04d年%02d月%02d日/n",date.year,date.month,date.day);
return 0;
}

  

原文地址:https://www.cnblogs.com/oyjj/p/2132874.html