Get compilation date and system time in C language

Publisher:翅膀小鹰Latest update time:2015-09-08 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
int GetCompileDateTime(char *szDateTime)
       
        const int   MONTH_PER_YEAR=12;
        const char szEnglishMonth[MONTH_PER_YEAR][4]={ "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
        char szTmpDate[40]={0};
        char szTmpTime[20]={0};
        char szMonth[4]={0};
        int iYear,iMonth,iDay,iHour,iMin,iSec;//,,

        //Get compile date and time
        sprintf(szTmpDate,"%s",__DATE__); //"Sep 18 2010"
        sprintf(szTmpTime,"%s",__TIME__);     //"10:59:19"
        
        sscanf(szTmpDate,"%s %d %d",szMonth,&iDay,&iYear);
        sscanf(szTmpTime,"%d:%d:%d",&iHour,&iMin,&iSec);
                
        for(int i=0;MONTH_PER_YEAR;i++)
        {
                if(strncmp(szMonth,szEnglishMonth[i],3)==0)
                {
                        iMonth=i+1;
                        break;
                }
        }

        printf("% d,%d,%d,%d,%d,%d ",iYear,iMonth,iDay,iHour,iMin,iSec);
        sprintf(szDateTime,"dddddd",iYear,iMonth,iDay,iHour,iMin,iSec);
        return 0;
}
 
 
 
typedef  struct
{
    unsigned  int  Year;
    unsigned  int  Month;
    unsigned  int  Date;
    unsigned  int  Hours;
    unsigned  int  Minutes;
    unsigned  int  Seconds;
}BuildDateTime;

const  unsigned  char  MonthStr[12][4]  {"Jan",  "Feb",  "Mar",  "Apr",  "May",  "Jun",  "Jul",  "Aug",  "Sep",  "Oct",  "Nov","Dec"};

void  GetBuildDateTime(BuildDateTime  *p_BuildDateTime)
{
    unsigned  char  temp_str[4]  {0,  0,  0,  0},  0;

    sscanf(__DATE__,  "%s - M",  temp_str,  &(p_BuildDateTime->Date),  &(p_BuildDateTime->Year));
    sscanf(__TIME__,  "-:-:-",  &(p_BuildDateTime->Hours),  &(p_BuildDateTime->Minutes),  &(p_BuildDateTime->Seconds));
    for  (i  0;  12;  i++)
    {
        if  (temp_str[0]  ==  MonthStr[i][0]  &&  temp_str[1]  ==  MonthStr[i][1]  &&  temp_str[2]  ==  MonthStr[i][2])
        {
            p_BuildDateTime->Month  1;
            break;
        }
    }
}
 
Usage examples:
 
BuildDateTime  MyBuildDateTime;

GetBuildDateTime(&MyBuildDateTime);

RTC_YEAR  MyBuildDateTime.Year;
RTC_MONTH  MyBuildDateTime.Month;
RTC_DATE  MyBuildDateTime.Date;
RTC_HOURS  MyBuildDateTime.Hours;
RTC_MINUTES  MyBuildDateTime.Minutes;
RTC_SECONDS  MyBuildDateTime.Seconds;
 
 

__LINE__ The line number of the current statement, as a decimal integer.
__FILE__ The name of the current source file, as a string constant.
__DATE__ The date the program was compiled, as a string in the format "Mmm dd yyyy".
__TIME__ The time the program was compiled, as a string in the format "hh:mm:ss", as returned by asctime.

__STDC__ This macro is 1 if the current compiler complies with the ISO standard.
__STDC_VERSION__ This macro is defined as 199409L if the current compiler complies with C89, and 199901L if it complies with C99. 
   
__STDC_HOSTED__ This macro is defined as 1 if the current system is "hosted". A host system means that the current system has a complete standard C library.


Reference address:Get compilation date and system time in C language

Previous article:Application of SPI
Next article:Problems encountered in keil C compilation

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号