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 //
13 #include <anna/core/functions.hpp>
14 #include <anna/core/util/Millisecond.hpp>
15 #include <anna/core/util/Second.hpp>
16 #include <anna/core/util/Microsecond.hpp>
21 #define implement_operator(op) \
22 bool Millisecond::operator op (const Second& other) const \
25 return a_value op (other.a_value * (type_t)1000);\
27 bool Millisecond::operator op (const Microsecond& other) const\
30 return a_value op (other.a_value / 1000);\
33 Millisecond::Millisecond(const Second& other) : a_value(other.a_value) { a_value *= (type_t)1000;}
35 Millisecond::Millisecond(const Microsecond& other) : a_value(other.a_value / 1000) {;}
37 Millisecond& Millisecond::operator= (const Second & other)
39 a_value = other.a_value;
40 a_value *= (type_t)1000;
44 Millisecond& Millisecond::operator= (const Microsecond & other)
46 a_value = (other.a_value / 1000);
50 implement_operator( ==)
51 implement_operator( !=)
52 implement_operator( >)
53 implement_operator( <)
56 Millisecond Millisecond::getTime()
59 gettimeofday(&tv, NULL);
60 Millisecond result(Second(tv.tv_sec));
61 Millisecond aux(Microsecond(tv.tv_usec));
62 result.a_value += aux.a_value;
66 timeval* Millisecond::getTimeVal(timeval& tv) const
71 tv.tv_sec = a_value / 1000;
72 tv.tv_usec = (a_value % 1000) * (type_t)1000;
76 string Millisecond::asString() const
78 string result(functions::asString(a_value));
79 return result += " ms";
83 Millisecond Millisecond::fromString(const std::string& value)
84 throw(RuntimeException) {
85 if(value.find(" ms") == string::npos) {
86 string msg("String: ");
88 msg += " | Invalid expression for Millisecond";
89 throw RuntimeException(msg, ANNA_FILE_LOCATION);
92 return Millisecond(atoll(value.c_str()));