boost初探-日期与时间_day of month value is out of range linux系统boost(1)

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前在阿里

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Linux运维全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以点击这里获取!

namespace boost {

// timer -------------------------------------------------------------------//

// A timer object measures elapsed time.

// It is recommended that implementations measure wall clock rather than CPU
// time since the intended use is performance measurement on systems where
// total elapsed time is more important than just process or CPU time.

// Warnings: The maximum measurable elapsed time may well be only 596.5+ hours
// due to implementation limitations. The accuracy of timings depends on the
// accuracy of timing information provided by the underlying platform, and
// this varies a great deal from platform to platform.

class timer
{
public:
timer() { _start_time = std::clock(); } // postcondition: elapsed()==0
// timer( const timer& src ); // post: elapsed()==src.elapsed()
// ~timer(){}
// timer& operator=( const timer& src ); // post: elapsed()==src.elapsed()
void restart() { _start_time = std::clock(); } // post: elapsed()==0
double elapsed() const // return elapsed time in seconds
{ return double(std::clock() - _start_time) / CLOCKS_PER_SEC; }

double elapsed_max() const // return estimated maximum value for elapsed()
// Portability warning: elapsed_max() may return too high a value on systems
// where std::clock_t overflows or resets at surprising values.
{
return (double((std::numeric_limitsstd::clock_t::max)())
- double(_start_time)) / double(CLOCKS_PER_SEC);
}

double elapsed_min() const // return minimum value for elapsed()
{ return double(1)/double(CLOCKS_PER_SEC); }

private:
std::clock_t _start_time;
}; // timer

} // namespace boost

#endif // BOOST_TIMER_HPP


* timer模块需要依赖下面三个文件



#include <boost/config.hpp>
#include
#include <boost/limits.hpp>


* timer模块存在于命名空间namespace boost
* timer模块的主类为timer,包含1个构造函数,4个成员函数,1个成员变量


可以看出timer模块是非常简单的,当然我们刚开始学习一些简单的模块可以帮助我们建立一些使用boost的基础,让我们更快的上手,不失为一个好的选择。


下面我们用一点代码来简单使用一下timer模块



#include
using namespace std;

#include <boost/timer.hpp>
using namespace boost;

int main()
{
timer t; //定义一个timer对象,通过该对象进行时间的相应操作

cout << CLOCKS_PER_SEC << endl;
cout << "max timespan:"
    << t.elapsed\_max() /3600 << "h" <<endl;
cout << "min timespan:"
    << t.elapsed\_min() << "s" << endl;
cout << "now time elapsed:"
    << t.elapsed() <<"s" << endl;

}


#### 2.progress\_timer


progress\_timer也是一个计时器,它派生自timer,会在析构时自动输出时间,省去了timer手动调用elapsed()的工作,是一个相当方便的自动计时的小工具。


progress\_timer位于命名空间boost,需要包含的头文件如下



#include <boost/progress.hpp>
using namespace boost;


先看progress\_timer的源码



// boost progress.hpp header file ------------------------------------------//

// Copyright Beman Dawes 1994-99. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

// See http://www.boost.org/libs/timer for documentation.

// Revision History
// 1 Dec 01 Add leading progress display strings (suggested by Toon Knapen)
// 20 May 01 Introduce several static_casts<> to eliminate warning messages
// (Fixed by Beman, reported by Herve Bronnimann)
// 12 Jan 01 Change to inline implementation to allow use without library
// builds. See docs for more rationale. (Beman Dawes)
// 22 Jul 99 Name changed to .hpp
// 16 Jul 99 Second beta
// 6 Jul 99 Initial boost version

#ifndef BOOST_PROGRESS_HPP
#define BOOST_PROGRESS_HPP

#include <boost/config/header_deprecated.hpp>
BOOST_HEADER_DEPRECATED( “the facilities in <boost/timer/timer.hpp> or <boost/timer/progress_display.hpp>” )

#include <boost/timer.hpp>
#include <boost/noncopyable.hpp>
#include <boost/cstdint.hpp> // for uintmax_t
#include // for ostream, cout, etc
#include // for string

namespace boost {

// progress_timer ----------------------------------------------------------//

// A progress_timer behaves like a timer except that the destructor displays
// an elapsed time message at an appropriate place in an appropriate form.

class progress_timer : public timer, private noncopyable
{
public:
explicit progress_timer( std::ostream & os = std::cout )
// os is hint; implementation may ignore, particularly in embedded systems
: timer(), noncopyable(), m_os(os) {}
~progress_timer()
{
// A) Throwing an exception from a destructor is a Bad Thing.
// B) The progress_timer destructor does output which may throw.
// C) A progress_timer is usually not critical to the application.
// Therefore, wrap the I/O in a try block, catch and ignore all exceptions.
try
{
// use istream instead of ios_base to workaround GNU problem (Greg Chicares)
std::istream::fmtflags old_flags = m_os.setf( std::istream::fixed,
std::istream::floatfield );
std::streamsize old_prec = m_os.precision( 2 );
m_os << elapsed() << " s\n" // “s” is System International d’Unites std
<< std::endl;
m_os.flags( old_flags );
m_os.precision( old_prec );
}

catch (...) {} // eat any exceptions

} // ~progress_timer

private:
std::ostream & m_os;
};

// progress_display --------------------------------------------------------//

// progress_display displays an appropriate indication of
// progress at an appropriate place in an appropriate form.

// NOTE: (Jan 12, 2001) Tried to change unsigned long to boost::uintmax_t, but
// found some compilers couldn’t handle the required conversion to double.
// Reverted to unsigned long until the compilers catch up.

class progress_display : private noncopyable
{
public:
explicit progress_display( unsigned long expected_count_,
std::ostream & os = std::cout,
const std::string & s1 = “\n”, //leading strings
const std::string & s2 = “”,
const std::string & s3 = “” )
// os is hint; implementation may ignore, particularly in embedded systems
: noncopyable(), m_os(os), m_s1(s1), m_s2(s2), m_s3(s3) { restart(expected_count_); }

void restart( unsigned long expected_count_ )
// Effects: display appropriate scale
// Postconditions: count()==0, expected_count()==expected_count_
{
_count = _next_tic_count = _tic = 0;
expected_count = expected_count;

m_os << m_s1 << "0% 10 20 30 40 50 60 70 80 90 100%\n"
     << m_s2 << "|----|----|----|----|----|----|----|----|----|----|"
     << std::endl  // endl implies flush, which ensures display
     << m_s3;
if ( !_expected_count ) _expected_count = 1;  // prevent divide by zero

} // restart

unsigned long operator+=( unsigned long increment )
// Effects: Display appropriate progress tic if needed.
// Postconditions: count()== original count() + increment
// Returns: count().
{
if ( (_count += increment) >= _next_tic_count ) { display_tic(); }
return _count;
}

unsigned long operator++() { return operator+=( 1 ); }
unsigned long count() const { return _count; }
unsigned long expected_count() const { return _expected_count; }

private:
std::ostream & m_os; // may not be present in all imps
const std::string m_s1; // string is more general, safer than
const std::string m_s2; // const char *, and efficiency or size are
const std::string m_s3; // not issues

unsigned long _count, _expected_count, _next_tic_count;
unsigned int _tic;
void display_tic()
{
// use of floating point ensures that both large and small counts
// work correctly. static_cast<>() is also used several places
// to suppress spurious compiler warnings.
unsigned int tics_needed = static_cast((static_cast(_count)
/ static_cast(_expected_count)) * 50.0);
do { m_os << ‘*’ << std::flush; } while ( ++_tic < tics_needed );
_next_tic_count =
static_cast((_tic/50.0) * static_cast(_expected_count));
if ( _count == _expected_count ) {
if ( _tic < 51 ) m_os << ‘*’;
m_os << std::endl;
}
} // display_tic
};

} // namespace boost

#endif // BOOST_PROGRESS_HPP


#### 3.data\_time库


data\_time库需要编译才能使用,data\_time库包括两个部分,处理日期的gregorian,处理时间的posix\_time,它们各自需要包含的头文件如下:



//处理日期的组件
#include <boost/date_time/gregorian/gregorian.hpp>
using namespace boost::gregorian

//处理时间的组件
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace boost::posix_time


##### 3.1gregorian


用下面一段代码来测试一下这个模块



#include
using namespace std;

//#define DATE_TIME_NO_DEFAULT_CONSTRUCTOR
#include <boost/date_time/gregorian/gregorian.hpp>
using namespace boost::gregorian;

//

void case1()
{
date d1;
date d2(2010,1,1);
date d3(2000, Jan , 1);
date d4(d2);

assert(d1 == date(not_a_date_time));
assert(d2 == d4);
assert(d3 <  d4);

}

//

void case2()
{
date d1 = from_string(“1999-12-31”);
date d2 ( from_string(“2015/1/1”) );
date d3 = from_undelimited_string(“20011118”) ;

cout << d1 << d2 << d3 << endl;

cout << day_clock::local\_day()    << endl;
cout << day_clock::universal\_day() << endl;

}

//
void case3()
{
date d1(neg_infin);
date d2(pos_infin);
date d3(not_a_date_time);
date d4(max_date_time);
date d5(min_date_time);

cout << d1 << d2 << d3 << d4 << d5 << endl;

try
{
    //date d1(1399,12,1);
    //date d2(10000,1,1);
    date d3(2017,2,29);
}
catch(std::exception& e)
{
    cout << e.what() << endl;
}

}

//
void case4()
{
date d(2017,6,1);
assert(d.year() == 2017);
assert(d.month() == 6);
assert(d.day() == 1);

date::ymd_type ymd =  d.year\_month\_day();
assert(ymd.year    == 2017);
assert(ymd.month   == 6);
assert(ymd.day     == 1);

cout << d.day\_of\_week() << endl;
cout << d.day\_of\_year() << endl;
assert(d.end\_of\_month() == date(2017,6,30));

cout << date(2015,1,10).week\_number() << endl;
cout << date(2016,1,10).week\_number()  << endl;
cout << date(2017,1,10).week\_number()  << endl;

assert(date(pos_infin).is\_infinity()  );
assert(date(pos_infin).is\_pos\_infinity() );
assert(date(neg_infin).is\_neg\_infinity() );
assert(date(not_a_date_time).is\_not\_a\_date() );
assert(date(not_a_date_time).is\_special() );
assert(!date(2017,5,31).is\_special() );

}

//
void case5()
{
date d(2017,1,23);

cout << to\_simple\_string(d) << endl;
cout << to\_iso\_string(d) << endl;
cout << to\_iso\_extended\_string(d) << endl;
cout << d << endl;

//cout << "input date:";
//cin >>d;
//cout << d;

}

//
void case6()
{
date d(2017,5,20);
tm t = to_tm(d);
assert(t.tm_hour == 0 && t.tm_min == 0);
assert(t.tm_year == 117 && t.tm_mday == 20);

date d2 = date\_from\_tm(t);
assert(d == d2);

}

//
void case7()
{
days dd1(10), dd2(-100), dd3(255);

assert( dd1 > dd2 && dd1 < dd3);
assert( dd1 + dd2 == days(-90));
assert((dd1 + dd3).days() == 265);
assert( dd3 / 5 == days(51));

weeks w(3);
assert(w.days() == 21);

months m(5);
years y(2);

months m2 = y + m;
assert(m2.number\_of\_months() == 29);
assert((y \* 2).number\_of\_years() == 4);

}

//
void case8()
{
date d1(2000,1,1),d2(2017,11,18);
cout << d2 - d1 << endl;
assert(d1 + (d2 - d1) == d2);

d1 += days(10);
assert(d1.day() == 11);
d1 += months(2);
assert(d1.month() == 3 && d1.day() == 11);
d1 -= weeks(1);
assert(d1.day() == 4);

d2 -= years(10);
assert(d2.year() == d1.year() + 7);

{
    date d1(2017,1,1);

    date d2 = d1 + days(pos_infin);
    assert(d2.is\_pos\_infinity());

    d2 = d1 + days(not_a_date_time);
    assert(d2.is\_not\_a\_date());
    d2 = date(neg_infin);
    days dd = d1 - d2;
    assert(dd.is\_special() && !dd.is\_negative());
}

{
    date d(2017,3,30);
    d -= months(1);
    d -= months(1);
    d += months(2);
    assert(d.day() == 31);
}

}

//
void case9()
{
date_period dp1(date(2017,1,1), days(20));
date_period dp2(date(2017,1,1), date(2016,1,1));
date_period dp3(date(2017,3,1), days(-20));

date_period dp(date(2017,1,1), days(20));

assert(!dp.is\_null());
assert(dp.begin().day() == 1);
assert(dp.last().day() == 20);
assert(dp.end().day() == 21);
assert(dp.length().days() == 20);

{
    date_period dp1(date(2017,1,1), days(20));
    date_period dp2(date(2017,2,19), days(10));

    cout << dp1;                        //[2010-Jan-01/2010-Jan-20]
    assert(dp1 < dp2);
}

}

//

int main()
{
case1();
case2();
case3();
case4();
case5();
case6();
case7();
case8();
case9();
}


##### 3.2date\_period


data\_time库使用date\_period来表示日期区间的概念,它是时间轴上的一个左闭右开的区间,其端点是两个date对象。日期区间的左边界必须小于右边界,否则date\_period将表示一个无效的日期区间。


date\_period的类摘要如下:



class date_period
{
public:
period(date, date);
period(date, days);

date begin() const;
date end() const;
date last() const;
days length() const;
bool is\_null() const;

bool operator==(const period &) const;
bool operator<(const period &) const;

void shift(const days &);
void expand(const days &);

bool contains(const date &) const;
bool contains(const period &) const;
bool intersects(const period &) const;
bool is\_adjacent(const period &) const;
bool is\_before(const date &) const;
bool is\_after(const date  &) const;
period intersection(const date &) const;
period merge(const period &) const;
period span(const period &) const;

};


用一段代码来测试一下



#include
using namespace std;

#include <boost/date_time/gregorian/gregorian.hpp>
using namespace boost::gregorian;

//
void case1()
{
date_period dp(date(2017,1,1), days(20));

dp.shift(days(3));
assert(dp.begin().day() == 4);
assert(dp.length().days() == 20);

dp.expand(days(3));
assert(dp.begin().day() == 1);
assert(dp.length().days() == 26);

}

//

void case2()
{
date_period dp(date(2010,1,1), days(20));

assert(dp.is\_after(date(2009,12,1)));
assert(dp.is\_before(date(2010,2,1)));
assert(dp.contains(date(2010,1,10)));

date_period dp2(date(2010,1,5), days(10));
assert(dp.contains(dp2));

assert(dp.intersects(dp2));
assert(dp.intersection(dp2) == dp2);

date_period dp3(date(2010,1,21), days(5));
assert(!dp3.intersects(dp2));
assert(dp3.intersection(dp2).is\_null());

assert(dp.is\_adjacent(dp3));
assert(!dp.intersects(dp3));

}

//
void case3()
{
date_period dp1(date(2010,1,1), days(20));
date_period dp2(date(2010,1,5), days(10));
date_period dp3(date(2010,2,1), days(5));
date_period dp4(date(2010,1,15), days(10));

assert( dp1.contains(dp2) && dp1.merge(dp2) == dp1);
assert(!dp1.intersects(dp3) && dp1.merge(dp3).is\_null());
assert( dp1.intersects(dp2) && dp1.merge(dp4).end() == dp4.end());
assert( dp1.span(dp3).end() == dp3.end());

}

//
void case4()
{
date d(2007,9,28);
day_iterator d_iter(d);

assert(d_iter == d);
++d_iter;
assert(d_iter == date(2007,9,29));

year_iterator y\_iter(\*d_iter, 10);
assert(y_iter == d + days(1));
++y_iter;
assert(y_iter->year() == 2017);

day_iterator iter(day_clock::local\_day());
++iter;

//iter += 5;
//std::advance(iter, 5);

}

//
void case5()
{
typedef gregorian_calendar gre_cal;
cout << “Y2017 is "
<< (gre_cal::is_leap_year(2017)?”“:“not”)
<< " a leap year.” << endl;
assert(gre_cal::end_of_month_day(2017, 2) == 28);
}

//
void case6()
{
date d(2017,1,23);

date d\_start(d.year(), d.month(), 1);
date d_end = d.end\_of\_month();

for(day_iterator d\_iter(d_start);
    d_iter <= d_end; ++d_iter)
{
        cout << \*d_iter << " " <<
                d_iter->day\_of\_week()<< endl;
}

}

//
void case7()
{
date d(2017,1,23);

date d18years = d + years(18);
cout << d18years << " is "
    << d18years.day\_of\_week()<< endl;

int count = 0;
for (day_iterator d\_iter(date(d18years.year(),1,1));
        d_iter <= d18years.end\_of\_month(); ++d_iter)
{
    if (d_iter->day\_of\_week() == Sunday)
    {
        ++count;
    }
}
cout << "total " << count << " Sundays." << endl;

count = 0;
for (month_iterator m\_iter(date(d18years.year(),1,1));
        m_iter < date(d18years.year() + 1 ,1, 1); ++m_iter)
{
    count += m_iter->end\_of\_month().day();
}

为了做好运维面试路上的助攻手,特整理了上百道 【运维技术栈面试题集锦】 ,让你面试不慌心不跳,高薪offer怀里抱!

这次整理的面试题,小到shell、MySQL,大到K8s等云原生技术栈,不仅适合运维新人入行面试需要,还适用于想提升进阶跳槽加薪的运维朋友。

本份面试集锦涵盖了

  • 174 道运维工程师面试题
  • 128道k8s面试题
  • 108道shell脚本面试题
  • 200道Linux面试题
  • 51道docker面试题
  • 35道Jenkis面试题
  • 78道MongoDB面试题
  • 17道ansible面试题
  • 60道dubbo面试题
  • 53道kafka面试
  • 18道mysql面试题
  • 40道nginx面试题
  • 77道redis面试题
  • 28道zookeeper

总计 1000+ 道面试题, 内容 又全含金量又高

  • 174道运维工程师面试题

1、什么是运维?

2、在工作中,运维人员经常需要跟运营人员打交道,请问运营人员是做什么工作的?

3、现在给你三百台服务器,你怎么对他们进行管理?

4、简述raid0 raid1raid5二种工作模式的工作原理及特点

5、LVS、Nginx、HAproxy有什么区别?工作中你怎么选择?

6、Squid、Varinsh和Nginx有什么区别,工作中你怎么选择?

7、Tomcat和Resin有什么区别,工作中你怎么选择?

8、什么是中间件?什么是jdk?

9、讲述一下Tomcat8005、8009、8080三个端口的含义?

10、什么叫CDN?

11、什么叫网站灰度发布?

12、简述DNS进行域名解析的过程?

13、RabbitMQ是什么东西?

14、讲一下Keepalived的工作原理?

15、讲述一下LVS三种模式的工作过程?

16、mysql的innodb如何定位锁问题,mysql如何减少主从复制延迟?

17、如何重置mysql root密码?

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以点击这里获取!

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

1、什么是运维?

2、在工作中,运维人员经常需要跟运营人员打交道,请问运营人员是做什么工作的?

3、现在给你三百台服务器,你怎么对他们进行管理?

4、简述raid0 raid1raid5二种工作模式的工作原理及特点

5、LVS、Nginx、HAproxy有什么区别?工作中你怎么选择?

6、Squid、Varinsh和Nginx有什么区别,工作中你怎么选择?

7、Tomcat和Resin有什么区别,工作中你怎么选择?

8、什么是中间件?什么是jdk?

9、讲述一下Tomcat8005、8009、8080三个端口的含义?

10、什么叫CDN?

11、什么叫网站灰度发布?

12、简述DNS进行域名解析的过程?

13、RabbitMQ是什么东西?

14、讲一下Keepalived的工作原理?

15、讲述一下LVS三种模式的工作过程?

16、mysql的innodb如何定位锁问题,mysql如何减少主从复制延迟?

17、如何重置mysql root密码?

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以点击这里获取!

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 14
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值