首页 > 技术文章 > 时间戳转换为字符串

foreverstars 2015-09-10 17:32 原文

#include <cctype>
#include <iostream>
using namespace std;


char* gettimestr(time_t itime, char* gettime, const char* format)
{
    struct tm ptm;
    
    localtime_r(&itime, &ptm);
    strftime(gettime, 1024, format, &ptm);
    return gettime;
}

int main()
{
    time_t tNow;
    tNow = time(NULL);
    int offset1 = 10;
    tNow = tNow - offset1 * 60;
    char cptimebegin[100];
    char cptimeend[100];
    gettimestr(tNow, cptimebegin, "%Y-%m-%d %H:%M:00");
    printf("After offset1, the begin time is %s\n", cptimebegin);

    int offset2 = 10;
    gettimestr(tNow - offset2 * 60, cptimeend, "%Y-%m-%d %H:%M:00");
    printf("After offset2, the begin time is %s\n", cptimeend);

    return 0;
}

 

推荐阅读