io_tutorial.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include <iostream>
  2. #include <boost/date_time/local_time/local_time.hpp>
  3. int main(){
  4. using namespace boost::gregorian;
  5. using namespace boost::posix_time;
  6. using namespace boost::local_time;
  7. using namespace std;
  8. /****** basic use ******/
  9. date d(2004, Feb, 29);
  10. time_duration td(12,34,56,789);
  11. stringstream ss;
  12. ss << d << ' ' << td;
  13. ptime pt(not_a_date_time);
  14. cout << pt << endl; // "not-a-date-time"
  15. ss >> pt;
  16. cout << pt << endl; // "2004-Feb-29 12:34:56.000789"
  17. ss.str("");
  18. ss << pt << " EDT-05EDT,M4.1.0,M10.5.0";
  19. local_date_time ldt(not_a_date_time);
  20. ss >> ldt;
  21. cout << ldt << endl; // " 2004-Feb-29 12:34:56.000789 EDT"
  22. /****** format strings ******/
  23. local_time_facet* output_facet = new local_time_facet();
  24. local_time_input_facet* input_facet = new local_time_input_facet();
  25. ss.imbue(locale(locale::classic(), output_facet));
  26. ss.imbue(locale(ss.getloc(), input_facet));
  27. output_facet->format("%a %b %d, %H:%M %z");
  28. ss.str("");
  29. ss << ldt;
  30. cout << ss.str() << endl; // "Sun Feb 29, 12:34 EDT"
  31. output_facet->format(local_time_facet::iso_time_format_specifier);
  32. ss.str("");
  33. ss << ldt;
  34. cout << ss.str() << endl; // "20040229T123456.000789-0500"
  35. output_facet->format(local_time_facet::iso_time_format_extended_specifier);
  36. ss.str("");
  37. ss << ldt;
  38. cout << ss.str() << endl; // "2004-02-29 12:34:56.000789-05:00"
  39. // extra words in format
  40. string my_format("The extended ordinal time %Y-%jT%H:%M can also be represented as %A %B %d, %Y");
  41. output_facet->format(my_format.c_str());
  42. input_facet->format(my_format.c_str());
  43. ss.str("");
  44. ss << ldt;
  45. cout << ss.str() << endl;
  46. // matching extra words in input
  47. ss.str("The extended ordinal time 2005-128T12:15 can also be represented as Sunday May 08, 2005");
  48. ss >> ldt;
  49. cout << ldt << endl; // cout is using default format "2005-May-08 12:15:00 UTC"
  50. /****** content strings ******/
  51. // set up the collections of custom strings.
  52. // only the full names are altered for the sake of brevity
  53. string month_names[12] = { "january", "february", "march",
  54. "april", "may", "june",
  55. "july", "august", "september",
  56. "october", "november", "december" };
  57. vector<string> long_months(&month_names[0], &month_names[12]);
  58. string day_names[7] = { "sunday", "monday", "tuesday", "wednesday",
  59. "thursday", "friday", "saturday" };
  60. vector<string> long_days(&day_names[0], &day_names[7]);
  61. // create date_facet and date_input_facet using all defaults
  62. date_facet* date_output = new date_facet();
  63. date_input_facet* date_input = new date_input_facet();
  64. ss.imbue(locale(ss.getloc(), date_output));
  65. ss.imbue(locale(ss.getloc(), date_input));
  66. // replace names in the output facet
  67. date_output->long_month_names(long_months);
  68. date_output->long_weekday_names(long_days);
  69. // replace names in the input facet
  70. date_input->long_month_names(long_months);
  71. date_input->long_weekday_names(long_days);
  72. // customize month, weekday and date formats
  73. date_output->format("%Y-%B-%d");
  74. date_input->format("%Y-%B-%d");
  75. date_output->month_format("%B"); // full name
  76. date_input->month_format("%B"); // full name
  77. date_output->weekday_format("%A"); // full name
  78. date_input->weekday_format("%A"); // full name
  79. ss.str("");
  80. ss << greg_month(3);
  81. cout << ss.str() << endl; // "march"
  82. ss.str("");
  83. ss << greg_weekday(3);
  84. cout << ss.str() << endl; // "tuesday"
  85. ss.str("");
  86. ss << date(2005,Jul,4);
  87. cout << ss.str() << endl; // "2005-july-04"
  88. /****** special values ******/
  89. // reset the formats to defaults
  90. output_facet->format(local_time_facet::default_time_format);
  91. input_facet->format(local_time_input_facet::default_time_input_format);
  92. // create custom special_values parser and formatter objects
  93. // and add them to the facets
  94. string sv[5] = {"nadt","neg_inf", "pos_inf", "min_dt", "max_dt" };
  95. vector<string> sv_names(&sv[0], &sv[5]);
  96. special_values_parser sv_parser(sv_names.begin(), sv_names.end());
  97. special_values_formatter sv_formatter(sv_names.begin(), sv_names.end());
  98. output_facet->special_values_formatter(sv_formatter);
  99. input_facet->special_values_parser(sv_parser);
  100. ss.str("");
  101. ldt = local_date_time(not_a_date_time);
  102. ss << ldt;
  103. cout << ss.str() << endl; // "nadt"
  104. ss.str("min_dt");
  105. ss >> ldt;
  106. ss.str("");
  107. ss << ldt;
  108. cout << ss.str() << endl; // "1400-Jan-01 00:00:00 UTC"
  109. /****** date/time periods ******/
  110. // reset all formats to defaults
  111. date_output->format(date_facet::default_date_format);
  112. date_input->format(date_facet::default_date_format);
  113. date_output->month_format("%b"); // abbrev
  114. date_input->month_format("%b"); // abbrev
  115. date_output->weekday_format("%a"); // abbrev
  116. date_input->weekday_format("%a"); // abbrev
  117. // create our date_period
  118. date_period dp(date(2005,Mar,1), days(31)); // month of march
  119. // custom period formatter and parser
  120. period_formatter per_formatter(period_formatter::AS_OPEN_RANGE,
  121. " to ", "from ", " exclusive", " inclusive" );
  122. period_parser per_parser(period_parser::AS_OPEN_RANGE,
  123. " to ", "from ", " exclusive" , " inclusive" );
  124. // default output
  125. ss.str("");
  126. ss << dp;
  127. cout << ss.str() << endl; // "[2005-Mar-01/2005-Mar-31]"
  128. // add out custom parser and formatter to the facets
  129. date_output->period_formatter(per_formatter);
  130. date_input->period_parser(per_parser);
  131. // custom output
  132. ss.str("");
  133. ss << dp;
  134. cout << ss.str() << endl; // "from 2005-Feb-01 to 2005-Apr-01 exclusive"
  135. /****** date generators ******/
  136. // custom date_generator phrases
  137. string dg_phrases[9] = { "1st", "2nd", "3rd", "4th", "5th",
  138. "final", "prior to", "following", "in" };
  139. vector<string> phrases(&dg_phrases[0], &dg_phrases[9]);
  140. // create our date_generator
  141. first_day_of_the_week_before d_gen(Monday);
  142. // default output
  143. ss.str("");
  144. ss << d_gen;
  145. cout << ss.str() << endl; // "Mon before"
  146. // add our custom strings to the date facets
  147. date_output->date_gen_phrase_strings(phrases);
  148. date_input->date_gen_element_strings(phrases);
  149. // custom output
  150. ss.str("");
  151. ss << d_gen;
  152. cout << ss.str() << endl; // "Mon prior to"
  153. return 0;
  154. }
  155. /* Copyright 2005: CrystalClear Software, Inc
  156. * http://www.crystalclearsoftware.com
  157. *
  158. * Subject to the Boost Software License, Version 1.0.
  159. * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  160. */