Linux C/C++时间字符串与time_t之间的转换方法(转)

1.指定time_t类型的时间,格式化为YYYYMMDDHH24MISS型的字符串
void FormatTime(time_t time1, char *szTime)
{
       struct tm tm1;
#ifdef WIN32
       tm1 =*localtime(&time1);
#else
      localtime_r(&time1,&tm1 );
#endif
       sprintf( szTime,"%4.4d%2.2d%2.2d%2.2d%2.2d%2.2d",
                  tm1.tm_year+1900,tm1.tm_mon+1, tm1.tm_mday,
                     tm1.tm_hour, tm1.tm_min,tm1.tm_sec);
}
 
2.指定YYYYMMDDHH24MISS型的时间,格式化为time_t型的时间
time_t FormatTime2(char * szTime)
{
       struct tm tm1;
       time_t time1;
       sscanf(szTime, "M-----",   
                 &tm1.tm_year,
                 &tm1.tm_mon,
                 &tm1.tm_mday,
                 &tm1.tm_hour,
                 &tm1.tm_min,
                 &tm1.tm_sec);
            
       tm1.tm_year -= 1900;
       tm1.tm_mon --;
       tm1.tm_isdst=-1;
   
       time1 =mktime(&tm1);
       return time1;
}
 
 

posted on 2017-09-15 10:57  浪子回头jin不换  阅读(1316)  评论(0编辑  收藏  举报

导航