时间戳转换为字符串

#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;
}

 

posted on 2015-09-10 17:32  霏霏暮雨  阅读(871)  评论(0)    收藏  举报

导航