Remove dynamic exceptions
[anna.git] / source / core / util / Second.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 <time.h>
10 #include <sys/time.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13
14 #include <anna/core/functions.hpp>
15 #include <anna/core/util/Second.hpp>
16 #include <anna/core/util/Millisecond.hpp>
17 #include <anna/core/util/Microsecond.hpp>
18
19 using namespace std;
20 using namespace anna;
21
22 #define implement_operator(op) \
23    bool Second::operator op (const Millisecond& other) const \
24       \
25    {\
26       return a_value op (other.a_value / 1000);\
27    }\
28    bool Second::operator op (const Microsecond& other) const\
29       \
30    {\
31       return a_value op (other.a_value / 1000000);\
32    }
33
34 Second::Second(const Millisecond& other) : a_value(other.a_value / 1000) {;}
35
36 Second::Second(const Microsecond& other) : a_value(other.a_value / 1000000) {;}
37
38 Second& Second::operator= (const Millisecond & other)
39 {
40   a_value = (other.a_value / 1000);
41   return *this;
42 }
43
44 Second& Second::operator= (const Microsecond & other)
45 {
46   a_value = (other.a_value / 1000000);
47   return *this;
48 }
49
50 implement_operator( ==)
51 implement_operator( !=)
52 implement_operator( >)
53 implement_operator( <)
54
55 string Second::asDateTime(const char* format) const
56 {
57   char aux [DateTimeSizeString];
58   return string(asDateTime(aux, format));
59 }
60
61 const char* Second::asDateTime(char* result, const char* format) const
62 {
63   struct tm* tt = localtime((time_t*) & a_value);
64   char aux [256];
65
66   if(strftime(aux, sizeof(aux), format, tt) == 0)
67     anna_strcpy(aux, "Bad date");
68
69   return anna_strcpy(result, aux);
70 }
71
72 //static
73 Second Second::getTime()
74 {
75   struct timeval tv;
76   gettimeofday(&tv, NULL);
77   return Second(tv.tv_sec);
78 }
79
80 //static
81 Second Second::getLocalTime()
82 {
83   return Second(time(NULL));
84 }
85
86 string Second::asString() const
87 {
88   string result(functions::asString(a_value));
89   return result += " sec";
90 }
91
92 //static
93 Second Second::fromString(const std::string& value)
94 noexcept(false) {
95   if(value.find(" sec") == string::npos) {
96     string msg("String: ");
97     msg += value;
98     msg += " | Invalid expression for Second";
99     throw RuntimeException(msg, ANNA_FILE_LOCATION);
100   }
101
102   return Second(atoi(value.c_str()));
103 }
104
105