b5d0d28c49e1d1740b4822fbff0595db7073821d
[anna.git] / test / time / main.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #define BOOST_TEST_MODULE ANNA_TIME_TEST
10
11 #include <anna/test/clang_specific.hpp>
12 #include <boost/test/included/unit_test.hpp>
13
14 //#include <iostream>
15 #include <string>
16
17 #include <anna/time/Date.hpp>
18 #include <anna/time/functions.hpp>
19
20 using namespace std;
21 using namespace anna;
22
23 // see http://www.boost.org/doc/libs/1_44_0/libs/test/doc/html/utf/testing-tools.html
24 //     http://www.boost.org/doc/libs/1_44_0/libs/test/doc/html/utf/testing-tools/reference.html
25
26 BOOST_AUTO_TEST_CASE(date) {
27   anna::time::functions::initialize();
28   anna::time::Date current, current2;
29   current.setNow();
30   unsigned int ntptime = current.getNtpTimestamp();
31   time_t unixtime = current.getUnixTimestamp();
32   current2.storeNtp(ntptime);
33   std::string ntpStr = current2.asString();
34   current2.storeUnix(unixtime);
35   std::string unixStr = current2.asString();
36   BOOST_REQUIRE_EQUAL(ntpStr, unixStr);
37
38   anna::time::Date myBirth("CET"); // 19 December 1974, 10:00 (GMT+1 = UTC + 1 = CET)
39   myBirth.store("19741219101500"); // yyyymmddHHmmss
40   anna::time::Date same_moment_canary_island("GMT");
41   same_moment_canary_island.store(myBirth.getUnixTimestamp());
42   myBirth.setTz("GMT");
43   BOOST_REQUIRE_EQUAL(same_moment_canary_island.yyyymmddHHmmss(), myBirth.yyyymmddHHmmss());
44
45   anna::time::Date birthday("EET");
46   birthday.store(same_moment_canary_island.getTm(), "GMT");  // TZ origin = "GMT"
47   BOOST_REQUIRE_EQUAL(birthday.getUnixTimestamp(), myBirth.getUnixTimestamp());
48
49   myBirth.setTz();
50   birthday.setTz();
51   BOOST_REQUIRE_EQUAL(myBirth.asString(), birthday.asString());
52   BOOST_REQUIRE_EQUAL(myBirth.yyyymmddHHmmss(), birthday.yyyymmddHHmmss());
53
54   myBirth.setTz("GMT");
55   BOOST_REQUIRE_EQUAL(myBirth.yyyymmddHHmmss(), "19741219091500");
56
57   // Adding 18 years to 'myBirth':
58   same_moment_canary_island.setTz("CET");
59   anna::time::Date eighteen("CET");
60   struct tm Tm = myBirth.getTm();
61   Tm.tm_year += 18;
62   eighteen.store(Tm, "GMT");
63   BOOST_REQUIRE(eighteen >= same_moment_canary_island);
64   BOOST_REQUIRE_EQUAL(eighteen.yyyymmddHHmmss(), "19921219101500");
65 }
66