datetime.cpp

Example of date & time manipulation.

00001 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
00002 //
00003 // This file is part of GNU uCommon C++.
00004 //
00005 // GNU uCommon C++ is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU Lesser General Public License as published
00007 // by the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // GNU uCommon C++ is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public License
00016 // along with GNU uCommon C++.  If not, see <http://www.gnu.org/licenses/>.
00017 
00018 #ifndef DEBUG
00019 #define DEBUG
00020 #endif
00021 
00022 #include <ucommon-config.h>
00023 #include <ucommon/ucommon.h>
00024 
00025 #include <stdio.h>
00026 
00027 using namespace UCOMMON_NAMESPACE;
00028 
00029 int main(int argc, char **argv)
00030 {
00031     Date date = Date(2003, 1, 6);
00032     int exp_year = 2003;
00033     unsigned exp_month = 1;
00034     unsigned exp_day = 6;
00035     unsigned exp_dayofweek = 1;
00036     String exp_stringdate;
00037     tm_t exp_dt;
00038     time_t exp_ctime;
00039     char buf[20];
00040 
00041     snprintf(buf, sizeof(buf),
00042         "%04d-%02d-%02d", exp_year, exp_month, exp_day);
00043 
00044     memset(&exp_dt, 0, sizeof(exp_dt));
00045     exp_dt.tm_year = exp_year - 1900;
00046     exp_dt.tm_mon = exp_month - 1;
00047     exp_dt.tm_mday = exp_day;
00048     exp_ctime = mktime(&exp_dt);
00049 
00050     assert(exp_year == date.year());
00051     assert(exp_month == date.month());
00052     assert(exp_day == date.day());
00053     assert(exp_dayofweek == date.dow());
00054 
00055     // test some conversions...
00056     exp_stringdate = date();
00057     assert(eq(*exp_stringdate, "2003-01-06"));
00058     date.put(buf);
00059     assert(eq(buf, "2003-01-06"));
00060     assert(exp_ctime == date.timeref());
00061 
00062     // some operator tests...
00063     Date aday = date;
00064     Date nextday(2003, 1, 7);
00065     assert(aday == date);
00066     assert((++aday) == nextday);
00067     assert(aday != date);
00068     assert(date <= aday);
00069     assert(date < aday);
00070 
00071     // play with math and casting operators...
00072     Date newday = nextday + 5l;
00073     assert((long)newday == 20030112l);
00074     assert((long)nextday == 20030107l);
00075     assert(newday - nextday == 5);
00076 
00077     // test some math...
00078     assert(20030106l == date.get());
00079     date -= 6;
00080     assert(20021231l == date.get());
00081 
00082     // test invalid date...
00083     date = "20031306";
00084     assert(!is(date));
00085 
00086     // conversion check...
00087     date = "2003-08-04";
00088     assert((long)date == 20030804l);
00089 
00090     DateTimeString dts("2003-02-28 23:59:55");
00091     eq((const char *)dts, "2003-02-28 23:59:55");
00092 
00093     DateTime tmp("2003-02-28 23:59:55");
00094     snprintf(buf, sizeof(buf), "%.5f", (double)tmp);
00095     assert(eq(buf, "2452699.99994"));
00096     assert((long)tmp == 20030228l);
00097     tmp += 5;   // add 5 seconds to force rollover...
00098     assert((long)tmp == 20030301l);
00099 
00100     return 0;
00101 }
00102 

Generated on 14 Aug 2013 for UCommon by  doxygen 1.4.7