Ensures normalization on waitfe/fc-xml operations
[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 #include <string>
10 #include <anna/time/Date.hpp>
11 #include <anna/time/functions.hpp>
12
13 #include <gtest/gtest.h>
14
15 using namespace std;
16 using namespace anna;
17
18
19 TEST(date, Conversions)
20 {
21   anna::time::functions::initialize();
22   anna::time::Date current, current2;
23   current.setNow();
24   unsigned int ntptime = current.getNtpTimestamp();
25   time_t unixtime = current.getUnixTimestamp();
26   current2.storeNtp(ntptime);
27   std::string ntpStr = current2.asString();
28   current2.storeUnix(unixtime);
29   std::string unixStr = current2.asString();
30   EXPECT_EQ(ntpStr, unixStr);
31
32   anna::time::Date myBirth("CET"); // 19 December 1974, 10:00 (GMT+1 = UTC + 1 = CET)
33   myBirth.store("19741219101500"); // yyyymmddHHmmss
34   anna::time::Date same_moment_canary_island("GMT");
35   same_moment_canary_island.store(myBirth.getUnixTimestamp());
36   myBirth.setTz("GMT");
37   EXPECT_EQ(same_moment_canary_island.yyyymmddHHmmss(), myBirth.yyyymmddHHmmss());
38
39   anna::time::Date birthday("EET");
40   birthday.store(same_moment_canary_island.getTm(), "GMT");  // TZ origin = "GMT"
41   EXPECT_EQ(birthday.getUnixTimestamp(), myBirth.getUnixTimestamp());
42
43   myBirth.setTz();
44   birthday.setTz();
45   EXPECT_EQ(myBirth.asString(), birthday.asString());
46   EXPECT_EQ(myBirth.yyyymmddHHmmss(), birthday.yyyymmddHHmmss());
47
48   myBirth.setTz("GMT");
49   EXPECT_EQ(myBirth.yyyymmddHHmmss(), "19741219091500");
50
51   // Adding 18 years to 'myBirth':
52   same_moment_canary_island.setTz("CET");
53   anna::time::Date eighteen("CET");
54   struct tm Tm = myBirth.getTm();
55   Tm.tm_year += 18;
56   eighteen.store(Tm, "GMT");
57   EXPECT_TRUE(eighteen >= same_moment_canary_island);
58   EXPECT_EQ(eighteen.yyyymmddHHmmss(), "19921219101500");
59 }
60
61
62
63 int main(int argc, char **argv)
64 {
65     testing::InitGoogleTest(&argc, argv);
66     return RUN_ALL_TESTS();
67 }
68