C + + boost time and date processing detailed introduction _c language

Source: Internet
Author: User
Tags assert locale time and date time interval

Boost Time and date processing

Guided view:

Class
Characteristics
Disadvantages
Description
Timer
Timer base class
Not suitable for large span time
Suitable for most ordinary timings
Progress_timer
Inherited from timer can be automatically written to the stream
Accurate to 0.01s only
If you need more precision, you can derive a class that invokes the precision setting of the stream
Progress_display Graphical display of progress Output to cout only If there are other outputs, it will interfere with the progress display.
The tradeoff is to show pd.restart (size) again; pd+= Pnum;
Date Date structure, point in time —— Date is the core class of the Date_time library Boost::gregorian
Date_duration Days, months, years time periods —— For some time, you can think of it as an int
Date_period Scalar, left open, right, time range —— Can be considered as a starting point of the date_duration. Can do intersection, and set
Date_iterator An iterator that increases or decreases in a unit —— Four iterators of days, weeks, months, and years, moving in some increments.
Time_duration Time period with Date_duration —— Hours, minutes, seconds, millisec, boost::p osix_time
Ptime Time Point Date+time_duration —— The date () and the Time_of_day () operation are divided.
Time_period Time interval with Date_period —— ——
Time_iterator An iterator that increases or decreases in a unit —— can be directly compared with ptime
Date_facet Stream Format Date —— %y year%m Month%d days
time_facet stream format time -- %y year%m month%d day%h point%m%s%f seconds
#include <boost/timer.hpp> #include <boost/progress.hpp> #include <iostream> #include &LT;SSTREAM&G 
T #include <fstream> #include <string> #include <vector> #include <Windows.h> #include <boo st/date_time/gregorian/gregorian.hpp> #include <boost/date_time/posix_time/posix_time.hpp> using 
 
namespace Std; 
  int main () {Boost::timer T; 
  std::cout<< "Max" <<t.elapsed_max () <<endl; 
  std::cout<< "Min" <<t.elapsed_min () <<endl; 
  std::cout<< "Elapsed:" <<t.elapsed () <<endl; 
  T.restart (); 
  Sleep (100);  
  std::cout<< "Elapsed:" <<t.elapsed () <<endl; 
  cout<< "---------------------------" <<endl; 
  StringStream SS;  
    {boost::p Rogress_timer t (ss); 
  Sleep (300); 
  } cout<<ss.str (); 
 
  cout<< "---------------------------" <<endl; 
  Vector<string> V (100); Do Data Fill ... ofstream fs ("C:\Test.TXT "); 
  Boost::p rogress_display PD (V.size ()); 
  Vector<string>::iterator POS; 
    for (pos = v.begin ();p os!= v.end (); ++pos) {fs<<*pos<<endl; 
    Sleep (10); 
    ++PD; 
    Pd.restart (V.size ()); 
  pd+= (Pos-v.begin () + 1); 
   
  } cout<< "---------------------------" <<endl; 
    {using namespace Boost::gregorian; 
    cout<< "-----------------Date------------------" <<endl; 
    Date D1; 
    Date D2 (2013,4,7); 
    Date D3 (2013,apr,7); 
 
    Date D4 (D2); assert (D1 = = Date (not_a_date_time)); 
    The default is initialized to an invalid date assert (D2 = = D4); 
     
    ASSERT (D3 = = D2); 
    D1 = from_string ("1999,9,9"); 
    Date d5 (from_string ("2008/8/8")); 
     
    D3 = from_undelimited_string ("20110111"); 
    Cout<<day_clock::local_day () <<endl; 
 
    Cout<<day_clock::universal_day () <<endl; 
    Date d6 (Neg_infin); 
    Date D7 (Pos_infin); 
    cout<<d6<<endl; 
 
    cout<<d7<<endl;cout<< "---------------------------" <<endl; 
    Date today (2013,4,17); 
    ASSERT (today.year () = = 2013); 
    ASSERT (today.month () = = 4); 
 
    ASSERT (today.day () = = 17); 
    Date::ymd_type ymd = Today.year_month_day (); 
    ASSERT (Ymd.year = 2013); 
    ASSERT (Ymd.month = 4); 
 
    ASSERT (Ymd.day = 17); ASSERT (Today.day_of_week () = = 3);  Weeks Sunday for 0 cout<<today.day_of_year () <<endl;  In the year is the first days assert (today.end_of_month () = = Date (2013,4,30));  The last day of the Month Cout<<today.week_number () <<endl;      The first few weeks of the year, the half week of the beginning of 0-53 years, is the last year, that is, d6.is_infinity (); 
    Date is an infinite date assert (D6.is_neg_infinity ()); 
 
    cout<< "---------------------------" <<endl; 
    Cout<<to_simple_string (today) <<endl;     
    Cout<<to_iso_string (today) <<endl; Cout<<to_iso_extended_string (today) <<endl; 
   
    Common date format Yyyy-mm-dd cout<<today<<endl; cout<< "---------------------------"<<endl;   
    TM T = To_tm (today); 
     
    ASSERT (T.tm_hour = = 0 && t.tm_min = 0);  Date New_today = Date_from_tm (t); 
 
    Convert from TM to date assert (New_today = today); 
    cout<< "--------------days (date_duration)--------------" <<endl; 
    Days dd1, DD2 ( -20), DD3 (365); 
    ASSERT (Dd1>dd2 &&dd1<dd3); 
    ASSERT (Dd1+dd2 = = Days (-10)); 
    ASSERT ((DD2+DD3). Days () = 345); 
 
    ASSERT (DD3/5 = = Days (73));   Weeks W (3); 
     
    3 Week 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); 
    cout<< "--------------Calc--------------" <<endl; 
    Date DA (2000,1,1), DB (2008,8,8);   cout<<db-da<<endl; 
    3,142 days Da+=days (10); 
    ASSERT (da.day () = = 11); 
    Da+=months (2); 
 
    ASSERT (Da.month () ==3 && da.day () = = 11); 
    Da-=weeks (1); 
 
ASSERT (da.day () = = 4);    Db-=years (7); 
 
    ASSERT (da.year () = = Db.year ()-1); 
    If the date is the last day of the month, add or subtract the month or the annual meeting to get the month end time, instead of the simple month, year plus 1 date sp (2013,3,30); 
    Sp-=months (1); 
    ASSERT (sp.month () = = 2 && sp.day () = = 28); 
    SP-=months (1); 
    ASSERT (sp.month () = = 1 && sp.day () = = 31); 
    Sp+=months (2); ASSERT (sp.day () = = 31); 
 
    And the original date is not equal! 
    cout<< "--------------date_period--------------" <<endl;  Date_period DP (Date (2013,4,17), Days (14)); 
    Left open right closed with STL container similar assert (!dp.is_null ()); 
    ASSERT (Dp.begin (). Day () = 17); 
    ASSERT (Dp.last (). Day () = 30); 
 
    ASSERT (Dp.end (). Day () = 1); 
 
    cout<<dp<<endl; 
    Date_period NEW_DP = DP;   New_dp.shift (Days (3)); 
    Move the time interval back to assert (New_dp.begin (). Day () = 20); 
 
     
    ASSERT (New_dp.length (). Days () = 14);   New_dp.expand (Days (3)); 
    The interval two sections extend n days, namely lengthen 2n days. 
    ASSERT (New_dp.begin (). Day () = 17); 
 
    ASSERT (New_dp.length (). Days () = 20); 
ASSERT (Dp.is_after date (2013,1,1));    ASSERT (dp.contains date (2013,4,20)); 
    Date_period DP2 (Date (2013,4,17), Days (5)); 
     
    ASSERT (Dp.contains (DP2));   ASSERT (Dp.intersects (DP2)); 
     
    Intersection assert (dp.intersection (DP2) = = DP2); 
    Date_period dp3 (Date (2013,5,1), Days (5)); 
    ASSERT (!dp3.intersects (DP)); 
 
    ASSERT (Dp3.intersection (DP2). Is_null ()); 
     
    ASSERT (Dp.is_adjacent (DP3)); Date_period dp4 (Date (2013,4,17), Days (19));  and set assert (Dp.merge (DP3). Is_null ());    No intersection return NULL assert (Dp.span (dp3) = = DP4); 
    Fill in the middle area cout<< "--------------date_iterator--------------" <<endl; 
 
    Date last (2013,4,17); Day_iterator D_iter (last); 
    Date iterator assert (D_iter = last); 
    ++d_iter; 
 
    ASSERT (D_iter = = Date (2013,4,18));  Year_iterator Y_iter (*d_iter,3); 
 
    Increase or decrease the step to 3 assert (Y_iter = last + days (1)); 
    ++y_iter; 
   
    ASSERT (y_iter->year () = = 2016); 
    cout<< "--------------func--------------" <<endl; Cout<< (Gregorian_calendar::is_leap_year (2000)?  "Yes": "No") <<endl;   Leap year assert (Gregorian_calendar::end_of_month_day (2013,2) = = 28); 
    Month-end day {using namespace boost::p osix_time; 
    cout<< "--------------time_duration--------------" <<endl;  Time_duration TD (1,1,1); 
    Time, minutes, seconds will automatically borrow, carry hours H0 (1); 
    Minutes m (1); 
    Seconds s (1); 
     
    Millisec MS (1); 
    Time_duration TD2 = h0+m+s+ms; 
    Time_duration td3 = hours (2) + minutes (10); 
 
    Time_duration td4 = duration_from_string ("1:10:10:300");  
    ASSERT (td4.hours () = = 1 && td4.minutes () = = && Td4.seconds () = 10); ASSERT (td.total_seconds () = = 1*3600 + 1*60 + 1); 
    Convert to SEC hours h (-10); 
 
    ASSERT (H.is_negative ()); Time_duration H2 = H.invert_sign (); 
 
    Take the counter assert (!h2.is_negative () && h2.hours () = = 10); 
    cout<<td3-td2<<endl; 
    Cout<<to_simple_string (TD4) <<endl; Cout<<to_iso_string (tD4) <<endl; 
    cout<< "--------------ptime--------------" <<endl; 
      {using namespace Boost::gregorian; Ptime p (Date (2013,4,17), hours (1)); 
      Ptime equivalent to date+time_duration ptime p1 = time_from_string ("2013-4-17 16:25:00"); 
      cout<<p<<endl; 
      cout<<p1<<endl;     Ptime P2 = second_clock::local_time ();  Commonly used time output ptime p3 = Microsec_clock::universal_time (); 
 
      Microsecond Precision cout<<p2<<endl<<p3<<endl; 
 
      Ptime op (Date (2013,4,17), hours (1) +minutes (30)); 
      Date d = op.date (); 
      Time_duration optd = Op.time_of_day (); 
      ASSERT (D.day () = && d.month () = = 4); 
      ASSERT (optd.hours () = = 1 && optd.minutes () = = 30); 
 
      Cout<<to_iso_extended_string (OP) <<endl;  TM T = To_tm (OP); 
       
      Irreversible, this is different from date//can only be used Date_from_tm first get the date, and then fill the time. cout<< "--------------time_period--------------" <<endl; 
      Time_period TP1 (Op,hours (8)); 
      Time_period TP2 (Op+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 (OP)); 
 
      ASSERT (Tp1.intersects (TP2)); 
      Tp2.expand (Hours (10)); 
 
      Assert (Tp2.contains (OP) && tp2.contains (TP1)); 
      cout<< "--------------time_iterator--------------" <<endl; For (Time_iterator t_iter (op,minutes); t_iter<op+hours (1); ++t_iter) {Cout<<*t_iter<<end 
      L 
      cout<< "--------------formate--------------" <<endl; 
      date_facet* Dfacet = new Date_facet ("%Y year%m Month%d days"); 
      Cout.imbue (Locale (Cout.getloc (), Dfacet)); 
 
      Cout<<date (2013,4,17) <<endl; 
      time_facet* Tfacet = new Time_facet ("%Y year%m month%d day%h point%m minute%s%f seconds"); 
      Cout.imbue (Locale (Cout.getloc (), Tfacet)); 
   cout<<op<<endl; } getchar (); 
return 0;  }
Run Result:
Max 2.14748e+006 Min 0.001 elapsed:0.001 elapsed:0.1---------------------------0.30 s------------------------- 
-0% 100% |----|----|----|----|----|----|----|----|----|----| --------------------------------------------Date------------ 
------2013-APR-17 2013-apr-17-infinity +infinity------------------------------------------------------ 2013-APR-17 20130417 2013-04-17 2013-apr-17-----------------------------------------days (date_duration)--------- -------------------Calc--------------3142--------------date_period--------------[2013-apr-17/2013-apr-30]--- -----------date_iterator----------------------------func--------------Yes--------------time_duration---------- ----01:08:58.999000 01:10:10.300000 011010.300000--------------ptime--------------2013-apr-17 01:00:00 2013-apr -17 16:25:00 2013-apr-17 17:19:21 2013-apr-17 09:19:21.870604 2013-04-17t01:30:00--------------time_period----------------------------time_iterator--------------2013-a PR-17 01:30:00 2013-apr-17 01:40:00 2013-apr-17 01:50:00 2013-apr-17 02:00:00 2013-apr-17 02:10:00 2013-Apr-17 02:20:  --------------formate--------------April 17, 2013 April 17, 2013 01:30 00 seconds

Thank you for reading, I hope to help you, thank you for your support for this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.