source: qutecom-coip/libs/owutil/util/src/Time.cpp @ 125:d648f4cb122f

Last change on this file since 125:d648f4cb122f was 125:d648f4cb122f, checked in by laurent, 3 years ago

wengo => qutecom

File size: 2.4 KB
Line 
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
27Time::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
35Time::Time(const Time & time) {
36        setHour(time._hour);
37        setMinute(time._minute);
38        setSecond(time._second);
39}
40
41Time::Time(unsigned hour, unsigned minute, unsigned second) {
42        setHour(hour);
43        setMinute(minute);
44        setSecond(second);
45}
46
47Time::~Time() {
48}
49
50bool Time::operator==(const Time & time) const {
51        return ((_hour == time._hour)
52                && (_minute == time._minute)
53                && (_second == time._second));
54}
55
56unsigned Time::getHour() const {
57        return _hour;
58}
59
60void Time::setHour(unsigned hour) {
61        if (hour > 23) {
62                LOG_FATAL("hour cannot be > 23");
63        }
64
65        _hour = hour;
66}
67
68unsigned Time::getMinute() const {
69        return _minute;
70}
71
72void Time::setMinute(unsigned minute) {
73        if (minute > 59) {
74                LOG_FATAL("minute cannot be > 59");
75        }
76
77        _minute = minute;
78}
79
80unsigned Time::getSecond() const {
81        return _second;
82}
83
84void Time::setSecond(unsigned second) {
85        if (second > 59) {
86                LOG_FATAL("second cannot be > 59");
87        }
88
89        _second = second;
90}
91
92std::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
Note: See TracBrowser for help on using the repository browser.