获取时间方式

格式一:YYYYMMDD

#include<iostream>
#include<string>
#include<boost/date_time/gregorian/gregorian.hpp> using namespace std; int main(int argc, char* argv)
{
string strTime = boost::gregorian::to_iso_string(boost::gregorian::day_clock::local_day());
cout<<strTime<<endl;

  //getchar();
return ;
}

输出格式结果

输出格式二:YYYYMMDD-HH:MM:SS

#include<iostream>
#include<string>
#include<boost/date_time/posix_time/posix_time.hpp> using namespace std; int main(int argc, char* argv)
{
   string strPosixTime = boost::posix_time::to_iso_string(boost::posix_time::second_clock::local_time());

     //这时候strPosixTime里存放时间的格式是YYYYMMDDTHHMMSS,日期和时间用大写字母T隔开了,这里是以秒为单元,也可以换成
     //string strPosixTime = boost::posix_time::to_iso_string(boost::posix_time::microsec_clock::local_time());

    int pos = strPosixTime.find('T');
strPosixTime.replace(pos,,std::string("-"));
strPosixTime.replace(pos + ,,std::string(":"));
strPosixTime.replace(pos + ,,std::string(":"));
    cout<<strPosixTime<<endl;
return ;
}

输出格式结果

获取时间间隔

#include<iostream>
#include<string>
#include<boost/thread.hpp>
#include<boost/date_time/posix_time/posix_time.hpp> using namespace std; boost::posix_time::ptime time_now,time_now1;
boost::posix_time::millisec_posix_time_system_config::time_duration_type time_elapse; int main(int argc, char* argv)
{ // 这里为微秒为单位;这里可以将microsec_clock替换成second_clock以秒为单位;
time_now = boost::posix_time::microsec_clock::universal_time(); // sleep 100毫秒;
boost::this_thread::sleep(boost::posix_time::millisec()); time_now1 = boost::posix_time::microsec_clock::universal_time(); time_elapse = time_now1 - time_now; // 类似GetTickCount,只是这边得到的是2个时间的ticket值的差,以微秒为单位;
int ticks = time_elapse.ticks(); // 得到两个时间间隔的秒数;
int sec = time_elapse.total_milliseconds(); cout<<time_now<<" "<<time_now1<<endl;
cout<<time_elapse<<endl;
cout<<ticks<<endl;
cout<<sec<<endl; getchar();
return ;
}

结果输出

【Boost】boost库获取格式化时间的更多相关文章

  1. Python获取当前时间_获取格式化时间_格式化日期

    Python获取当前时间_获取格式化时间: Python获取当前时间: 使用 time.time( ) 获取到距离1970年1月1日的秒数(浮点数),然后传递给 localtime 获取当前时间 #使 ...

  2. python 获取格式化时间

    #!/usr/bin/python # -*- coding: UTF- -*- import time localtime = time.asctime( time.localtime(time.t ...

  3. 【javascript】获取 格式化时间

    function getDate() { var myDate = new Date(); var month = myDate.getMonth() + 1; var day = myDate.ge ...

  4. [Boost]boost的时间和日期处理-(1)日期的操作

    <开篇> Boost.DateTime库提供了时间日期相关的计算.格式化.转换.输入输出等等功能,为C++的编程提供了便利.不过它有如下特点: 1. Boost.DateTime 只支持1 ...

  5. Python获取当前时间及时间转换(datetime)

    datetime是Python处理日期和时间的标准库 获取当前时间 import datetime day = datetime.datetime.now() day2 = datetime.date ...

  6. js 格式化时间、字符串指定长度、随机字符串

    格式化字符串长度 方法 function formatWidth(str, width){ str += '' if(str.length<width) '+str, width) else r ...

  7. 一起学习Boost标准库--Boost.texical_cast&format库

    今天接续介绍有关字符串表示相关的两个boost库: lexical_cast 将数值转换成字符串 format 字符串输出格式化 首先,介绍下lexical_cast ,闻其名,知其意.类似C中的at ...

  8. 如何在WINDOWS下编译BOOST C++库 .

    如何在WINDOWS下编译BOOST C++库 cheungmine 2008-6-25   写出来,怕自己以后忘记了,也为初学者参考.使用VC8.0和boost1.35.0.   1)下载boost ...

  9. Boost线程库学习笔记

    一.创建一个线程 创建线程 boost::thread myThread(threadFun); 需要注意的是:参数可以是函数对象或者函数指针.并且这个函数无参数,并返回void类型. 当一个thre ...

随机推荐

  1. Django中ORM增删改查

    新建模型 class Author(models.Model): nid = models.AutoField(primary_key=True) name=models.CharField( max ...

  2. python数据类型、操作符

    python中数据类型包含:int,float,boolean,string,list(列表),set(集合),dictionary(字典) 数据类型转换: ①字符串 转 int:>>&g ...

  3. javascript.history.go();

    转自:http://www.mikebai.com/Article/2009-11/757.html <input type=button value=刷新 onclick="wind ...

  4. java web工程启动socket服务

    1.新建web工程 2.自定义类 实现ServletContextListener 接口 在contextInitialized方法中启动socket服务的线程 在contextDestroyed方法 ...

  5. BZOJ4923 [Lydsy1706月赛]K小值查询

    题意 维护一个长度为n的正整数序列a_1,a_2,...,a_n,支持以下两种操作: 1 k,将序列a从小到大排序,输出a_k的值. 2 k,将所有严格大于k的数a_i减去k. \(n \leq 10 ...

  6. 学习动态性能表(22)V$resource_limit

    学习动态性能表 第20篇--V$resource_limit  2007.6.15 就一条SQL语句供你参考: select * from V$RESOURCE_LIMIT where resourc ...

  7. mybatis与oracle使用总结

    Oracle使用总结 1.新建表删除表 新建表语句: CREATE TABLE +表名{ } create table AFA_USER ( USER_ID VARCHAR2() not null, ...

  8. iptables的启动和关闭【转载】

    原文网址:http://os.51cto.com/art/201103/249049.htm iptables的启动和关闭: 1.启动和关闭iptables 下面将正式使用iptables来创建防火墙 ...

  9. 修改分区后的 Grub rescue

    声明:这里用到的知识不是原创,综合了几篇教程的成果.找的时候比较混乱,所以来源已经不确定.希望原作者见谅. 系统是Windows 8.1 和 Ubuntu 14.04, Windows是先装的, gr ...

  10. bzoj 3398 [Usaco2009 Feb]Bullcow 牡牛和牝牛——前缀和优化dp / 排列组合

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3398 好简单呀.而且是自己想出来的. dp[ i ]表示最后一个牡牛在 i 的方案数. 当前 ...