boost的posix_time用法详解01

// boost_time.cpp : 定义控制台应用程序的入口点。
//made by davidsu33
//2014-5-11
//the usage of posix_time

#include "stdafx.h"
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
#include <cassert>

using namespace std;

#define SHOW_VARIABLE(x){	cout<<#x<<"="<<x<<endl;}

void show_tm(std::tm & atm)
{
	cout<<"year="<<atm.tm_year<<" month="<<atm.tm_mon<<" day="<<atm.tm_mday<<endl;
	cout<<"hour="<<atm.tm_hour<<" minute="<<atm.tm_min<<" sec="<<atm.tm_sec<<endl;
}

//时间区间
void use_time_duration()
{
	//mili-sec 毫秒
	//mico-sec 微妙
	//nano-sec 纳秒
	//1hour 20minutes 30seconds 1000mico-seconds
	boost::posix_time::time_duration td(1, 20, 30, 1000);
	//cout<<"td="<<td<<endl;
	SHOW_VARIABLE(td);

	//根据所给的数值自动的进制
	boost::posix_time::time_duration td2(1, 60, 60, 1000*1000*6);
	SHOW_VARIABLE(td2);
	boost::posix_time::time_duration td3(2, 1, 6);
	assert(td3 == td2);
	SHOW_VARIABLE(td3);

	//hours minutes seconds 分别从time_duration派生
	boost::posix_time::hours h(2);
	boost::posix_time::minutes m(1);
	boost::posix_time::seconds s(6);
	boost::posix_time::time_duration td4 = h + m + s;
	assert(td4 == td3);
	SHOW_VARIABLE(td4);

	//duration_from_string
	std::string str = "2:1:6:0";
	boost::posix_time::time_duration td5 = boost::posix_time::duration_from_string(str);
	assert(td5 == td4);
	SHOW_VARIABLE(td5);

	//转换为tm结构
	std::tm tm2 = boost::posix_time::to_tm(td5);
	show_tm(tm2);
}

void uage_of_ptime()
{
	//ptime时间点
	//2014年5月11号 凌晨1点
	boost::posix_time::ptime
		t1(boost::gregorian::date(2014, 5, 11), boost::posix_time::hours(1));
	SHOW_VARIABLE(t1);

	//time_from_string
	std::string s1 = "2014-5-11 01:00:00";
	boost::posix_time::ptime t2 =
		boost::posix_time::time_from_string(s1);
	SHOW_VARIABLE(t2);

	//from_iso_string
	boost::posix_time::ptime t3 =
		boost::posix_time::from_iso_string("20140511T010000");
	SHOW_VARIABLE(t3);

}

int _tmain(int argc, _TCHAR* argv[])
{
	use_time_duration();
	uage_of_ptime();
	getchar();

	return 0;
}

boost的posix_time用法详解01

时间: 2024-08-17 23:39:35

boost的posix_time用法详解01的相关文章

boost::filesystem常用用法详解

提示: filesystem库提供了两个头文件,一个是<boost/filesystem.hpp>,这个头文件包含主要的库内容.它提供了对文件系统的重要操作.同时它定义了一个类path,正如大家所想的,这个是一个可移植的路径表示方法,它是filesystem库的基础. 一个是<boost/filesystem/fstream.hpp>,是对std::fstream的一个补充,使用可以使用类boost::path作为参数,从而使得filesystem库与标准库的关系更亲密. 由于文件

boost::lexical_cast常见用法详解之万能转换

提示: 虽然在c中可是使用类似于atoi之类的函数对字符串转换成整型,但是我们在这儿还是推荐使用这个函数 如果转换发生了错误,lexical_cast会抛出一个bad_lexical_cast异常,因此程序中需要对其进行捕捉. 下面是程序示例: #include <iostream> #include <boost/lexical_cast.hpp> using namespace std; using namespace boost; int main() { string s

Linux tar 命令参数及用法详解--Linux打包备份命令

linux tar命令参数及用法详解--linux打包备份命令 tar命令 tar - tar 档案文件管理程序的 GNU 版本.下面将逐个介绍其含义tar [-cxtzjvfpPN] 文件与目录 ....常用参数:-c :建立一个压缩文件的参数指令(create 的意思):-x :解开一个压缩文件的参数指令!-t :查看 tarfile 里面的文件!特别注意,在参数的下达中, c/x/t 仅能存在一个!不可同时存在!因为不可能同时压缩与解压缩.-z :是否同时具有 gzip 的属性?亦即是否需

AFNetwork 作用和用法详解

AFNetworking是一个轻量级的iOS网络通信类库.它建立在NSURLConnection和NSOperation等类库的基础上,让很多网络通信功能的实现变得十分简单.它支持HTTP请求和基于REST的网络服务(包括GET.POST. PUT.DELETE等).支持ARC. Github地址:https://github.com/AFNetworking/AFNetworking // // MJViewController.m // 03.AFN演练 // // Created by a

expdp / impdp 用法详解

一  关于expdp和impdp     使用EXPDP和IMPDP时应该注意的事项:EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用.EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端使用,不能在客户端使用.IMP只适用于EXP导出的文件,不适用于EXPDP导出文件:IMPDP只适用于EXPDP导出的文件,而不适用于EXP导出文件.expdp或impdp命令时,可暂不指出用户名/密码@实例名 as 身份,然后根据提示再输入,如:expdp sche

53 kvm及libvirt、使用virsh管理kvm虚拟机、网络虚拟化技术基础、网络名称空间netns用法详解

01 kvm及libvirt [[email protected] ~]# yum install libvirt libvirt-client python-virtinst virt-manager virt-install -y [[email protected] ~]# yum -y install qemu-kvm [[email protected] ~]# systemctl start libvirtd.service #创建桥 [[email protected] ~]# v

Oracle中常用的to_Char用法详解

Oracle中常用的to_Char用法详解(有FMT的详细列表) The following are number examples for the to_char function. to_char(1210.73, '9999.9') would return '1210.7' to_char(1210.73, '9,999.99') would return '1,210.73' to_char(1210.73, '$9,999.00') would return '$1,210.73'

iOS多线程实现方案详解01——NSThread

NSThread 一.创建和启动线程 1.开线程的几种方式 1)先创建线程,后启动 NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil]; [thread start]; 2)创建线程后自动直接启动 [NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil]; [sel

转:sql cast和convert用法详解

原文:http://www.2cto.com/database/201310/250880.html sql cast和convert用法详解 总结: 以下这条仅仅适用于mysql SELECT {fn CONCAT(CONVERT(user_id,CHAR),USER_NAME)} AS str FROM t_sys_user 以下这条仅仅适用于sqlserver2008 SELECT {fn CONCAT(CONVERT(CHAR,user_id),USER_NAME)} AS str FR