[Boost] ptime class Introduction

Source: Internet
Author: User

1. Overview
Header file: # include <boost/date_time/posix_time/posix_time.hpp>
General namespace: Using namespace boost: posix_time;
2. Constructor
Like the date constructor, they are all basic constructor, string constructor, and clock constructor.
Ptime can also be constructed using conversion functions.

Void test_ptime_construct () {using namespace boost: Gregorian; using namespace boost: posix_time; // 1. the following are common constructor types: ptime P0; // The most common constructor ptime p1 (date (2012, 11, 30), time_duration (1, 2, 3 )); // 2012-nov-30 01: 02: 03 ptime P2 (P1); // copy and construct ptime PS1 (neg_infin); ptime PS2 (pos_infin); ptime PS3 (not_a_date_time ); ptime PS4 (max_date_time); // 9999-dec-31ptime ps5 (min_date_time); // 1400-Jan-01 // 2. construct ptime pstr1 (time_from_string ("23:59:59. 000 "); ptime pstr2 (from_iso_string (" 20121130t200001 "); // 3. construct ptime pC1 (second_clock: local_time () through clock; ptime PC2 (second_clock: universal_time (); ptime PC3 (microsec_clock: local_time ()); ptime pc4 (microsec_clock: universal_time (); // 4. use the from_time_t and ptime_from_tm conversion functions. // Note: After from_time_t is converted to ptime, it seems that the Greenwich Mean Time time_t T1 = Time (null); ptime Pc5 = from_time_t (T1 ); STD: cout <Pc5 <STD: Endl; TM * t2 = localtime (& T1); ptime PC6 = ptime_from_tm (* t2); STD :: cout <PC6 <STD: Endl ;}

3. Common functions
Returns the decimal part of the date, time, and second.

Void test_ptime_accessor () {using namespace boost: posix_time; using namespace boost: Gregorian; // obtain the date part ptime Pt (second_clock: local_time (); Date d1 = pt. date (); // obtain the time part time_duration TD = pt. time_of_day (); // obtain the fractional part of the second _ int64 L = TD. fractional_seconds (); STD: cout <L <STD: Endl ;}

4. convert to a string

void test_ptime2string(){using namespace boost::posix_time;time_duration td1(1, 2, 3, 4);std::cout << to_simple_string(td1) << std::endl;std::cout << to_iso_string(td1) << std::endl;}

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.