首页 > 编程语言 >C++-C11-chrono-获取当前时间、获取阶段时间

C++-C11-chrono-获取当前时间、获取阶段时间

时间:2023-04-10 23:44:40浏览次数:42  
标签:std clock chrono C++ 获取 point time

C++-C11-chrono-获取当前时间、获取阶段时间

Linux下使用C++11的chrono库获取时间。

#include <chrono>
#include <thread>
#include <iostream>

int64_t getCurrentLocalTimeStamp(){
    std::chrono::time_point<std::chrono::system_clock, 
        std::chrono::milliseconds> tp = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
    auto tmp = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch());
    return tmp.count();
    return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}

char* print_time_point(std::chrono::system_clock::time_point timePoint) {
  std::time_t timeStamp = std::chrono::system_clock::to_time_t(timePoint);
  return std::ctime(&timeStamp);
}

int64_t getTimeSpan(){
    auto start = std::chrono::high_resolution_clock::now();
    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    /*
    std::chrono::nanoseconds
    std::chrono::microseconds
    std::chrono::milliseconds
    std::chrono::seconds
    std::chrono::minutes
    std::chrono::hours
    template< class Clock, class Duration >
    void sleep_until( const std::chrono::time_point<Clock,Duration>& sleep_time )
    */
    auto end = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double, std::milli> elapsed = end - start;
    return elapsed.count();
}

int main(){
    std::cout<<getTimeSpan()<<std::endl;
    std::cout<<getCurrentLocalTimeStamp()<<std::endl;
    std::cout<<print_time_point(std::chrono::high_resolution_clock::now())<<std::endl;
    return 0;
}

输出结果如下:

1000
1681140472905
Mon Apr 10 23:27:52 2023

标签:std,clock,chrono,C++,获取,point,time
From: https://www.cnblogs.com/yongchao/p/17304755.html

相关文章

  • C++第一天
    简单的C++程序实例#include<iostream>usingnamespacestd;intmain(){  cout<<"hello!"<<endl;  cout<<"welcomtoC++!"<<endl;  return0;}输出:Hello!welcometoC++!......
  • c++基础 打卡1
    一、面向对象的编程语言有的特点。    ①面向对象的编程语言最大的特点是结构化程序,二结构化程序的设计思路是自顶向下、逐步求精;其程序化结构是按功能划分为若干个基本模块,这些模块形成一个树状结构;各模块之间的关系尽可能简单,在功能上相对独立;每个模块内部均是由顺序、......
  • C++关键字
    staticstatic(静态的)静态变量作用范围在一个文件内,程序开始时分配空间,结束时释放空间,默认初始化为0,使用时可改变其值。静态变量或静态函数,只有本文件内的代码才可访问它,它的名字(变量名或函数名)在其它文件中不可见。因此也称为"文件作用域"。在C++类的成员变量被声明为static(称......
  • C++
    #include<iostream>usingnamespacestd;intmain(){ intyear; boolisLeapYear; cout<<"Entertheyear:"; cin>>year; isLeapYear=((year%4==0&&year%100!=0)||(year%400==0)); if(isLeapYear) cout<<year<<"isa......
  • C++通讯录管理系统
    编辑器vs2019代码如下:#include<iostream>usingnamespacestd;#defineMAX1000//最大人数//联系人信息结构体structPerson{ stringm_name; //性别1男2女 intm_sex; intm_age; stringm_phone; stringm_addr;};//通讯录结构体structAddressbo......
  • 3500/92 136180-01 从超声波计数器获取数据
    3500/92136180-01从超声波计数器获取数据当乘客在预先定义的时间之前经过传感器2附近时,计数器会将该动作记录为“上车”事件。如果传感器2没有检测到乘客,那么计数器将保持相同的值。图6a显示了乘客的“上车”事件。当一个人下车时,过程是相似的。在这种情况下,乘客将走到传感器2......
  • C++ 性能优化 - for循环条件中不要调用函数
    for循环条件中调用普通函数#include<iostream>#include<chrono>usingnamespacestd;longlongcount=0;constintN=10;intgetSize(){cout<<"getsize"<<endl;returnN+1;}voidtimeMeasure(void(*f)()){autobeg......
  • C/C++猜单词系统[2023-04-10]
    C/C++猜单词系统[2023-04-10]程序设计题二:猜单词面向专业:非计算机专业难度:41问题描述请从一片英文的短文中任意提取一个单词,给出该单词的字母数量,让游戏者猜单词的拼写字母,游戏者每次只能猜一个字母,如果游戏者猜的字母在单词中,单词中所有的该字母将被视为已猜出,例如:如果原单......
  • C/C++驾驶员理论课程模拟考试与学习系统[2023-04-10]
    C/C++驾驶员理论课程模拟考试与学习系统[2023-04-10]程序设计题:驾驶员理论课程模拟考试与学习系统出题人:金仙力面向专业:计算机科学与技术难度等级:41问题描述要求编写一个程序,模拟驾驶员科目一的考试,要求具有良好的操作界面。管理员负责试题库的管理(编辑、删除、增加等)......
  • C++,OpenCV鼠标操作(8)
    鼠标事件响应voidsetMouseCallback(constString&winname,MouseCallbackonMouse,void*userdata=0);/******************************************************************** winname: 监听窗口名称* onMouse: 鼠标事件回调函数* userdata: 递给回调函数的可......