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

<开篇>

本篇紧接着boost上篇叙述Boost::DateTime的时间处理。在C++中,常见的时间有time_t, FILETIME和tm,而boost中用ptime。

构造ptime

1.ptime的构造函数有四种:

1:      using namespace boost::posix_time;
2:      using namespace boost::gregorian;
3:  ptime pt(date(2013,Jan,24),time_duration(1,2,3)); //由date和time_duration构造
4:  ptime pt1(date(2013,Jan,24),hours()+nanosec(5));//改变形式的time_duration也能使用
5:  ptime pt2(p1);//拷贝构造函数
6:  ptime pt3(neg_infin);//特殊值构造
7:  ptime p;//默认构造函数,这里p等于not_a_date_time

2.用string构造ptime:

1:      std::string ts1("2013-01-30 23:32:22.000");//固定格式,小数点后支持6位
2:  ptime pt1(time_from_string(ts1));
3:  std::string ts2("20130130T233222");//没有分隔符的date和time
4:  ptime pt2(from_iso_string(ts2));
5:  

3.通过时钟构造ptime:

1:      ptime ct1(second_clock::local_time());
2:  ptime ct2(second_clock::universal_time());
3:  ptime ct3(microsec_clock::local_time());
4:  ptime ct4(microsec_clock::universal_time());
5:  

4.time_t和FILETIME构造ptime:

1:      ptime t = from_time_t(tt); // 其中tt为time_t
2:  ptime t1 = from_ftime<ptime>(ft); //其中ft为FILETIME

ptime访问日期时间

1:      using namespace boost::posix_time;
2:      using namespace boost::gregorian;
3:  ptime now(second_clock::local_time());
4:  std::cout << "today is: " << now.date() << std::endl;
5:  std::cout << "time is: " << now.time_of_day() << std::endl;
6:  

ptime转换为string

1:      std::string now_str(to_simple_string(now));
2:  std::string now_iso_str(to_iso_string(now));
3:  std::string now_iso_ext_str(to_iso_extended_string(now));
4:  std::cout << now_str << std::endl;
5:  std::cout << now_iso_str << std::endl;
6:  std::cout << now_iso_ext_str << std::endl;

ptime与tm,time_t,FILETIME互转

1.tm

 1:     using namespace boost::posix_time;
 2:     using namespace boost::gregorian;
 3:     tm pt_tm;
 4:     pt_tm.tm_year = 113;
 5:     pt_tm.tm_mon = 11;
 6:     pt_tm.tm_mday = 25;
 7:     pt_tm.tm_hour = 2;
 8:     pt_tm.tm_min = 23;
 9:     pt_tm.tm_sec = 40;
10:  
11:     ptime pt = data_from_tm(pt_tm);
12:     std::cout << pt << std::endl;
13:  
14:     pt = pt + hours(2);
15:     tm pt_tm1 = to_tm(pt);

2. time_t

 1:      using namespace boost::posix_time;
 2:      using namespace boost::gregorian;
 3:  
 4:  time_t now = time(NULL);
 5:  std::cout << "time_t : " << now << std::endl;
 6:  ptime now_pt = from_time_t(now);
 7:  std::cout << "ptime from time_t : " << now_pt.time_of_day() << std::endl;
 8:  tm* now_tm = gmtime(&now);
 9:  std::cout << "tm struct: hour : " << now_tm->tm_hour << std::endl;
10:  

3.FILETIME

1:      FILETIME ft;
2:  ft.dwHighDateTime = 29715317;
3:  ft.dwLowDateTime = 3865122988UL
4:      ptime pt = from_ftime<ptime>(ft);
5:  // pt ===> 2005-Jun-07 15:30:57.03958200
6:  

time_duration和time_period

 1:      using namespace boost::posix_time;
 2:      using namespace boost::gregorian;
 3:  
 4:  time_duration td(100,200,3,9);
 5:  std::cout << td << std::endl;
 6:  date d(2013,Feb,5);
 7:  ptime pt(d,minutes(10));
 8:  ptime pt1(d,hours(10));
 9:  time_period tp(pt,pt1);
10:  std::cout << tp << std::endl;
11:  

对于这两者的区别,一个是时间间隔,一个是时间起止的一个窗口。time_duration用于ptime的时间偏移计算为主。而time_period可以计算一个ptime时间点是否在这个时间区间内(参考contains函数)。time_period在创建之后可以扩展,可以平移,函数分别为expand和shift。请大家自己细究。

下一篇将介绍关于boost.datetime的格式化输入输出。

<完结>

时间: 2024-08-17 07:11:57

[Boost]boost的时间和日期处理-(2)时间的操作的相关文章

获得当前的系统时间和日期

1.获得当前的系统时间和日期 //获得系统时间 NSDate *  senddate=[NSDate date]; NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init]; [dateformatter setDateFormat:@"HH:mm"]; NSString *  locationString=[dateformatter stringFromDate:senddate]; //[dateformatte

时间和日期相关函数

在编程中,程序员会经常使用到日期相关的函数,比如:统计某段代码执行话费的时间等等. 1)时间和日期相关函数,需要导入time包 2)time.Time 类型,用于表示时间 3)获取到当前时间的方法:now := time.Now() //now 的类型就是time.Time func main() { //1.获取当前时间 now := time.Now() fmt.Printf("now=%v now type=%T \n", now, now) } 4)如何获取到其它的日期信息 f

Boost库简单运用——时间与日期的处理(一)

对于时间与日期的处理一直都是一个比较复杂的问题,而C++中对于时间与日期的处理也是比较简陋的,并不像Java.C#之流提供了非常方便易用的类.但随着Boost的推出,对于时间的处理也变得越来越简单方便了,今天我们就来学习一些较为方便的处理方式: 首先,我们先来了解Boost中对时间处理的一个比较基础的类,也是后续几个类的基类--timer. timer类可以测量时间的流逝,依据平台的不同,提供了毫秒甚至微秒级别的时间控制,我们也可以用它来做一个很简单的计时器,下面,我们通过代码来解释timer的

Boost库 对时间和日期的处理 date_timer库

/*Boost 对时间和日期的处理 提供了timer和data_time 库*/ //有关timer库提供了简易的度量时间和进度显示的功能可以用于性能测试等需要计时的任务 /* timer 的三个组件  计时器类timer  progress_timer和进度指示类progress_display timer 可测量时间的流逝,提供毫秒级的计时精确度 #include<boost\timer.hpp> #include<iostream> using namespace boost

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

<开篇> Boost.DateTime库提供了时间日期相关的计算.格式化.转换.输入输出等等功能,为C++的编程提供了便利.不过它有如下特点: 1. Boost.DateTime 只支持1400年以后的任何Gregorian日历日期.如果你需要计算再早的日期,则需要寻求其他库来支持. 日期和时间是编程过程中常用的操作.在C标准库中,<time.h>提供了time_t类型.和tm结构类型的时间日期相关函数.Windows API也提供了FILETIME类型的相关函数.由于这里是介绍b

c++ boost库学习一:时间和日期

timer类 #include <boost\timer.hpp> #include "iostream" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { boost::timer t; cout<<"max time span: "<<t.elapsed_max()/3600<<"h"<<endl; //596.5

asp 之 让实体中字段类型为DateTime的字段只显示日期不显示时间

       在我们平时的工作开发中,我们通常会遇到这样的一个问题:某个实体的某个字段是DateTime类型的,可是我们在界面上只想让它显示日期不显示时间! 一个订单实体: //订单类 public class order { //订单ID public int id{get;set;} //物品ID public int resId{get;set;} //物品名称 public string resName { get; set; } //物品价格 public decimal price

MATLAB——时间,日期及显示格式

一.日期和时间 1.生成指定格式日期和时间 标准日期格式 2.获取当前时间的数值 >> datestr(now,31) ans = 2017-09-11 19:56:26 >> datestr(now,'yyyy-mm-dd HH:MM:SS') ans = 2017-09-11 19:58:28 >> clock ans = 1.0e+03 * 2.0170 0.0090 0.0110 0.0200 0.0030 0.0264 >> datestr(now

Linux基础命令的操作(时间与日期,日历,计算器)

时间与日期 如果想知道Linux系统的当前时间,使用 date 命令即可显示. 上图显示的时间是2017年01月24日,星期二,18点50分26秒.CST为时区缩写(China Standard Time)表示中国的标准时间. 如果希望只显示年月日可以通过命令 date +%Y/%m/%d 当然,如果只想知道现在几点了,可以使用命令 date +%H:%M:%S 单独将时分秒显示出来 日历 如果想看这个月的日历,可以使用 cal 命令查看. 上图显示了2017年1月份的日历,并高亮显示出了今天的