OCILIB (C and C++ Driver for Oracle)  4.7.3
Open source and cross platform Oracle Driver delivering efficient access to Oracle databases.
Date.hpp
1 /*
2  * OCILIB - C Driver for Oracle (C Wrapper for Oracle OCI)
3  *
4  * Website: http://www.ocilib.net
5  *
6  * Copyright (c) 2007-2021 Vincent ROGIER <vince.rogier@ocilib.net>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #pragma once
22 
23 #include "ocilibcpp/types.hpp"
24 
25 namespace ocilib
26 {
27 
28 inline Date::Date(bool create)
29 {
30  if (create)
31  {
32  Allocate();
33  }
34 }
35 
36 inline Date::Date(const ostring& str, const ostring& format)
37 {
38  Allocate();
39 
40  FromString(str, format);
41 }
42 
43 inline Date::Date(const otext* str, const otext* format)
44 {
45  Allocate();
46 
47  FromString(str, format);
48 }
49 
50 inline Date::Date(OCI_Date *pDate, core::Handle *parent)
51 {
52  Acquire(pDate, nullptr, nullptr, parent);
53 }
54 
55 inline void Date::Allocate()
56 {
57  Acquire(core::Check(OCI_DateCreate(nullptr)), reinterpret_cast<HandleFreeFunc>(OCI_DateFree), nullptr, nullptr);
58 }
59 
61 {
62  Date result;
63 
64  result.Allocate();
65 
67 
68  return result;
69 }
70 
71 inline Date Date::Clone() const
72 {
73  Date result;
74 
75  result.Allocate();
76 
77  core::Check(OCI_DateAssign(result, *this));
78 
79  return result;
80 }
81 
82 inline int Date::Compare(const Date& other) const
83 {
84  return core::Check(OCI_DateCompare(*this, other));
85 }
86 
87 inline bool Date::IsValid() const
88 {
89  return (core::Check(OCI_DateCheck(*this)) == 0);
90 }
91 
92 inline int Date::GetYear() const
93 {
94  int year = 0, month = 0, day = 0;
95 
96  GetDate(year, month, day);
97 
98  return year;
99 }
100 
101 inline void Date::SetYear(int value)
102 {
103  int year = 0, month = 0, day = 0;
104 
105  GetDate(year, month, day);
106  SetDate(value, month, day);
107 }
108 
109 inline int Date::GetMonth() const
110 {
111  int year = 0, month = 0, day = 0;
112 
113  GetDate(year, month, day);
114 
115  return month;
116 }
117 
118 inline void Date::SetMonth(int value)
119 {
120  int year = 0, month = 0, day = 0;
121 
122  GetDate(year, month, day);
123  SetDate(year, value, day);
124 }
125 
126 inline int Date::GetDay() const
127 {
128  int year = 0, month = 0, day = 0;
129 
130  GetDate(year, month, day);
131 
132  return day;
133 }
134 
135 inline void Date::SetDay(int value)
136 {
137  int year = 0, month = 0, day = 0;
138 
139  GetDate(year, month, day);
140  SetDate(year, month, value);
141 }
142 
143 inline int Date::GetHours() const
144 {
145  int hour = 0, minutes = 0, seconds = 0;
146 
147  GetTime(hour, minutes, seconds);
148 
149  return hour;
150 }
151 
152 inline void Date::SetHours(int value)
153 {
154  int hour = 0, minutes = 0, seconds = 0;
155 
156  GetTime(hour, minutes, seconds);
157  SetTime(value, minutes, seconds);
158 }
159 
160 inline int Date::GetMinutes() const
161 {
162  int hour = 0, minutes = 0, seconds = 0;
163 
164  GetTime(hour, minutes, seconds);
165 
166  return minutes;
167 }
168 
169 inline void Date::SetMinutes(int value)
170 {
171  int hour = 0, minutes = 0, seconds = 0;
172 
173  GetTime(hour, minutes, seconds);
174  SetTime(hour, value, seconds);
175 }
176 
177 inline int Date::GetSeconds() const
178 {
179  int hour = 0, minutes = 0, seconds = 0;
180 
181  GetTime(hour, minutes, seconds);
182 
183  return seconds;
184 }
185 
186 inline void Date::SetSeconds(int value)
187 {
188  int hour = 0, minutes = 0, seconds = 0;
189 
190  GetTime(hour, minutes, seconds);
191  SetTime(hour, minutes, value);
192 }
193 
194 inline int Date::DaysBetween(const Date& other) const
195 {
196  return core::Check(OCI_DateDaysBetween(*this, other));
197 }
198 
199 inline void Date::SetDate(int year, int month, int day)
200 {
201  core::Check(OCI_DateSetDate(*this, year, month, day));
202 }
203 
204 inline void Date::SetTime(int hour, int min, int sec)
205 {
206  core::Check(OCI_DateSetTime(*this, hour, min , sec));
207 }
208 
209 inline void Date::SetDateTime(int year, int month, int day, int hour, int min, int sec)
210 {
211  core::Check(OCI_DateSetDateTime(*this, year, month, day, hour, min , sec));
212 }
213 
214 inline void Date::GetDate(int &year, int &month, int &day) const
215 {
216  core::Check(OCI_DateGetDate(*this, &year, &month, &day));
217 }
218 
219 inline void Date::GetTime(int &hour, int &min, int &sec) const
220 {
221  core::Check(OCI_DateGetTime(*this, &hour, &min , &sec));
222 }
223 
224 inline void Date::GetDateTime(int &year, int &month, int &day, int &hour, int &min, int &sec) const
225 {
226  core::Check(OCI_DateGetDateTime(*this, &year, &month, &day, &hour, &min , &sec));
227 }
228 
229 inline void Date::AddDays(int days)
230 {
231  core::Check(OCI_DateAddDays(*this, days));
232 }
233 
234 inline void Date::AddMonths(int months)
235 {
236  OCI_DateAddMonths(*this, months);
237 }
238 
239 inline Date Date::NextDay(const ostring& day) const
240 {
241  Date result = Clone();
242 
243  core::Check(OCI_DateNextDay(result, day.c_str()));
244 
245  return result;
246 }
247 
248 inline Date Date::LastDay() const
249 {
250  Date result = Clone();
251 
252  core::Check(OCI_DateLastDay(result));
253 
254  return result;
255 }
256 
257 inline void Date::ChangeTimeZone(const ostring& tzSrc, const ostring& tzDst)
258 {
259  core::Check(OCI_DateZoneToZone(*this, tzSrc.c_str(), tzDst.c_str()));
260 }
261 
262 inline void Date::FromString(const ostring& str, const ostring& format)
263 {
264  core::Check(OCI_DateFromText(*this, str.c_str(), format.empty() ? Environment::GetFormat(FormatDate).c_str() : format.c_str()));
265 }
266 
267 inline ostring Date::ToString(const ostring& format) const
268 {
269  if (!IsNull())
270  {
271  const size_t size = OCI_SIZE_BUFFER;
272 
273  core::ManagedBuffer<otext> buffer(static_cast<size_t>(size + 1));
274 
275  core::Check(OCI_DateToText(*this, format.c_str(), static_cast<int>(size), buffer));
276 
277  return core::MakeString(static_cast<const otext *>(buffer));
278  }
279 
280  return OCI_STRING_NULL;
281 }
282 
283 inline ostring Date::ToString() const
284 {
286 }
287 
289 {
290  return *this += 1;
291 }
292 
294 {
295  Date result = Clone();
296 
297  *this += 1;
298 
299  return result;
300 }
301 
303 {
304  return *this -= 1;
305 }
306 
308 {
309  Date result = Clone();
310 
311  *this -= 1;
312 
313  return result;
314 }
315 
316 inline Date Date::operator + (int value) const
317 {
318  Date result = Clone();
319  return result += value;
320 }
321 
322 inline Date Date::operator - (int value) const
323 {
324  Date result = Clone();
325  return result -= value;
326 }
327 
328 inline Date& Date::operator += (int value)
329 {
330  AddDays(value);
331  return *this;
332 }
333 
334 inline Date& Date::operator -= (int value)
335 {
336  AddDays(-value);
337  return *this;
338 }
339 
340 inline bool Date::operator == (const Date& other) const
341 {
342  return Compare(other) == 0;
343 }
344 
345 inline bool Date::operator != (const Date& other) const
346 {
347  return !(*this == other);
348 }
349 
350 inline bool Date::operator > (const Date& other) const
351 {
352  return Compare(other) > 0;
353 }
354 
355 inline bool Date::operator < (const Date& other) const
356 {
357  return Compare(other) < 0;
358 }
359 
360 inline bool Date::operator >= (const Date& other) const
361 {
362  const int res = Compare(other);
363 
364  return res == 0 || res > 0;
365 }
366 
367 inline bool Date::operator <= (const Date& other) const
368 {
369  const int res = Compare(other);
370 
371  return res == 0 || res < 0;
372 }
373 
374 }
bool operator<(const Date &other) const
Indicates if the current date value is inferior to the given date value.
Definition: Date.hpp:355
bool IsValid() const
Check if the given date is valid.
Definition: Date.hpp:87
Internal usage. Interface for handling ownership and relationship of a C API handle.
Definition: core.hpp:312
void SetMinutes(int value)
Set the date minutes value.
Definition: Date.hpp:169
Date operator+(int value) const
Return a new date holding the current date value incremented by the given number of days...
Definition: Date.hpp:316
OCILIB ++ Namespace.
void FromString(const ostring &str, const ostring &format=OTEXT(""))
Assign to the date object the value provided by the input date time string.
Definition: Date.hpp:262
void ChangeTimeZone(const ostring &tzSrc, const ostring &tzDst)
Convert the date from one zone to another zone.
Definition: Date.hpp:257
OCI_SYM_PUBLIC boolean OCI_API OCI_DateGetDateTime(OCI_Date *date, int *year, int *month, int *day, int *hour, int *min, int *sec)
Extract the date and time parts from a date handle.
Date LastDay() const
Return the last day of month from the current date object.
Definition: Date.hpp:248
Date NextDay(const ostring &day) const
Return the date of next day of the week, after the current date object.
Definition: Date.hpp:239
Date(bool create=false)
Create an empty null Date object.
Definition: Date.hpp:28
int GetMinutes() const
Return the date minutes value.
Definition: Date.hpp:160
OCI_SYM_PUBLIC boolean OCI_API OCI_DateZoneToZone(OCI_Date *date, const otext *zone1, const otext *zone2)
Convert a date from one zone to another zone.
static ostring GetFormat(FormatType formatType)
Return the format string for implicit string conversions of the given type.
bool operator<=(const Date &other) const
Indicates if the current date value is inferior or equal to the given date value. ...
Definition: Date.hpp:367
int DaysBetween(const Date &other) const
Return the number of days with the given date.
Definition: Date.hpp:194
static T Check(T result)
Internal usage. Checks if the last OCILIB function call has raised an error. If so, it raises a C++ exception using the retrieved error handle.
Definition: Utils.hpp:53
OCI_SYM_PUBLIC boolean OCI_API OCI_DateSetTime(OCI_Date *date, int hour, int min, int sec)
Set the time portion if the given date handle.
int GetHours() const
Return the date hours value.
Definition: Date.hpp:143
int GetDay() const
Return the date day value.
Definition: Date.hpp:126
void SetSeconds(int value)
Set the date seconds value.
Definition: Date.hpp:186
OCI_SYM_PUBLIC OCI_Date *OCI_API OCI_DateCreate(OCI_Connection *con)
Create a local date object.
int GetMonth() const
Return the date month value.
Definition: Date.hpp:109
void SetDateTime(int year, int month, int day, int hour, int min, int sec)
Set the date and time part.
Definition: Date.hpp:209
void SetHours(int value)
Set the date hours value.
Definition: Date.hpp:152
ostring MakeString(const otext *result, int size=-1)
Internal usage. Constructs a C++ string object from the given OCILIB string pointer.
Definition: Utils.hpp:65
void SetDay(int value)
Set the date day value.
Definition: Date.hpp:135
static Date SysDate()
Return the current system date time.
Definition: Date.hpp:60
void SetDate(int year, int month, int day)
Set the date part.
Definition: Date.hpp:199
OCI_SYM_PUBLIC boolean OCI_API OCI_DateGetDate(OCI_Date *date, int *year, int *month, int *day)
Extract the date part from a date handle.
OCI_SYM_PUBLIC boolean OCI_API OCI_DateGetTime(OCI_Date *date, int *hour, int *min, int *sec)
Extract the time part from a date handle.
OCI_SYM_PUBLIC int OCI_API OCI_DateCheck(OCI_Date *date)
Check if the given date is valid.
ostring ToString() const override
Convert the date value to a string using default format OCI_STRING_FORMAT_DATE.
Definition: Date.hpp:283
void SetMonth(int value)
Set the date month value.
Definition: Date.hpp:118
struct OCI_Date OCI_Date
Oracle internal date representation.
Definition: types.h:267
OCI_SYM_PUBLIC int OCI_API OCI_DateAssign(OCI_Date *date, OCI_Date *date_src)
Assign the value of a date handle to another one.
OCI_SYM_PUBLIC boolean OCI_API OCI_DateAddDays(OCI_Date *date, int nb)
Add or subtract days to a date handle.
OCI_SYM_PUBLIC int OCI_API OCI_DateCompare(OCI_Date *date, OCI_Date *date2)
Compares two date handles.
Internal usage. Provide a buffer class with RAII capabilities.
Definition: core.hpp:196
int GetSeconds() const
Return the date seconds value.
Definition: Date.hpp:177
bool operator!=(const Date &other) const
Indicates if the current date value is not equal the given date value.
Definition: Date.hpp:345
void AddDays(int days)
Add or subtract days.
Definition: Date.hpp:229
void GetDate(int &year, int &month, int &day) const
Extract the date parts.
Definition: Date.hpp:214
OCI_SYM_PUBLIC boolean OCI_API OCI_DateNextDay(OCI_Date *date, const otext *day)
Gets the date of next day of the week, after a given date.
bool operator>=(const Date &other) const
Indicates if the current date value is superior or equal to the given date value. ...
Definition: Date.hpp:360
void SetTime(int hour, int min, int sec)
Set the time part.
Definition: Date.hpp:204
bool operator==(const Date &other) const
Indicates if the current date value is equal to the given date value.
Definition: Date.hpp:340
void GetTime(int &hour, int &min, int &sec) const
Extract time parts.
Definition: Date.hpp:219
void GetDateTime(int &year, int &month, int &day, int &hour, int &min, int &sec) const
Extract the date and time parts.
Definition: Date.hpp:224
Date & operator--()
Decrement the date by 1 day.
Definition: Date.hpp:302
OCI_SYM_PUBLIC boolean OCI_API OCI_DateToText(OCI_Date *date, const otext *fmt, int size, otext *str)
Convert a Date value from the given date handle to a string.
OCI_SYM_PUBLIC boolean OCI_API OCI_DateFree(OCI_Date *date)
Free a date object.
Date Clone() const
Clone the current instance to a new one performing deep copy.
Definition: Date.hpp:71
Date operator-(int value) const
Return a new date holding the current date value decremented by the given number of days...
Definition: Date.hpp:322
Date & operator-=(int value)
Decrement the date by the given number of days.
Definition: Date.hpp:334
Date & operator+=(int value)
Increment the date by the given number of days.
Definition: Date.hpp:328
OCI_SYM_PUBLIC boolean OCI_API OCI_DateSetDate(OCI_Date *date, int year, int month, int day)
Set the date portion if the given date handle.
OCI_SYM_PUBLIC boolean OCI_API OCI_DateLastDay(OCI_Date *date)
Place the last day of month (from the given date) into the given date.
int GetYear() const
Return the date year value.
Definition: Date.hpp:92
bool operator>(const Date &other) const
Indicates if the current date value is superior to the given date value.
Definition: Date.hpp:350
OCI_SYM_PUBLIC boolean OCI_API OCI_DateAddMonths(OCI_Date *date, int nb)
Add or subtract months to a date handle.
void SetYear(int value)
Set the date year value.
Definition: Date.hpp:101
Date & operator++()
Increment the date by 1 day.
Definition: Date.hpp:288
OCI_SYM_PUBLIC int OCI_API OCI_DateDaysBetween(OCI_Date *date, OCI_Date *date2)
Return the number of days betWeen two dates.
std::basic_string< otext, std::char_traits< otext >, std::allocator< otext > > ostring
string class wrapping the OCILIB otext * type and OTEXT() macros ( see Character sets ) ...
Definition: config.hpp:120
void AddMonths(int months)
Add or subtract months.
Definition: Date.hpp:234
OCI_SYM_PUBLIC boolean OCI_API OCI_DateSetDateTime(OCI_Date *date, int year, int month, int day, int hour, int min, int sec)
Set the date and time portions if the given date handle.
OCI_SYM_PUBLIC boolean OCI_API OCI_DateFromText(OCI_Date *date, const otext *str, const otext *fmt)
Convert a string to a date and store it in the given date handle.
OCI_SYM_PUBLIC boolean OCI_API OCI_DateSysDate(OCI_Date *date)
Return the current system date/time into the date handle.
Object identifying the SQL data type DATE.
Definition: types.hpp:2655