下面是看
boost 程序库完全开发指南时,date_time(chapter2)时所敲下的代码片断....,不保证其正确性,仅作为一个记录....
#include"stdafx.h" #include <vector> #include <string> #include <fstream> #include <windows.h> #include <boost/progress.hpp> #include <iostream> #include <algorithm> using namespace std; //using namespace std::max; //using namespace boost; //#pragma comment(lib, libboost_date_time-vc80-mt-gd.lib) //#pragma comment(lib, libboost_date_time-vc80-mt.lib) #define BOOST_DATE_TIME_SOUCE #include <boost/date_time/gregorian/gregorian.hpp> #include <boost/date_time/posix_time/posix_time.hpp> #include <boost/date_time/local_time/local_time.hpp> using namespace boost::gregorian; using namespace boost::posix_time; using namespace boost::local_time; #if 1 template<typename Clock = microsec_clock> class basic_ptimer { public: basic_ptimer() {restart();} void restart() {_start_time = Clock::local_time();} void elapsed() const {cout << Clock::local_time() - _start_time;} ~basic_ptimer(){elapsed();} private: ptime _start_time; }; //typedef basic_ptimer<microsec_clock> ptimer; typedef basic_ptimer<> ptimer; typedef basic_ptimer<second_clock> sptimer; #endif #if 0 class credit_card { public: string bank_name; int bill_day_no; credit_card(const char* bname, int no):bank_name(bname), bill_day_no(no){} int calc_free_days(date consume_day = day_clock::local_day()) const; friend bool operator<(const credit_card& l, const credit_card& r) { return l.calc_free_days() < r.calc_free_days(); } }; int credit_card::calc_free_days(date consume_day ) const { date bill_day(consume_day.year(), consume_day.month(), bill_day_no); if (consume_day > bill_day) { bill_day += months(1); } return (bill_day-consume_day).days() + 20; } #endif #if 0 class work_time { public: typedef map<time_period, string> map_t; private: map_t map_ts; void init() { ptime p(day_clock::local_day()); map_ts[time_period(p, hours(9))] = "It's too early, just relax. \n"; p += hours(9); map_ts[time_period(p, hours(13))] = "work so hard. \n"; p += hours(13); } public: work_time() { init(); } void greeting(const ptime& t) { map_t::iterator pos; for(pos = map_ts.begin(); pos != map_ts.end(); ++pos) { if (pos->first.contains(t)) { cout << pos->second << endl; break; } } } }; #endif int main() { tz_database tz_db; { ptimer pt; tz_db.load_from_file ("f:\\boost\\libs\\date_time\\data\\date_time_zonespec.csv"); //tz_db.load_from_file("./date_time_zonespec.csv"); } cout << endl; time_zone_ptr shz = tz_db.time_zone_from_region("Asia/Shanghai"); time_zone_ptr nyz = tz_db.time_zone_from_region("America/New_York"); cout << shz->has_dst() << endl; cout << shz->std_zone_name() <<endl; local_date_time dt_bj(date(2008,1,7), hours(12), shz, false); cout <<dt_bj << endl; time_duration flight_time = hours(15); dt_bj += flight_time; cout << " after flight " << dt_bj << endl; local_date_time dt_ny = dt_bj.local_time_in(nyz); cout << " convert to NYTime is " << dt_ny << endl; #if 0 date d(2012, 1,2); date_facet* dfacet = new date_facet("%Y年%m月%d日"); cout.imbue(locale(cout.getloc(), dfacet)); cout << d << endl; date d2(day_clock::local_day()); cout << d2 <<endl; #endif #if 0 work_time wt; wt.greeting(second_clock::local_time()); #endif #if 0 { ptimer pt; Sleep(500); } #endif #if 0 time_duration td(1,30,34,35); time_duration td2 = duration_from_string("1:30:44:33"); cout << to_simple_string(td2) <<endl; cout << to_iso_string(td2) <<endl; #endif #if 0 ptime p = from_time_t(std::time(0)); assert(p.date() == day_clock::local_day()); cout << p << endl; #endif #if 0 ptime p(date(2010,1,1), hours(12)); time_period tp1(p, hours(8)); time_period tp2(p + hours(8), hours(1)); assert(tp1.end() == tp2.begin() && tp1.is_adjacent(tp2)); assert(!tp1.intersects(tp2)); tp1.shift(hours(1)); assert(tp1.is_after(p)); assert(tp1.intersects(tp2)); tp2.expand(hours(10)); assert(tp2.contains(p) && tp2.contains(tp1)); #endif #if 0 ptime p = second_clock::local_time(); for (time_iterator t_iter(p, minutes(10)); t_iter < p + hours(2); ++t_iter) { cout << *t_iter << endl; } #endif #if 0 ptime p1 = time_from_string("2013-3-24 20:53"); cout << p1 << endl; ptime p2 = from_iso_string("20130303T0345591234"); cout << p2 << endl; ptime p3 = second_clock::local_time(); //output in seconds cout << p3 << endl; // 2013-Mar-24 20:57:32 ptime p4 = microsec_clock::universal_time(); //in microseconds cout << p4 << endl; //2013-Mar-24 12:57:32.754747 #endif #if 0 cout <<max(1,3) << endl; credit_card c1("CMB", 5); credit_card c2("GF", 18); credit_card tmp = std::max (c1, c2); //credit_card tmp = (c1 < c2) ? c2: c1; cout << "you should use " << tmp.bank_name << ", free days = " << tmp.calc_free_days() << endl; #endif #if 0 //next define no use to avoid the invalid date construct //#define DATE_TIME_NO_DEFAULT_CONSTRUCTOR //date d1(2011,1,1); date d1; assert(d1 == date(not_a_date_time)); date d2(2012,2,2); date d3(d2); assert(d2 == d3); date d4(2013, Jul, 1); assert(d2 < d4); date d5(from_string("2012/1/8")); date d6(from_undelimited_string("20120109")); assert(d5 < d6); #endif #if 0 date d1 (neg_infin); date d2(pos_infin); date d3(not_special); date d4(max_date_time); date d5(min_date_time); cout << "d1 " << d1 <<endl; cout << "d2 " << d2 << endl; cout << "d3 " << d3 <<endl; cout << "d4 " << d4 << endl; cout << "d5 " << d5 <<endl; #endif #if 0 date d1 (10000, 1,2); date d2(2012,2,30); cout << date(2010, 1,1).week_number() << endl; date d1(2013,3,4); cout << to_simple_string(d1) << endl; cout << to_iso_string(d1) << endl; cout << to_iso_extended_string(d1) << endl; cout << d1 <<endl; #endif #if 0 //days d; date d1(from_undelimited_string("19851228")); date d2(2013,03,24); //cout << d2 << " - " << d1 << d2 - d1 << endl; //days day= d2 - d1; //cout << day.date_duration() << endl; cout << d2 - d1 << endl; date d3(1983,3,13); cout << d2 - d3 << endl; cout << d3 - d1 << endl; date d (2010,3,30); cout << to_iso_string(d) << endl; d -= months(1); cout << to_iso_string(d) << endl; d -= months(1); cout << to_iso_string(d) << endl; d += months(2); cout << to_iso_string(d) << endl; #endif #if 0 date_period dp1(date(2013,3,24), days(-100)); cout << dp1 << endl; #endif #if 0 //date d(2013, 3,24); date d = day_clock::local_day(); date d_start(d.year(), d.month(), 1); date d_end = d.end_of_month(); for (int i = 0; i < 7; ++i) { cout << "\t" << i ; } for (int i = 0; i < d_start.day_of_week()) for(day_iterator d_iter(d_start); d_iter != d_end; ++ d_iter) { cout << *d_iter << " " << d_iter->day_of_week() << endl; } #endif //calc the credit card #if 0 progress_display pd(10); Sleep(500); pd += 3; Sleep(500); cout <<"XXX"<<endl; pd.restart(10); pd += 5; Sleep(500); cout <<"XXX"<<endl; pd.restart(10); pd +=2; Sleep(500); pd.restart(10); pd += 13; #endif #if 0 vector<string> v(100); ofstream fs("d:\\test.txt"); progress_display pd(v.size()); vector<string>::iterator pos; int i = 0; for( pos = v.begin(); pos != v.end(); ++pos , ++i) { fs << *pos << endl; if ( i % 20 == 0 ) { Sleep(500); } ++pd; } #endif return 0; } #if 0 // date_time.cpp : Defines the entry point for the console application. // //#include "stdafx.h" //int _tmain(int argc, _TCHAR* argv[]) #include <Windows.h> #include <iostream> #if 0 #include<boost/timer.hpp> #include <boost/progress.hpp> #include <boost/assert.hpp> #endif #define BOOST_TEST_MAIN 0 #include <boost/test/included/unit_test.hpp> //#include <boost/test/minimal.hpp> //#include <boost/format.hpp> #define BOOST_TEST_INCLUDED #include <boost/test/unit_test_suite.hpp> //#include <boost/smart_ptr.hpp> using namespace std; using namespace boost; int my_add(int a, int b) { return a + b; } #if 0 double func(int x) { BOOST_ASSERT(x != 0 && "divide by zero"); int n = 10/x; return 0; } #endif BOOST_AUTO_TEST_SUITE(test1) BOOST_AUTO_TEST_CASE(t_test2) { BOOST_CHECK_EQUAL(my_add(2,3), 5); #if 0 scoped_ptr<int> p(new int(874)); BOOST_CHECK(p); //BOOST_CHECK_EQUAL(*p, 874); p.reset(); BOOST_CHECK(p == 0); #endif } BOOST_AUTO_TEST_SUITE_END() #if 0 int test_main(int argc, char* argv[]) { format fmt("%d-%d"); BOOST_CHECK(fmt.size() != 0); fmt % 12 % 34; BOOST_REQUIRE(fmt.str() == "12-34"); BOOST_ERROR("test is an error"); return 0; } #endif #if 0 int main() { func(0); timer t; Sleep(1000); cout << "max " << t.elapsed_max() / 3600 << "h" << endl; cout << "min " << t.elapsed_min() / 3600 << "h" << endl; cout << "elapse " << t.elapsed() << "s" << endl; { progress_timer t; Sleep(1056); } return 0; } #endif #endif