mirror of
https://github.com/boostorg/date_time.git
synced 2026-01-28 19:12:17 +00:00
74 lines
3.5 KiB
HTML
74 lines
3.5 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
|
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
|
|
<title>Example Documentation</title>
|
|
<link href="doxygen.css" rel="stylesheet" type="text/css">
|
|
</head><body bgcolor="#ffffff">
|
|
<!-- Generated by Doxygen 1.2.17 -->
|
|
<hr><h1>dates_as_strings.cpp</h1> Various parsing and output of strings.
|
|
<p>
|
|
<div class="fragment"><pre><font class="comment">/*</font>
|
|
<font class="comment">The following is a simple example that shows conversion of dates to and </font>
|
|
<font class="comment">from a std::string.</font>
|
|
<font class="comment"></font>
|
|
<font class="comment">Expected output:</font>
|
|
<font class="comment">2001-Oct-09</font>
|
|
<font class="comment">2001-10-09</font>
|
|
<font class="comment">Tuesday October 9, 2001</font>
|
|
<font class="comment">An expected exception is next: </font>
|
|
<font class="comment"> Exception: Month number is out of range 1..12</font>
|
|
<font class="comment"></font>
|
|
<font class="comment">*/</font>
|
|
|
|
<font class="preprocessor">#include "boost/date_time/gregorian/gregorian.hpp"</font>
|
|
<font class="preprocessor">#include "boost/date_time/date_parsing.hpp"</font>
|
|
<font class="preprocessor">#include <iostream></font>
|
|
<font class="preprocessor">#include <string></font>
|
|
|
|
|
|
|
|
<font class="keywordtype">int</font>
|
|
main()
|
|
{
|
|
|
|
<font class="keyword">using</font> <font class="keyword">namespace </font>boost::gregorian;
|
|
|
|
try {
|
|
<font class="comment">// The following date is in ISO 8601 extended format (CCYY-MM-DD)</font>
|
|
std::string s(<font class="stringliteral">"2001-10-9"</font>); <font class="comment">//2001-October-05</font>
|
|
date d(from_string(s));
|
|
std::cout << to_simple_string(d) << std::endl;
|
|
|
|
<font class="comment">//Read ISO Standard(CCYYMMDD) and output ISO Extended</font>
|
|
std::string ud(<font class="stringliteral">"20011009"</font>); <font class="comment">//2001-Oct-09</font>
|
|
date d1(from_undelimited_string(ud));
|
|
std::cout << to_iso_extended_string(d1) << std::endl;
|
|
|
|
<font class="comment">//Output the parts of the date - Tuesday October 9, 2001</font>
|
|
date::ymd_type ymd = d1.year_month_day();
|
|
greg_weekday wd = d1.day_of_week();
|
|
std::cout << wd.as_long_string() << <font class="stringliteral">" "</font>
|
|
<< ymd.month.as_long_string() << <font class="stringliteral">" "</font>
|
|
<< ymd.day << <font class="stringliteral">", "</font> << ymd.year
|
|
<< std::endl;
|
|
|
|
|
|
<font class="comment">//Let's send in month 25 by accident and create an exception</font>
|
|
std::string bad_date(<font class="stringliteral">"20012509"</font>); <font class="comment">//2001-??-09</font>
|
|
std::cout << <font class="stringliteral">"An expected exception is next: "</font> << std::endl;
|
|
date wont_construct(from_undelimited_string(bad_date));
|
|
<font class="comment">//use wont_construct so compiler doesn't complain, but you wont get here!</font>
|
|
std::cout << <font class="stringliteral">"oh oh, you should reach this line: "</font>
|
|
<< to_iso_string(wont_construct) << std::endl;
|
|
}
|
|
<font class="keywordflow">catch</font>(std::exception& e) {
|
|
std::cout << <font class="stringliteral">" Exception: "</font> << e.what() << std::endl;
|
|
}
|
|
|
|
|
|
<font class="keywordflow">return</font> 0;
|
|
}
|
|
</pre></div><hr><address><small>
|
|
Generated Wed Aug 21 16:54:33 2002 by Doxygen for CrystalClear Software © 2000-2002</small></address>
|
|
</body>
|
|
</html>
|