1 | // |
2 | // Time.cpp |
3 | // |
4 | // Library: Data |
5 | // Package: DataCore |
6 | // Module: Time |
7 | // |
8 | // Copyright (c) 2006, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/Data/Time.h" |
16 | #include "Poco/Data/DynamicDateTime.h" |
17 | #include "Poco/DateTime.h" |
18 | #include "Poco/Dynamic/Var.h" |
19 | |
20 | |
21 | using Poco::DateTime; |
22 | using Poco::Dynamic::Var; |
23 | |
24 | |
25 | namespace Poco { |
26 | namespace Data { |
27 | |
28 | |
29 | Time::Time() |
30 | { |
31 | DateTime dt; |
32 | assign(dt.hour(), dt.minute(), dt.second()); |
33 | } |
34 | |
35 | |
36 | Time::Time(int timeHour, int timeMinute, int timeSecond) |
37 | { |
38 | assign(timeHour, timeMinute, timeSecond); |
39 | } |
40 | |
41 | |
42 | Time::Time(const DateTime& dt) |
43 | { |
44 | assign(dt.hour(), dt.minute(), dt.second()); |
45 | } |
46 | |
47 | |
48 | Time::~Time() |
49 | { |
50 | } |
51 | |
52 | |
53 | void Time::assign(int timeHour, int timeMinute, int timeSecond) |
54 | { |
55 | if (timeHour < 0 || timeHour > 23) |
56 | throw InvalidArgumentException("Hour must be between 0 and 23." ); |
57 | |
58 | if (timeMinute < 0 || timeMinute > 59) |
59 | throw InvalidArgumentException("Minute must be between 0 and 59." ); |
60 | |
61 | if (timeSecond < 0 || timeSecond > 59) |
62 | throw InvalidArgumentException("Second must be between 0 and 59." ); |
63 | |
64 | _hour = timeHour; |
65 | _minute = timeMinute; |
66 | _second = timeSecond; |
67 | } |
68 | |
69 | |
70 | bool Time::operator < (const Time& time) const |
71 | { |
72 | int timeHour = time.hour(); |
73 | |
74 | if (_hour < timeHour) return true; |
75 | else if (_hour > timeHour) return false; |
76 | else // hours equal |
77 | { |
78 | int timeMinute = time.minute(); |
79 | if (_minute < timeMinute) return true; |
80 | else |
81 | if (_minute > timeMinute) return false; |
82 | else // minutes equal |
83 | if (_second < time.second()) return true; |
84 | } |
85 | |
86 | return false; |
87 | } |
88 | |
89 | |
90 | Time& Time::operator = (const Var& var) |
91 | { |
92 | #ifndef __GNUC__ |
93 | // g++ used to choke on this, newer versions seem to digest it fine |
94 | // TODO: determine the version able to handle it properly |
95 | *this = var.extract<Time>(); |
96 | #else |
97 | *this = var.operator Time(); |
98 | #endif |
99 | return *this; |
100 | } |
101 | |
102 | |
103 | } } // namespace Poco::Data |
104 | |
105 | |
106 | #ifdef __GNUC__ |
107 | // only needed for g++ (see comment in Time::operator = above) |
108 | |
109 | namespace Poco { |
110 | namespace Dynamic { |
111 | |
112 | |
113 | using Poco::Data::Time; |
114 | using Poco::DateTime; |
115 | |
116 | |
117 | template <> |
118 | Var::operator Time () const |
119 | { |
120 | VarHolder* pHolder = content(); |
121 | |
122 | if (!pHolder) |
123 | throw InvalidAccessException("Can not convert empty value." ); |
124 | |
125 | if (typeid(Time) == pHolder->type()) |
126 | return extract<Time>(); |
127 | else |
128 | { |
129 | Poco::DateTime result; |
130 | pHolder->convert(result); |
131 | return Time(result); |
132 | } |
133 | } |
134 | |
135 | |
136 | } } // namespace Poco::Dynamic |
137 | |
138 | |
139 | #endif // __GNUC__ |
140 | |