<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>ucommon: datetime.cpp</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.6.3 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="main.html"><span>Main Page</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Data Structures</span></a></li> <li><a href="files.html"><span>Files</span></a></li> <li><a href="examples.html"><span>Examples</span></a></li> </ul> </div> </div> <div class="contents"> <h1>datetime.cpp</h1><p>Example of date & time manipulation.</p> <div class="fragment"><pre class="fragment"><span class="comment">// Copyright (C) 2006-2010 David Sugar, Tycho Softworks.</span> <span class="comment">//</span> <span class="comment">// This file is part of GNU uCommon C++.</span> <span class="comment">//</span> <span class="comment">// GNU uCommon C++ is free software: you can redistribute it and/or modify</span> <span class="comment">// it under the terms of the GNU Lesser General Public License as published</span> <span class="comment">// by the Free Software Foundation, either version 3 of the License, or</span> <span class="comment">// (at your option) any later version.</span> <span class="comment">//</span> <span class="comment">// GNU uCommon C++ is distributed in the hope that it will be useful,</span> <span class="comment">// but WITHOUT ANY WARRANTY; without even the implied warranty of</span> <span class="comment">// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span> <span class="comment">// GNU Lesser General Public License for more details.</span> <span class="comment">//</span> <span class="comment">// You should have received a copy of the GNU Lesser General Public License</span> <span class="comment">// along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.</span> <span class="preprocessor">#ifndef DEBUG</span> <span class="preprocessor"></span><span class="preprocessor">#define DEBUG</span> <span class="preprocessor"></span><span class="preprocessor">#endif</span> <span class="preprocessor"></span> <span class="preprocessor">#include <<a class="code" href="a00204.html" title="Top level include file for the GNU uCommon C++ core library.">ucommon/ucommon.h</a>></span> <span class="preprocessor">#include <stdio.h></span> <span class="keyword">using namespace </span>UCOMMON_NAMESPACE; <span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv) { Date date = Date(2003, 1, 6); <span class="keywordtype">int</span> exp_year = 2003; <span class="keywordtype">int</span> exp_month = 1; <span class="keywordtype">int</span> exp_day = 6; <span class="keywordtype">int</span> exp_dayofweek = 1; <a name="a0"></a><a class="code" href="a00208.html#a9045ddbf2980f6330c065a6935f40161" title="A convenience type when mixing std::string in old compilers that are bad with namespaces...">String</a> exp_stringdate; <a name="a1"></a><a class="code" href="a00183.html#a2fea857c30e2c901566fcf45fac68ac5" title="Convenience type for struct tm.">tm_t</a> exp_dt; time_t exp_ctime; <span class="keywordtype">char</span> buf[20]; snprintf(buf, <span class="keyword">sizeof</span>(buf), <span class="stringliteral">"%04d-%02d-%02d"</span>, exp_year, exp_month, exp_day); memset(&exp_dt, 0, <span class="keyword">sizeof</span>(exp_dt)); exp_dt.tm_year = exp_year - 1900; exp_dt.tm_mon = exp_month - 1; exp_dt.tm_mday = exp_day; exp_ctime = mktime(&exp_dt); assert(exp_year == date[Date::year]); assert(exp_month == date[Date::month]); assert(exp_day == date[Date::day]); assert(exp_dayofweek == date[Date::dow]); <span class="comment">// test some conversions...</span> exp_stringdate = date(); assert(<a name="a2"></a><a class="code" href="a00208.html#affb4ebc34459cfbe1d2965c178e52ee9" title="Compare two socket addresses to see if equal.">eq</a>(*exp_stringdate, <span class="stringliteral">"2003-01-06"</span>)); date.get(buf); assert(<a class="code" href="a00208.html#affb4ebc34459cfbe1d2965c178e52ee9" title="Compare two socket addresses to see if equal.">eq</a>(buf, <span class="stringliteral">"2003-01-06"</span>)); assert(exp_ctime == date.getTime()); <span class="comment">// some operator tests...</span> Date aday = date; Date nextday(2003, 1, 7); assert(aday == date); assert((++aday) == nextday); assert(aday != date); assert(date <= aday); assert(date < aday); <span class="comment">// play with math and casting operators...</span> Date newday = nextday + 5l; assert((<span class="keywordtype">long</span>)newday == 20030112l); assert((<span class="keywordtype">long</span>)nextday == 20030107l); assert(newday - nextday == 5); <span class="comment">// test some math...</span> assert(20030106l == date.get()); date -= 6; assert(20021231l == date.get()); <span class="comment">// test invalid date...</span> date = <span class="stringliteral">"20031306"</span>; assert(!<a name="a3"></a><a class="code" href="a00208.html#acf24ef988438eb342a8a348f19524e37" title="Convenience function to validate object assuming it is castable to bool.">is</a>(date)); <span class="comment">// conversion check...</span> date = <span class="stringliteral">"2003-08-04"</span>; assert((<span class="keywordtype">long</span>)date == 20030804l); DateTimeString dts(<span class="stringliteral">"2003-02-28 23:59:55"</span>); <a class="code" href="a00208.html#affb4ebc34459cfbe1d2965c178e52ee9" title="Compare two socket addresses to see if equal.">eq</a>((<span class="keyword">const</span> <span class="keywordtype">char</span> *)dts, <span class="stringliteral">"2003-02-28 23:59:55"</span>); DateTime tmp(<span class="stringliteral">"2003-02-28 23:59:55"</span>); snprintf(buf, <span class="keyword">sizeof</span>(buf), <span class="stringliteral">"%.5f"</span>, (<span class="keywordtype">double</span>)tmp); assert(<a class="code" href="a00208.html#affb4ebc34459cfbe1d2965c178e52ee9" title="Compare two socket addresses to see if equal.">eq</a>(buf, <span class="stringliteral">"2452699.99994"</span>)); assert((<span class="keywordtype">long</span>)tmp == 20030228l); tmp += 5; <span class="comment">// add 5 seconds to force rollover...</span> assert((<span class="keywordtype">long</span>)tmp == 20030301l); <span class="keywordflow">return</span> 0; } </pre></div> </div> <hr class="footer"/><address style="text-align: right;"><small>Generated on Thu Jul 14 16:38:56 2011 for ucommon by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.3 </small></address> </body> </html>