利用boost获取时间并格式化
2015-03-09 23:37
232 查看
利用boost来获取当前时间又方便快捷,还不用考虑跨平台的问题。
1. 输出YYYYMMDD
[cpp]view plaincopyprint?#include <boost/date_time/gregorian/gregorian.hpp>
#define BOOST_DATE_TIME_SOURCE
std::string strTime = boost::gregorian::to_iso_string(\
boost::gregorian::day_clock::local_day());
std::cout << strTime.c_str() << std::endl;
[cpp]view plaincopyprint?#include <boost/date_time/gregorian/gregorian.hpp>
#define BOOST_DATE_TIME_SOURCE
std::string strTime = boost::gregorian::to_iso_string(\
boost::gregorian::day_clock::local_day());
std::cout << strTime.c_str() << std::endl;
[cpp]view plaincopyprint?#include <boost/date_time/gregorian/gregorian.hpp>
#define BOOST_DATE_TIME_SOURCE
std::string strTime = boost::gregorian::to_iso_string(\
boost::gregorian::day_clock::local_day());
std::cout << strTime.c_str() << std::endl;
#include <boost/date_time/gregorian/gregorian.hpp>
#define BOOST_DATE_TIME_SOURCE
std::string strTime = boost::gregorian::to_iso_string(\
boost::gregorian::day_clock::local_day());
std::cout << strTime.c_str() << std::endl;
2. 输出YYYYMMDD-HH:MM:SS [cpp]view plaincopyprint?#include <boost/date_time/posix_time/posix_time.hpp>
#define BOOST_DATE_TIME_SOURCE
std::string strTime = boost::posix_time::to_iso_string(\
boost::posix_time::second_clock::local_time());
// 这时候strTime里存放时间的格式是YYYYMMDDTHHMMSS,日期和时间用大写字母T隔开了
int pos = strTime.find('T');
strTime.replace(pos,1,std::string("-"));
strTime.replace(pos + 3,0,std::string(":"));
strTime.replace(pos + 6,0,std::string(":"));
std::cout << strTime.c_str() << std::endl;
[cpp]view plaincopyprint?#include <boost/date_time/posix_time/posix_time.hpp>
#define BOOST_DATE_TIME_SOURCE
std::string strTime = boost::posix_time::to_iso_string(\
boost::posix_time::second_clock::local_time());
// 这时候strTime里存放时间的格式是YYYYMMDDTHHMMSS,日期和时间用大写字母T隔开了
int pos = strTime.find('T');
strTime.replace(pos,1,std::string("-"));
strTime.replace(pos + 3,0,std::string(":"));
strTime.replace(pos + 6,0,std::string(":"));
std::cout << strTime.c_str() << std::endl;
[cpp]view plaincopyprint?#include <boost/date_time/posix_time/posix_time.hpp>
#define BOOST_DATE_TIME_SOURCE
std::string strTime = boost::posix_time::to_iso_string(\
boost::posix_time::second_clock::local_time());
// 这时候strTime里存放时间的格式是YYYYMMDDTHHMMSS,日期和时间用大写字母T隔开了
int pos = strTime.find('T');
strTime.replace(pos,1,std::string("-"));
strTime.replace(pos + 3,0,std::string(":"));
strTime.replace(pos + 6,0,std::string(":"));
std::cout << strTime.c_str() << std::endl;
#include <boost/date_time/posix_time/posix_time.hpp>
#define BOOST_DATE_TIME_SOURCE
std::string strTime = boost::posix_time::to_iso_string(\
boost::posix_time::second_clock::local_time());
// 这时候strTime里存放时间的格式是YYYYMMDDTHHMMSS,日期和时间用大写字母T隔开了
int pos = strTime.find('T');
strTime.replace(pos,1,std::string("-"));
strTime.replace(pos + 3,0,std::string(":"));
strTime.replace(pos + 6,0,std::string(":"));
std::cout << strTime.c_str() << std::endl;
3. 计算时间间隔。boost里计算时间间隔的功能很多很强大,我列举的仅仅是我目前用到的。
[cpp]view plaincopyprint?#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread.hpp>
#define BOOST_DATE_TIME_SOURCE
boost::posix_time::ptime time_now,time_now1;
boost::posix_time::millisec_posix_time_system_config::time_duration_type time_elapse;
// 这里为微秒为单位;这里可以将microsec_clock替换成second_clock以秒为单位;
time_now = boost::posix_time::microsec_clock::universal_time();
// sleep 100毫秒;
boost::this_thread::sleep(boost::posix_time::millisec(100));
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_seconds();
[cpp]view plaincopyprint?#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread.hpp>
#define BOOST_DATE_TIME_SOURCE
boost::posix_time::ptime time_now,time_now1;
boost::posix_time::millisec_posix_time_system_config::time_duration_type time_elapse;
// 这里为微秒为单位;这里可以将microsec_clock替换成second_clock以秒为单位;
time_now = boost::posix_time::microsec_clock::universal_time();
// sleep 100毫秒;
boost::this_thread::sleep(boost::posix_time::millisec(100));
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_seconds();
[cpp]view plaincopyprint?#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread.hpp>
#define BOOST_DATE_TIME_SOURCE
boost::posix_time::ptime time_now,time_now1;
boost::posix_time::millisec_posix_time_system_config::time_duration_type time_elapse;
// 这里为微秒为单位;这里可以将microsec_clock替换成second_clock以秒为单位;
time_now = boost::posix_time::microsec_clock::universal_time();
// sleep 100毫秒;
boost::this_thread::sleep(boost::posix_time::millisec(100));
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_seconds();
1. 输出YYYYMMDD
[cpp]view plaincopyprint?#include <boost/date_time/gregorian/gregorian.hpp>
#define BOOST_DATE_TIME_SOURCE
std::string strTime = boost::gregorian::to_iso_string(\
boost::gregorian::day_clock::local_day());
std::cout << strTime.c_str() << std::endl;
[cpp]view plaincopyprint?#include <boost/date_time/gregorian/gregorian.hpp>
#define BOOST_DATE_TIME_SOURCE
std::string strTime = boost::gregorian::to_iso_string(\
boost::gregorian::day_clock::local_day());
std::cout << strTime.c_str() << std::endl;
[cpp]view plaincopyprint?#include <boost/date_time/gregorian/gregorian.hpp>
#define BOOST_DATE_TIME_SOURCE
std::string strTime = boost::gregorian::to_iso_string(\
boost::gregorian::day_clock::local_day());
std::cout << strTime.c_str() << std::endl;
#include <boost/date_time/gregorian/gregorian.hpp>
#define BOOST_DATE_TIME_SOURCE
std::string strTime = boost::gregorian::to_iso_string(\
boost::gregorian::day_clock::local_day());
std::cout << strTime.c_str() << std::endl;
2. 输出YYYYMMDD-HH:MM:SS [cpp]view plaincopyprint?#include <boost/date_time/posix_time/posix_time.hpp>
#define BOOST_DATE_TIME_SOURCE
std::string strTime = boost::posix_time::to_iso_string(\
boost::posix_time::second_clock::local_time());
// 这时候strTime里存放时间的格式是YYYYMMDDTHHMMSS,日期和时间用大写字母T隔开了
int pos = strTime.find('T');
strTime.replace(pos,1,std::string("-"));
strTime.replace(pos + 3,0,std::string(":"));
strTime.replace(pos + 6,0,std::string(":"));
std::cout << strTime.c_str() << std::endl;
[cpp]view plaincopyprint?#include <boost/date_time/posix_time/posix_time.hpp>
#define BOOST_DATE_TIME_SOURCE
std::string strTime = boost::posix_time::to_iso_string(\
boost::posix_time::second_clock::local_time());
// 这时候strTime里存放时间的格式是YYYYMMDDTHHMMSS,日期和时间用大写字母T隔开了
int pos = strTime.find('T');
strTime.replace(pos,1,std::string("-"));
strTime.replace(pos + 3,0,std::string(":"));
strTime.replace(pos + 6,0,std::string(":"));
std::cout << strTime.c_str() << std::endl;
[cpp]view plaincopyprint?#include <boost/date_time/posix_time/posix_time.hpp>
#define BOOST_DATE_TIME_SOURCE
std::string strTime = boost::posix_time::to_iso_string(\
boost::posix_time::second_clock::local_time());
// 这时候strTime里存放时间的格式是YYYYMMDDTHHMMSS,日期和时间用大写字母T隔开了
int pos = strTime.find('T');
strTime.replace(pos,1,std::string("-"));
strTime.replace(pos + 3,0,std::string(":"));
strTime.replace(pos + 6,0,std::string(":"));
std::cout << strTime.c_str() << std::endl;
#include <boost/date_time/posix_time/posix_time.hpp>
#define BOOST_DATE_TIME_SOURCE
std::string strTime = boost::posix_time::to_iso_string(\
boost::posix_time::second_clock::local_time());
// 这时候strTime里存放时间的格式是YYYYMMDDTHHMMSS,日期和时间用大写字母T隔开了
int pos = strTime.find('T');
strTime.replace(pos,1,std::string("-"));
strTime.replace(pos + 3,0,std::string(":"));
strTime.replace(pos + 6,0,std::string(":"));
std::cout << strTime.c_str() << std::endl;
3. 计算时间间隔。boost里计算时间间隔的功能很多很强大,我列举的仅仅是我目前用到的。
[cpp]view plaincopyprint?#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread.hpp>
#define BOOST_DATE_TIME_SOURCE
boost::posix_time::ptime time_now,time_now1;
boost::posix_time::millisec_posix_time_system_config::time_duration_type time_elapse;
// 这里为微秒为单位;这里可以将microsec_clock替换成second_clock以秒为单位;
time_now = boost::posix_time::microsec_clock::universal_time();
// sleep 100毫秒;
boost::this_thread::sleep(boost::posix_time::millisec(100));
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_seconds();
[cpp]view plaincopyprint?#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread.hpp>
#define BOOST_DATE_TIME_SOURCE
boost::posix_time::ptime time_now,time_now1;
boost::posix_time::millisec_posix_time_system_config::time_duration_type time_elapse;
// 这里为微秒为单位;这里可以将microsec_clock替换成second_clock以秒为单位;
time_now = boost::posix_time::microsec_clock::universal_time();
// sleep 100毫秒;
boost::this_thread::sleep(boost::posix_time::millisec(100));
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_seconds();
[cpp]view plaincopyprint?#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread.hpp>
#define BOOST_DATE_TIME_SOURCE
boost::posix_time::ptime time_now,time_now1;
boost::posix_time::millisec_posix_time_system_config::time_duration_type time_elapse;
// 这里为微秒为单位;这里可以将microsec_clock替换成second_clock以秒为单位;
time_now = boost::posix_time::microsec_clock::universal_time();
// sleep 100毫秒;
boost::this_thread::sleep(boost::posix_time::millisec(100));
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_seconds();
相关文章推荐
- 利用boost获取时间并格式化
- 利用boost获取时间并格式化
- 利用boost计算运行时间间隔及获取系统时间并格式化
- 利用boost获取时间并格式化
- 利用boost获取时间并格式化的方法
- 利用boost获取时间并格式化
- 利用boost获取时间并格式化
- 利用boost计算运行时间间隔及获取系统时间并格式化
- 利用boost获取时间并格式化
- 时间格式化以及获取当前时间,利用日期生成随机数字
- 利用普通的input标签实现获取后台时间再格式化,再进行修改保存
- boost 获取时间并格式化
- 获取系统时间,并格式化
- 小例子--获取格式化的系统时间
- 利用uipickerview获取时间的倒计时程序
- PHP获取当前日期和时间格式化方法
- C语言下获取当前时间并格式化成字符串的方法-可用作截图或者录像的文件名
- C\C++时间处理函数[获取系统时间,求时间差,格式化输出等]
- 在iphone中获取格式化的时间
- 利用线程获取日期时间时间