Boost library for time and date processing Date_timer Library

Source: Internet
Author: User
Tags filetime time and date

/*boost provides a timer and a data_time library for time and date processing * *
The Timer library provides easy time-to-measure and progress-display features that can be used for tasks such as performance testing that require timing
/*
Timer of the three component timers classes timer Progress_timer and progress indicator class Progress_display
Timer can measure the passage of time, providing millisecond-level timing accuracy


#include <boost\timer.hpp>
#include <iostream>
using namespace boost;
using namespace Std;
int main ()
{
Timer t;//Start Timing
cout << Max TimeSpan: << T.elapsed_max ()/3600 << "h" << endl;//maximum time range for measuring time
cout << min TimeSpan << t.elapsed_min () << "s" << endl;//min. accuracy
cout << "Now time elapsed" << t.elapsed () << "s" << endl;//has elapsed
}*/


/*
Usage recommendations: Not suitable for high-precision timing, window under the accuracy of 1/1000,linux 1/1000000 difficult to achieve cross-platform
*/
/*--------------------Progress_timer-------------------
Inherit from timer will automatically output time when destructor must include header file <>
The I/O stream can be specified in the constructor so that it is written to the I/O stream at the time of the destructor
Extended timing Accuracy:
Using template technology to imitate Progress_timer write new class New_progress_timer achieve arbitrary precision output
Likewise inherits from the timer class just becomes the template class n specifies the precision default to 2




#include <boost\progress.hpp>
#include <boost\static_assert.hpp>
Template<int n=2>
Class New_progress_timer:p ublic boost::timer
{
Public
The constructor initializes the output stream m_os in the window platform n>3 meaningless/
New_progress_timer (std::ostream& os = std::cout): M_os (OS)
{
Boost_static_assert (n >= 0 && n <= 10);
}
~new_progress_timer ()
{
try{
Save the state of a stream
Std::istream::fmtflags old_flags =
M_OS.SETF (std::istream::fixed, Std::istream::floatfield);
Std::streamsize Old_prec = m_os.precision (N);
M_os << Elapsed () << "s\n" <<
Std::endl;
Status of the recovery stream
M_os.flags (Old_flags);
M_os.precision (OLD_PREC);


}
catch (...) {}//destructors must never throw exceptions
}
Private
std::ostream& M_os;
};
Template<>
Class new_progress_timer<2>:p ublic boost::p rogress_timer//of templates for n=2
{


};
int main ()
{
//{
Boost::p Rogress_timer t;
Std::cout << t.elapsed () << Std::endl;
Do something;
//}
Use of templates
New_progress_timer<10> T;
Do someting;
}
*/
----------------------Progress_display
/*
You can display the execution time of the program on the console so she was able to provide a friendly user interface
is a standalone class and the other two component classes okay
Its constructor accepts a long parameter expected_count the cardinality used for the progress display
Another constructor Progress_display (unsigned long expected_count,std::ostream&os,const std::string&s1= "\ n",
Const std::string& s2= "", Const std::string & s3= ""); three string parameter definitions display a string of three headers
!! Precautions:
The display output of the progress cannot be separated from the output of the program this is because Progress_display and all C + + programs output characters to standard output (cout) if the program that uses Progress_display also has output operation that progress bar will
It's a mess. A possible but far more perfect way to do this is to call restart () to re-display progress and then use operator+= to specify the current progress.


#include <boost\progress.hpp>
#include <string>
#include <fstream>
#include <vector>
using namespace Std;
int main ()
{
Vector<string>v ("a");
Ofstream FS ("G:\\test.txt");
Boost::p rogress_display PD (V.size (), std::cout, "\ n", "", "*");//After three parameters provide default parameters
Vector<string>::iterator ITPOs;
for (ITPOs = V.begin (); ITPOs! = V.end (); ++itpos)
{
FS << *itpos << Endl;
++PD;
_sleep (100);
}
}
*/
An overview of the---------------------------------date_time Library


/* Very comprehensive date-time library
The Date_time Library uses enumeration special_values to define special time concepts in the namespace:
Pos_infin---means positive infinity
Neg_infin------means negative infinity
Not_a_date_time-------Invalid Time
Min_data_time------represents the minimum date 1400-01-01
Max_data_time--------Maximum Date 9999-12-31
Date of processing:
Date is the core class of Date_time library processing uses 32-bit integers as internal storage, can copy values, compare operations, stream input and output
Use:
Date d1;///an invalid Date object
Date D2 (2010,1,1) Number construction dates
Date D3 (2000,jan,1)//English specified month
Date d4=from_string ("1999-12-31")
Date D4 (from_string (2005/1/1))//Support copy Construction Engineering function pass/or-split
Date d3=from_undelimted_string ("20011112")//plain string with no delimiters
Day_lock is a non-level clock He is a project class calling his static member function Local_day () or Universal_day () will return the current day date objects are local time and UTC date respectively
The 5 is_xxx () function of date is used to detect whether a date is a special date
Is_infinity () is an unlimited date
Is_neg_infinity () is a negative infinite date
Is_pos_infinity () .... Positive Unlimited Date
Is_not_a_date ... Invalid Date
Is_special () ... Special Dates
Date is conveniently converted into a string to provide three free functions
To_simple_string (date D) converted to Yyy-mmm-dd mmm three-character English month
To_iso_string (Date D) YYYMMDD
To_iso_extended_string (date D) converted to YYY-MM-DD


----------------------conversion to the TM structure:
TO_TM (date)
DATE_FROM_TM:TM Conversion to date
-----------------------Date Length
Date length is the length of time in days, which is a scalar of the duration of the measurement
The basic date-length class is date_duration support for full-order comparison operations (= = = > >=) also supports the increment and decrement operations and also supports division operations but cannot be in addition to the Date_duration type
Multiplication take-over modulus is not supported
The Date_time Library defines a common typedef:day for Date_duration, which describes the measurement of a number of days
To facilitate the calculation of the length of time the Date_time Library also provides additional three time classes such as months years weeks
*/
---------------------Date Arithmetic
/*
Date is mainly associated with time-length concepts




#define Boost_date_time_source
#include <boost\date_time\gregorian\gregorian.hpp>
using namespace Boost::gregorian;
int main ()
{
Date D1 (2000, 1, 1);
Date D2 (2008, 8, 8);
Std::cout << d2-d1 << Std::endl;
D1 + + days (10);
D1 + = months (2);
D1-= weeks (1);
D2-= years (2);
}
*/
-----------------------date intervals and their operations
/*
Date_time Library The concept of using the Date_period class to represent a date range is a two date object on the timeline with a left-closed, right-open interval endpoint
Structure:
Date_period PD1 (Date (2010,1,1), Days (20));
The member function shift () and function expand () can vary the interval: shift () shifts the date interval by n days and the length is constant expend () extends the date to both ends by N days
*/
----------------------Date iterators
/*
Date_iterator,week_iterator,month_iterator, and Year_iterator, respectively, increment and decrement by day Zhou units
Date d (2006,1,1);
Day_iterator D_iter (d);//The default increment and decrement step is 1;
++diter;
Year_iterator Y_iter (*d_iter,3); increment to 3 years


*/
--------------------------Other Features
/*
Class Boost::gregorian::gregorian_calendar provides some of the operation functions of the Gregory. Basically, he was used internally by the date class, and several of his static functions
Is_leap_year () can determine if it is a leap year end_of_month_day (), returning the last day of the month
*/
------------------------------------------------------processing Time
/*
The Date_time Library provides a microsecond-level time system on the basis of the Gregorian calendar, which can be up to nanosecond accuracy if needed
The time function of the Date_time library is located in the namespace boos::p osix_time
Mode of use: #include <boost/date_time/posix_time/posix_time.hpp>
Using namespace boost::p osix_time
----------------------------------------------Length of time
Date_time Library uses time_duration to measure the length of time exactly to microseconds, if a macro is defined before header file <boost/date_time/posix_time/posix_time.hpp>
The boost_date_time_std_config can be accurate to the nanosecond level
Time_duration also has several sub-classes that measure different time resolutions such as hour ....... such as
Length of---------------------------------------------operating time
Time_duration can be constructed when the constructor is specified in seconds, minutes and microseconds.
Time_duration TD (1,20,30,1000);
Use time_duration subclasses to create a more intuitive length of time
Hour h (1);
Minutes m (10);
Time_duration td=h+m;
Using the factory function duration_from_string can be created from a string
Time_duration td=duration_from_string ("1:10:30:001");
If you want to get a Time_duration object string representation You can use the free function to_simple_string (time_duration)
*/
--------------------------------------------Time-Length accuracy
/*
The accuracy of the default time of the Date_time library is microsecond nanosecond-related classes and functions such as NANOSEC and member functions nanoseconds (), total_nanoseconds () are not available, and the measure of time in seconds uses microseconds
When macro boost_date_time_posix_std-config is used, some of the behavior of Time_duration will change time resolution will reach nanoseconds, and the time of the constructor is less than seconds the lone wolf is in microseconds.
boost_date_time_posix_std-config//defining nanosecond-level macros
#define Boost_date_time_source
#include <boost\date_time\posix_time/posix_time.hpp>
Time_duration TD (1,10,30,1000);//1000 nanoseconds
cout<<td;
ASSERT (Td.total_milliseconds () ==td.total_seconds () *1000);
Time_duration provides static member functions resolution () and num_fractional_digits () to detect current accuracy
----------------------------------------------Point in time
Ptime is the core class of Date_time library processing time data with 64-bit or 96-bit integer internal storage time
--------------------------------------Create a point-in-time object
using namespace Boost::gregorian;
Ptime p (Date (2010,3,5), hours (1));
Ptime p1=time_from_string ("2010-3-5 01:00:00")//using the factory function to create
Ptime p2=from_iso_string ("20100305t010000")
The ptime has a clock class that can generate the current time from the clock, with a different resolution of two classes Second_time () and Microsec_clock respectively to provide second-and microsecond-resolution
Local_time get local time univeral_time () get UTC Current Time
Ptime p1=second_clock::local_time ();//sec-level accuracy microsec_clock::universal_time ()//microsecond accuracy
Ptime using Date () and Time_of_day () two member functions to get the date and time length in a point in time
Time provides three free functions to convert to a string
To_simple_string (ptime): YYYY-MMM-DD hh:mm:ss.fffffff format
To_iso_string (ptime): Convert to YYYYMMDDTHHMMSS,FFFFF format
To_sio_extended_string (ptime): yyyy-mm-ddthh:mm:ss,ffffff
Transformation of structures such as----------------tm,time_t
To_tm (), ptime can be converted to a TM structure in one direction, and the conversion rules are: a combination of date and time_duration
Ptime p (Date (2010,2,14), Hours (20));
TM T=to_tm (p);
!!! There is no function to convert from TM to Ptime
If you want to convert to a Date object (Date_from_tm () function) Get the Date object and then manually manipulate the TM structure to get the Time_duration object, and then create the Ptime
from_time_t (time_t) and from_ftime<ptime> (FILETIME), they can create time_t objects from FILETIME and ptime structures, and this conversion is one-way
----------------time interval
corresponding to the date interval date_period, the Date_time library also has the concept of time interval, using class: Time_period Its usage is basically the same as Date_period
---------------Time iterators
Unlike a date iterator, the time iterator has only one time_iterator. When it is constructed, it passes in a starting point Ptime object and a not long time_duration object
Time_iterator can also be datetime to direct comparison with Ptime No need to use the dereference operator
--------------format Time
Date_time Library The default datetime format is simple and English, but not immutable Date_time library provides specialized formatting objects Date_facet,time_facet and so on with IO flow customization
Date and time representation
------------------local time
The Date_time library addresses local time using classes such as Time_zone_base,posix_time_zone,custom_time_zone,local_date_time and a text-formatted time zone database.
Serialization of-------------------
The ability of the Date_time library to use the Boost.serialization library is a letter. Data serialization stores datetime data in a file, and then reads the restore at any time



Boost library for time and date processing Date_timer 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.