1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
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 //
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>
22 #define implement_operator(op) \
23 bool Second::operator op (const Millisecond& other) const \
26 return a_value op (other.a_value / 1000);\
28 bool Second::operator op (const Microsecond& other) const\
31 return a_value op (other.a_value / 1000000);\
34 Second::Second(const Millisecond& other) : a_value(other.a_value / 1000) {;}
36 Second::Second(const Microsecond& other) : a_value(other.a_value / 1000000) {;}
38 Second& Second::operator= (const Millisecond & other)
40 a_value = (other.a_value / 1000);
44 Second& Second::operator= (const Microsecond & other)
46 a_value = (other.a_value / 1000000);
50 implement_operator( ==)
51 implement_operator( !=)
52 implement_operator( >)
53 implement_operator( <)
55 string Second::asDateTime(const char* format) const
57 char aux [DateTimeSizeString];
58 return string(asDateTime(aux, format));
61 const char* Second::asDateTime(char* result, const char* format) const
63 struct tm* tt = localtime((time_t*) & a_value);
66 if(strftime(aux, sizeof(aux), format, tt) == 0)
67 anna_strcpy(aux, "Bad date");
69 return anna_strcpy(result, aux);
73 Second Second::getTime()
76 gettimeofday(&tv, NULL);
77 return Second(tv.tv_sec);
81 Second Second::getLocalTime()
83 return Second(time(NULL));
86 string Second::asString() const
88 string result(functions::asString(a_value));
89 return result += " sec";
93 Second Second::fromString(const std::string& value)
95 if(value.find(" sec") == string::npos) {
96 string msg("String: ");
98 msg += " | Invalid expression for Second";
99 throw RuntimeException(msg, ANNA_FILE_LOCATION);
102 return Second(atoi(value.c_str()));