【Boost】time_duration, time_period, time_iterator

time_duration的例子
void test_ptime_duration()
{
	using namespace boost::posix_time;
	using namespace boost::gregorian;

	// time_duration构造的常用方法
	time_duration td1(1, 2, 3, 4);
	time_duration td2 = time_duration(1, 2, 3) + milliseconds(4) + microseconds(5);
	time_duration td3 = hours(1) + minutes(2) + seconds(3) + milliseconds(4) + microseconds(5);
	time_duration td4(duration_from_string("01:02:03.000"));

	// 计算总共的秒数, 毫秒数, 微秒数.
	std::cout << td1.total_seconds() << std::endl;
	std::cout << td1.total_milliseconds() << std::endl;
	std::cout << td1.total_microseconds() << std::endl;
}
time_period的例子
void test_ptime_period()
{
	using namespace boost::posix_time;
	using namespace boost::gregorian;

	// time_period构造的常用方法, 注意这样的构造函数是左闭右开, 如果end <= begin則时间段定义为无效.
	time_period tp1(ptime(date(2010, 11, 29), hours(11) + minutes(23) + seconds(45)), 
					ptime(date(2010, 12, 10), hours(22) + minutes(45) + seconds(56) + milliseconds(10)));	
	time_period tp2(ptime(date(2010, 11, 29), hours(11) + minutes(23) + seconds(45)),
					time_duration(2, 3, 4, 5));	
	time_period tp3(tp2);

	std::cout << tp1 << std::endl;
	std::cout << tp2 << std::endl;

	// shift, expand
	time_period tp5(tp2);
	tp5.shift(time_duration(1, 2, 3, 4)); 
	time_period tp6(tp2);
	tp6.expand(time_duration(2, 3, 4, 5)); 
	std::cout << tp5 << std::endl;
	std::cout << tp6 << std::endl;

	// begin, last, end
	ptime pt1 = tp2.begin();
	ptime pt2 = tp2.end();
	ptime pt3 = tp2.last();
	std::cout << pt1 << std::endl;
	std::cout << pt2 << std::endl;
	std::cout << pt3 << std::endl;

	// length, merge, span, intersects, intersection
	// 同date, 参见: http://blog.csdn.net/huang_xw/article/details/8239518	
}
time_iterator的例子
void test_ptime_iterator()
{
	using namespace boost::gregorian;
	using namespace boost::posix_time;
	date d(2012, 11, 30);
	ptime start(d);
	ptime end = start + hours(1);
	// 每次递增15分钟
	time_iterator titr(start, minutes(15)); 
	// 生成 00:00:00, 00:15:00, 00:30:00, 00:45:00
	while (titr < end) {
		std::cout << to_simple_string(*titr) << std::endl;
		++titr;
	}
}

你可能感兴趣的:(【Boost】time_duration, time_period, time_iterator)