| 1 | /* |
|---|
| 2 | * QuteCom, a voice over Internet phone |
|---|
| 3 | * Copyright (C) 2010 Mbdsys |
|---|
| 4 | * |
|---|
| 5 | * This program is free software; you can redistribute it and/or modify |
|---|
| 6 | * it under the terms of the GNU General Public License as published by |
|---|
| 7 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | * (at your option) any later version. |
|---|
| 9 | * |
|---|
| 10 | * This program is distributed in the hope that it will be useful, |
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | * GNU General Public License for more details. |
|---|
| 14 | * |
|---|
| 15 | * You should have received a copy of the GNU General Public License |
|---|
| 16 | * along with this program; if not, write to the Free Software |
|---|
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | #include <util/Time.h> |
|---|
| 21 | |
|---|
| 22 | #include <util/String.h> |
|---|
| 23 | #include <util/Logger.h> |
|---|
| 24 | |
|---|
| 25 | #include <ctime> |
|---|
| 26 | |
|---|
| 27 | Time::Time() { |
|---|
| 28 | std::time_t curTime = time(NULL); |
|---|
| 29 | struct std::tm * timeinfo = std::localtime(&curTime); |
|---|
| 30 | setHour(timeinfo->tm_hour); |
|---|
| 31 | setMinute(timeinfo->tm_min); |
|---|
| 32 | setSecond(timeinfo->tm_sec); |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | Time::Time(const Time & time) { |
|---|
| 36 | setHour(time._hour); |
|---|
| 37 | setMinute(time._minute); |
|---|
| 38 | setSecond(time._second); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | Time::Time(unsigned hour, unsigned minute, unsigned second) { |
|---|
| 42 | setHour(hour); |
|---|
| 43 | setMinute(minute); |
|---|
| 44 | setSecond(second); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | Time::~Time() { |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | bool Time::operator==(const Time & time) const { |
|---|
| 51 | return ((_hour == time._hour) |
|---|
| 52 | && (_minute == time._minute) |
|---|
| 53 | && (_second == time._second)); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | unsigned Time::getHour() const { |
|---|
| 57 | return _hour; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | void Time::setHour(unsigned hour) { |
|---|
| 61 | if (hour > 23) { |
|---|
| 62 | LOG_FATAL("hour cannot be > 23"); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | _hour = hour; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | unsigned Time::getMinute() const { |
|---|
| 69 | return _minute; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | void Time::setMinute(unsigned minute) { |
|---|
| 73 | if (minute > 59) { |
|---|
| 74 | LOG_FATAL("minute cannot be > 59"); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | _minute = minute; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | unsigned Time::getSecond() const { |
|---|
| 81 | return _second; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | void Time::setSecond(unsigned second) { |
|---|
| 85 | if (second > 59) { |
|---|
| 86 | LOG_FATAL("second cannot be > 59"); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | _second = second; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | std::string Time::toString() const { |
|---|
| 93 | std::string hour = String::fromNumber(_hour); |
|---|
| 94 | std::string minute = String::fromNumber(_minute); |
|---|
| 95 | std::string second = String::fromNumber(_second); |
|---|
| 96 | |
|---|
| 97 | if (hour.size() == 1) { |
|---|
| 98 | hour = "0" + hour; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | if (minute.size() == 1) { |
|---|
| 102 | minute = "0" + minute; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | if (second.size() == 1) { |
|---|
| 106 | second = "0" + second; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | return hour + ":" + minute + ":" + second; |
|---|
| 110 | } |
|---|
| 111 | |
|---|