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

今天我们就开始boost的日期处理部分,日期有太多需要细细考虑的地方,遇到日期处理问题时,自己写一个处理类,那显然是极其耗时的。学习了今天的内容之后,相信我们在日期处理时将更加得心应手。今天我们学习的是boost日期处理的核心类——date。同样,下面我们从代码入手进行学习:

#include <iostream>
#include <boost/date_time/gregorian/gregorian.hpp>

int main()
{
    boost::gregorian::date d1(2014, 9, 26);
    std::cout << d1 << std::endl;
    std::cout << d1.year() << std::endl;
    std::cout << d1.month() << std::endl;
    std::cout << d1.day() << std::endl;
    boost::gregorian::date::ymd_type ymd = d1.year_month_day();
    std::cout << ymd.year << std::endl;
    std::cout << ymd.month << std::endl;
    std::cout << ymd.day << std::endl;
    return 0;
}

date类算是一个比较灵活的类,初始化不仅可以传类似(2014, 9, 26)的形式,也可以传(2014, Sep, 26)的形式,当然也可以以同类对象初始化咯。对日期的获取可以使用类方法year(), month(), day(),或者可以声明date支持的ymd_type结构体,如上使用方法。若date类就这点用处,那估计大多数人都要说:“这么简陋的功能,自己写一个也比这个好。”

date的功能当然不止这些,下面我们再通过一段代码来了解它另外的功能:

#include <iostream>
#include <boost/date_time/gregorian/gregorian.hpp>

int main()
{
    boost::gregorian::date d1(2014, 9, 26);
    std::cout << d1.day_of_week() << std::endl;
    std::cout << d1.day_of_year() << std::endl;
    std::cout << d1.end_of_month() << std::endl;
    std::cout << boost::gregorian::date(2014, 9, 26).week_number() << std::endl;
    return 0;
}

date类提供了成员方法可以获取初始化的日期是星期几,这一年的第几天和这个月最后一天是几号。从上面代码中的函数名我们就能了解它们谁是谁了。另外,date还能判断初始化日期是这一年的第几周。注意:boost-1.56.0中,day_of_week直接返回星期几的英文,但有些老版本中是返回数字的,0代表星期天。

时间: 2024-08-28 00:48:54

Boost库简单运用——时间与日期的处理(三)的相关文章

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

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

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

在上文中,我们了解了一种对时间进行处理的boost类,今天我们就来学习另一种时间处理的类--progress_timer,它继承于timer类,拥有timer类的功能,又在其易用性上更进一步,接下来我们以代码对该类进行说明: #include <iostream> #include <boost/progress.hpp> int main() { boost::progress_timer pt1; std::cout << pt1.elapsed_max() <

(一)boost库之日期、时间

(一)boost库之日期.时间 一.计时器 计时器,通常在一个项目中统计一个函数的执行时间是非常实用的. #include <boost/timer.hpp> void PrintUserTime() { boost::timer t; //定义一个计时类,开始计时 std::cout << "可度量的最大时间:" << t.elapsed_max()/3600 << "h" << std::endl; s

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

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

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

简单编写makefile文件,实现GCC4.9编译项目,加入boost库测试等等。。

一.需要用到的hw.cpp hw.h funtest.cpp funtest.h makefile 几个测试文件 1.hw.cpp代码如下: #include "hw.h" #include "funtest.h" using namespace std; using namespace boost; int main() { timer t; { int i=1; } auto i="abc"; cout<<i<<end

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

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

【ThinkingInC++】48、用标准C库中的时间函数生成简单的Time类

Cpptime.h /** * 书本:[ThinkingInC++] * 功能:用标准C库中的时间函数生成简单的Time类 * 时间:2014年9月11日07:53:56 * 作者:cutter_point */ #ifndef CPPTIME_H_INCLUDED #define CPPTIME_H_INCLUDED #include<ctime> #include<cstring> //这里ctime和cstring是不包含using namespace std;的而后面加.h

【c++】简单模拟实现boost库下的shared-ptr

//简单模拟实现boost库下的shared_ptr #include <iostream> #include <string.h> using namespace std; class shared_ptr_Rep { friend class shared_ptr; public: shared_ptr_Rep(const char *str = " ") :count(0) { px = new char[strlen(str) + 1]; strcpy(