Alexandre Lision | ddd731e | 2014-01-31 11:50:08 -0500 | [diff] [blame] | 1 | // Copyright (C) 2006-2010 David Sugar, Tycho Softworks. |
| 2 | // |
| 3 | // This file is part of GNU uCommon C++. |
| 4 | // |
| 5 | // GNU uCommon C++ is free software: you can redistribute it and/or modify |
| 6 | // it under the terms of the GNU Lesser General Public License as published |
| 7 | // by the Free Software Foundation, either version 3 of the License, or |
| 8 | // (at your option) any later version. |
| 9 | // |
| 10 | // GNU uCommon C++ is distributed in the hope that it will be useful, |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | // GNU Lesser General Public License for more details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU Lesser General Public License |
| 16 | // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | #ifndef DEBUG |
| 19 | #define DEBUG |
| 20 | #endif |
| 21 | |
| 22 | #include <ucommon-config.h> |
| 23 | #include <ucommon/ucommon.h> |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | |
| 27 | using namespace UCOMMON_NAMESPACE; |
| 28 | |
| 29 | int main(int argc, char **argv) |
| 30 | { |
| 31 | Date date = Date(2003, 1, 6); |
| 32 | int exp_year = 2003; |
| 33 | unsigned exp_month = 1; |
| 34 | unsigned exp_day = 6; |
| 35 | unsigned exp_dayofweek = 1; |
| 36 | String exp_stringdate; |
| 37 | tm_t exp_dt; |
| 38 | time_t exp_ctime; |
| 39 | char buf[20]; |
| 40 | |
| 41 | snprintf(buf, sizeof(buf), |
| 42 | "%04d-%02d-%02d", exp_year, exp_month, exp_day); |
| 43 | |
| 44 | memset(&exp_dt, 0, sizeof(exp_dt)); |
| 45 | exp_dt.tm_year = exp_year - 1900; |
| 46 | exp_dt.tm_mon = exp_month - 1; |
| 47 | exp_dt.tm_mday = exp_day; |
| 48 | exp_ctime = mktime(&exp_dt); |
| 49 | |
| 50 | assert(exp_year == date.year()); |
| 51 | assert(exp_month == date.month()); |
| 52 | assert(exp_day == date.day()); |
| 53 | assert(exp_dayofweek == date.dow()); |
| 54 | |
| 55 | // test some conversions... |
| 56 | exp_stringdate = date(); |
| 57 | assert(eq(*exp_stringdate, "2003-01-06")); |
| 58 | date.put(buf); |
| 59 | assert(eq(buf, "2003-01-06")); |
| 60 | assert(exp_ctime == date.timeref()); |
| 61 | |
| 62 | // some operator tests... |
| 63 | Date aday = date; |
| 64 | Date nextday(2003, 1, 7); |
| 65 | assert(aday == date); |
| 66 | assert((++aday) == nextday); |
| 67 | assert(aday != date); |
| 68 | assert(date <= aday); |
| 69 | assert(date < aday); |
| 70 | |
| 71 | // play with math and casting operators... |
| 72 | Date newday = nextday + 5l; |
| 73 | assert((long)newday == 20030112l); |
| 74 | assert((long)nextday == 20030107l); |
| 75 | assert(newday - nextday == 5); |
| 76 | |
| 77 | // test some math... |
| 78 | assert(20030106l == date.get()); |
| 79 | date -= 6; |
| 80 | assert(20021231l == date.get()); |
| 81 | |
| 82 | // test invalid date... |
| 83 | date = "20031306"; |
| 84 | assert(!is(date)); |
| 85 | |
| 86 | // conversion check... |
| 87 | date = "2003-08-04"; |
| 88 | assert((long)date == 20030804l); |
| 89 | |
| 90 | DateTimeString dts("2003-02-28 23:59:55"); |
| 91 | eq((const char *)dts, "2003-02-28 23:59:55"); |
| 92 | |
| 93 | DateTime tmp("2003-02-28 23:59:55"); |
| 94 | snprintf(buf, sizeof(buf), "%.5f", (double)tmp); |
| 95 | assert(eq(buf, "2452699.99994")); |
| 96 | assert((long)tmp == 20030228l); |
| 97 | tmp += 5; // add 5 seconds to force rollover... |
| 98 | assert((long)tmp == 20030301l); |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |