mirror of
https://github.com/boostorg/date_time.git
synced 2026-02-24 16:02:26 +00:00
74 lines
3.8 KiB
HTML
74 lines
3.8 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.10 -->
|
|
<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/gdtl/gregorian/gregorian.hpp"</font>
|
|
<font class="preprocessor">#include "boost/gdtl/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"> </font>
|
|
<font class="keyword"></font>{
|
|
|
|
<font class="keyword">using</font> <font class="keyword">namespace </font>gregorian;
|
|
|
|
<font class="keywordflow">try</font> {
|
|
<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(gdtl::parse_date<date>(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(gdtl::parse_undelimited_date<date>(ud));
|
|
std::cout << <a name="a1"></a>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(gdtl::parse_undelimited_date<date>(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 Mar 13 13:38:03 2002 by <a href="http://www.doxygen.org">Doxygen</a> for <a href="http://www.crystalclearsoftware.com">CrystalClear Software</a> © 2000-2002</small></address>
|
|
</body>
|
|
</html>
|