1. 程式人生 > >c++通過時間戳獲得自定格式時間

c++通過時間戳獲得自定格式時間

//通過時間戳得到字串日期(年月日)

std::string VHelper::GetTimeStringByTimeStamp( time_t timeStamp )
{
    timeStamp += 28800;
    struct tm *pt;
    pt = gmtime( &timeStamp );
    char str[100];
    strftime( str, sizeof( str ), "%Y-%m-%d", pt );

    std::string timeStr( str );

    return timeStr;
}

//通過時間戳得到字串日期(年月日時分秒格式)

std::string VHelper::GetYMDHMSTimeStringByTimeStamp( time_t timeStamp )
{
    timeStamp += 28800;
    struct tm *pt;
    pt = gmtime( &timeStamp );
    char str[100];
    strftime( str, sizeof( str ), "%Y-%m-%d %H:%M:%S", pt );

    std::string timeStr( str );

    return timeStr;
}

////通過時間戳得到字串的時間(時分秒)

std::string VHelper::GetHMSTimeStringByTimeStamp(time_t timeStamp)
{
    timeStamp += 28800;
    struct tm *pt;
    pt = gmtime( &timeStamp );
    char str[100];
    strftime( str, sizeof( str ), "%H:%M:%S", pt );

    std::string timeStr( str );

    return timeStr;
}