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
Date()31 Date::Date()
32 {
33 DateTime dt;
34 assign(dt.year(), dt.month(), dt.day());
35 }
36
37
Date(int year,int month,int day)38 Date::Date(int year, int month, int day)
39 {
40 assign(year, month, day);
41 }
42
43
Date(const DateTime & dt)44 Date::Date(const DateTime& dt)
45 {
46 assign(dt.year(), dt.month(), dt.day());
47 }
48
49
~Date()50 Date::~Date()
51 {
52 }
53
54
assign(int year,int month,int day)55 void Date::assign(int year, int month, int day)
56 {
57 if (year < 0 || year > 9999)
58 throw InvalidArgumentException("Year must be between 0 and 9999");
59
60 if (month < 1 || month > 12)
61 throw InvalidArgumentException("Month must be between 1 and 12");
62
63 if (day < 1 || day > DateTime::daysOfMonth(year, month))
64 throw InvalidArgumentException("Month must be between 1 and " +
65 NumberFormatter::format(DateTime::daysOfMonth(year, month)));
66
67 _year = year;
68 _month = month;
69 _day = day;
70 }
71
72
operator <(const Date & date) const73 bool Date::operator < (const Date& date) const
74 {
75 int year = date.year();
76
77 if (_year < year) return true;
78 else if (_year > year) return false;
79 else // years equal
80 {
81 int month = date.month();
82 if (_month < month) return true;
83 else
84 if (_month > month) return false;
85 else // months equal
86 if (_day < date.day()) return true;
87 }
88
89 return false;
90 }
91
92
operator =(const Var & var)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 <>
operator Date() const121 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