(i) The date and time of the boost library

Source: Internet
Author: User
Tags local time

(i) The date and time of the boost libraryFirst, the timer

Timers, it is often useful to count the execution time of a function in a project.

#include <boost/timer.hpp>
voidprintusertime() 
{
    Boost::timer T;  //Define a timing class to start timing
    std::" maximum measurable time:" << T.elapsed_max ()/"hstd::endl;     
    std::" maximum time measurable:"sstd::endl;    
    std::" use time:" <<  Std::endl;   
}

Second, get the current date

D (day_clock::local_day ()); 

Iii. converting a string into a date

Date fmtdt1 = from_string ("2013-04-21");  
Date FMTDT2 = date_from_iso_string ("20140320");  
Date FMTDT3 = from_us_string ("1-25-2003")  

Iv. converting dates to strings

To_iso_string
To_simple_string

Five, date length, in days units

Date_duration dur = End-beg;    
cout << dur.days () << Endl;

Vi. Date Range

Receive two time points to determine the intersection of the time interval, the set, the interval, the interval translation, and so on the operation of the interval

End);

Seven, date arithmetic

Provides days, months, years three time classes that can be mixed with date classes

m (1);
M

Viii. date, date and string conversions, date plus subtraction operations

#include <boost/date_time/gregorian/gregorian.hpp>
PrintDate()
{
    namespace Boost::gregorian;
    Std
    Get current time
    DateD(Day_clock::local_day ()); 
    The date class supports IO stream output
    cout << D << Endl;
    Convert date to string  20140320
    cout << to_iso_string (d) << Endl;
    Convert a string to a date
    Date fmtdt1 = from_string ("2013-04-21");  
    Date FMTDT2 = from_string ("2013/04/21");  
    "<< fmtdt2 << Endl;
    
    Day(1);
    M(1);
    Y(1);
    Date Beg = d + day-y;
    Date end = d + M;
    "Yes"No") << Endl;   
    Date Date = date_duration, and date +/-Date_duration is also supported 
    Date length
    Date_duration dur = End-beg;    
    cout << dur.days () << Endl;
    Date Range
    P(Beg, dur);
    cout << p << Endl;
}

Nine, time, time and string conversion, time plus subtraction operation

and date similar, also provide time length: time_duration, Time range: time_period, timely Operation class hours, minutes, seconds, milliseconds, microseconds

#include <boost/date_time/posix_time/posix_time.hpp>
Printtime()
{
    Namespace boost::p osix_time;
    Std
    namespace Boost::gregorian;
    Get local time
    T1(Second_clock::local_time ());
    Get UTC time
    T2(Second_clock::universal_time ());
    cout << T1 << T2 << Endl;
    Time 8:30
    Dur(8,30,0);
    H(1);
    M(1);
    s(1);
    Mi(1);   //MS 
    Mic(1);  //μs 
    Nanoseconds na (1);    Nanosecond   requires system support
    T3(date(2013,4,20)); 
    T4(date(2013,4,20), dur); 
    T5(date(2013,4,20), mic); 
    cout << to_simple_string (T3) <<<<  to_simple_string (T5) << Endl; 
    
    The operation of time, similar to the date, is very simple.
    cout << t2-t1 << Endl;
}

Ten, the format of time

Mainly used for two classes, input: Date_input_facet, Output: Date_facet
The time format class is: Time_input_facet time_facet boost::p osix_time::time_facet *timefmt = new boost::p Osix_time::time_facet ("%Y-% m-%d%h:%m:%s ");

#include <boost/date_time/gregorian/gregorian.hpp>
FormatDateTime()
{
    namespace Boost::gregorian;
    Std
    
    Input
    Date D;
    std::"140320";  
    New Boost::gregorian::d ate_input_facet ("%y%m%d   
    std::ss1(teststring); 
    Ss1.imbue (Locale (Ss1.getloc (), FMT));
    SS1 >> D;
    cout << D << Endl;
    Output
    Boost::gregorian::d ateD2  
    New Boost::gregorian::d ate_facet ("%A,%d%B%Y   
    std::cout.imbue (std::locale (std::    
    STD::
    Custom
    Std::locale::global (std::locale ("German    
    STD::String months[12] = {"Januar","Februar","März", "april "mai"  " Juni "juli  "august ", " september "oktober"  " November "dezember"};  
 std::string weekdays[7] = { "sonntag"  "montag"  " Dienstag "mittwoch", donnerstag", Span class= "hljs-string" > "freitag",  "samstag"};      
    Boost::gregorian::d ateD3  
    New Boost::gregorian::d ate_facet ("%A,%d.%B%Y   
    Df->long_month_names (std::vector<std::    
    Df->long_weekday_names (std::vector<std::    
    std::cout.imbue (std::locale (std::    
    STD::
}

(i) The date and time of the boost library

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.