把ms的时间戳转换成格式化输出到ms的字符串

static std::string get_format_time(int64_t ms) {
    int microsec = ms%1000;

    time_t t(ms / 1000);
    struct tm *p = localtime(&t);
    char s[64];
    int pos = strftime(s, sizeof(s), "%Y%m%d-%H:%M:%S", p);

    sprintf(&s[pos], ".%03d", microsec);

    return std::string(s);
}

static std::string get_curr_format_time() {
    struct  timeval tv;
    gettimeofday(&tv, 0x0);

    int microsec = tv.tv_usec/1000;
    time_t t(tv.tv_sec);
    struct tm *p = localtime(&t);
    char s[64];
    int pos = strftime(s, sizeof(s), "%Y%m%d-%H:%M:%S", p);

    sprintf(&s[pos], ".%03d", microsec);

    return std::string(s);
}

int main()
{
    std::cout << get_format_time(1604992150091) << std::endl;

    return 0;
}

 

posted @ 2020-11-10 16:17  小鼬就是我  阅读(288)  评论(0编辑  收藏  举报