用C语言(apue)实现 把时间戳转换为国标格式的字符串(2017-07-17 22:36:12)的函数

时间:2023-03-10 01:06:02
用C语言(apue)实现 把时间戳转换为国标格式的字符串(2017-07-17 22:36:12)的函数
/*******************************************************************************/
/**
*** 函 数 名:  char *Time2String(const time_t timep, char *strDest)
*** 功能描述:  将时间转换成国标格式的字符串
*** 全局变量:
*** 输  入:
       const time_t timep :获得的时间
       char *strDest:存储转换后的字符串
*** 输  出:     转换后的字符串地址
******************************************************************************/
char *Time2String(const time_t timep, char *strDest)
{
    char *address = strDest;
   // assert(strDest != NULL);
    struct tm* tm;
    time_t tempTime = timep;
    char *bars = (char *)"-",*colon = (char *)":",
         *space = (char *)" ";  

    tm = localtime(&tempTime);
    tm->tm_year += ;
    tm->tm_mon += ;
    sprintf(strDest, "%04d%s%02d%s%02d%s%02d%s%02d%s%02d", tm->tm_year, bars, tm->tm_mon,
            bars, tm->tm_mday, space, tm->tm_hour, colon, tm->tm_min, colon, tm->tm_sec);  

    return address;
}