c++标准中的时间函数

c++标准中的时间函数

说明


// Time manipulation

// clock
// returns raw processor clock time since the program is started 
// (function)

// time 
// returns the current time of the system as time since epoch
// (function)

// difftime
// computes the difference between times
// (function)

// timespec_get(C++17)
// returns the calendar time based on a given time base
// (function)


// Format conversions

// ctime
// converts a std::time_t object to a textual representation
// (function)

// asctime
// converts a std::tm object to a textual representation
// (function)

// strftime
// converts a std::tm object to custom textual representation
// (function)

// wcsftime
// converts a std::tm object to custom wide string textual representation
// (function)

// gmtime
// converts time since epoch to calendar time expressed as Universal Coordinated Time
// (function)

// localtime
// converts time since epoch to calendar time expressed as local time
// (function)

// mktime
// converts calendar time to time since epoch
// (function)

代码


#define _CRT_SECURE_NO_WARNINGS
#include <ctime>
#include <iostream>




int main()
{
    time_t tm1 = std::time(nullptr);
    clock_t cl1 = std::clock();
    char* pc = std::ctime(&tm1);

    std::cout << "time:\t" << tm1 << std::endl;
    std::cout << "clock:\t" << cl1 << std::endl;
    std::cout << "ctime:\t" << pc << std::endl;

    char buffer[80];
    wchar_t wcbuffer[80];
    // 本地时间
    std::cout << "local " << std::endl;
    std::tm* plocal = std::localtime(&tm1);
    time_t mk1 = std::mktime(plocal);
    std::cout << "mktime:\t" << mk1 << std::endl;
    std::strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", plocal);
    std::cout << "strftime:\t" << buffer << std::endl;
    std::wcsftime(wcbuffer, 80, L"%Y-%m-%d %H:%M:%S", plocal);
    std::wcout << L"wcsftime:\t" << wcbuffer << std::endl;
    char* asc = std::asctime(plocal);
    std::cout << "asctime:\t" << asc << std::endl;


    // 通用时间
    std::cout << "Universal Coordinated" << std::endl;
    std::tm* ptime = std::gmtime(&tm1);
    time_t mk2 = std::mktime(ptime);
    std::cout << "mktime:\t" << mk2 << std::endl;
    std::strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", ptime);
    std::cout << "strftime:\t" << buffer << std::endl;
    std::wcsftime(wcbuffer, 80, L"%Y-%m-%d %H:%M:%S", ptime);
    std::wcout << L"wcsftime:\t" << wcbuffer << std::endl;
    char* asctime = std::asctime(ptime);
    std::cout << "asctime:\t" << asctime << std::endl;

    // c++17
    // windows下timespec不在std里
    timespec ts;
    timespec_get(&ts, TIME_UTC);
    std::strftime(buffer, sizeof(buffer), "%D %T", std::gmtime(&ts.tv_sec));
    std::cout << "Current time:\t" << buffer << '.' << ts.tv_nsec << " UTC" << std::endl;
    
    time_t tm2 = std::time(nullptr);
    double d = std::difftime(tm2, tm1);
    std::cout << "Wall time passed:\t " << d << " s." << std::endl;
    return 0;
}

参考

https://en.cppreference.com/w/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值