1 | // |
2 | // Date.cpp |
3 | // |
4 | // Library: Data |
5 | // Package: DataCore |
6 | // Module: Date |
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/Date.h" |
16 | #include "Poco/DateTime.h" |
17 | #include "Poco/NumberFormatter.h" |
18 | #include "Poco/Data/DynamicDateTime.h" |
19 | #include "Poco/Dynamic/Var.h" |
20 | |
21 | |
22 | using Poco::DateTime; |
23 | using Poco::Dynamic::Var; |
24 | using Poco::NumberFormatter; |
25 | |
26 | |
27 | namespace Poco { |
28 | namespace Data { |
29 | |
30 | |
31 | Date::Date() |
32 | { |
33 | DateTime dt; |
34 | assign(dt.year(), dt.month(), dt.day()); |
35 | } |
36 | |
37 | |
38 | Date::Date(int dateYear, int dateMonth, int dateDay) |
39 | { |
40 | assign(dateYear, dateMonth, dateDay); |
41 | } |
42 | |
43 | |
44 | Date::Date(const DateTime& dt) |
45 | { |
46 | assign(dt.year(), dt.month(), dt.day()); |
47 | } |
48 | |
49 | |
50 | Date::~Date() |
51 | { |
52 | } |
53 | |
54 | |
55 | void Date::assign(int dateYear, int dateMonth, int dateDay) |
56 | { |
57 | if (dateYear < 0 || dateYear > 9999) |
58 | throw InvalidArgumentException("Year must be between 0 and 9999" ); |
59 | |
60 | if (dateMonth < 1 || dateMonth > 12) |
61 | throw InvalidArgumentException("Month must be between 1 and 12" ); |
62 | |
63 | if (dateDay < 1 || dateDay > DateTime::daysOfMonth(dateYear, dateMonth)) |
64 | throw InvalidArgumentException("Month must be between 1 and " + |
65 | NumberFormatter::format(DateTime::daysOfMonth(dateYear, dateMonth))); |
66 | |
67 | _year = dateYear; |
68 | _month = dateMonth; |
69 | _day = dateDay; |
70 | } |
71 | |
72 | |
73 | bool Date::operator < (const Date& date) const |
74 | { |
75 | int dateYear = date.year(); |
76 | |
77 | if (_year < dateYear) return true; |
78 | else if (_year > dateYear) return false; |
79 | else // years equal |
80 | { |
81 | int dateMonth = date.month(); |
82 | if (_month < dateMonth) return true; |
83 | else |
84 | if (_month > dateMonth) return false; |
85 | else // months equal |
86 | if (_day < date.day()) return true; |
87 | } |
88 | |
89 | return false; |
90 | } |
91 | |
92 | |
93 | Date& Date::operator = (const Var& var) |
94 | { |
95 | #ifndef __GNUC__ |
96 | // g++ used to choke on this, newer versions seem to digest it fine |
97 | // TODO: determine the version able to handle it properly |
98 | *this = var.extract<Date>(); |
99 | #else |
100 | *this = var.operator Date(); |
101 | #endif |
102 | return *this; |
103 | } |
104 | |
105 | |
106 | } } // namespace Poco::Data |
107 | |
108 | |
109 | #ifdef __GNUC__ |
110 | // only needed for g++ (see comment in Date::operator = above) |
111 | |
112 | namespace Poco { |
113 | namespace Dynamic { |
114 | |
115 | |
116 | using Poco::Data::Date; |
117 | using Poco::DateTime; |
118 | |
119 | |
120 | template <> |
121 | Var::operator Date () const |
122 | { |
123 | VarHolder* pHolder = content(); |
124 | |
125 | if (!pHolder) |
126 | throw InvalidAccessException("Can not convert empty value." ); |
127 | |
128 | if (typeid(Date) == pHolder->type()) |
129 | return extract<Date>(); |
130 | else |
131 | { |
132 | Poco::DateTime result; |
133 | pHolder->convert(result); |
134 | return Date(result); |
135 | } |
136 | } |
137 | |
138 | |
139 | } } // namespace Poco::Dynamic |
140 | |
141 | |
142 | #endif // __GNUC__ |
143 | |