OCILIB (C and C++ Driver for Oracle)  4.7.6
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-2023 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  AcquireTransient(pDate, parent);
53 }
54 
55 inline void Date::Allocate()
56 {
57  AcquireAllocated
58  (
59  core::Check(OCI_DateCreate(nullptr)),
60  Environment::GetEnvironmentHandle()
61  );
62 }
63 
65 {
66  Date result;
67 
68  result.Allocate();
69 
71 
72  return result;
73 }
74 
75 inline Date Date::Clone() const
76 {
77  Date result;
78 
79  result.Allocate();
80 
81  core::Check(OCI_DateAssign(result, *this));
82 
83  return result;
84 }
85 
86 inline int Date::Compare(const Date& other) const
87 {
88  return core::Check(OCI_DateCompare(*this, other));
89 }
90 
91 inline bool Date::IsValid() const
92 {
93  return (core::Check(OCI_DateCheck(*this)) == 0);
94 }
95 
96 inline int Date::GetYear() const
97 {
98  int year = 0, month = 0, day = 0;
99 
100  GetDate(year, month, day);
101 
102  return year;
103 }
104 
105 inline void Date::SetYear(int value)
106 {
107  int year = 0, month = 0, day = 0;
108 
109  GetDate(year, month, day);
110  SetDate(value, month, day);
111 }
112 
113 inline int Date::GetMonth() const
114 {
115  int year = 0, month = 0, day = 0;
116 
117  GetDate(year, month, day);
118 
119  return month;
120 }
121 
122 inline void Date::SetMonth(int value)
123 {
124  int year = 0, month = 0, day = 0;
125 
126  GetDate(year, month, day);
127  SetDate(year, value, day);
128 }
129 
130 inline int Date::GetDay() const
131 {
132  int year = 0, month = 0, day = 0;
133 
134  GetDate(year, month, day);
135 
136  return day;
137 }
138 
139 inline void Date::SetDay(int value)
140 {
141  int year = 0, month = 0, day = 0;
142 
143  GetDate(year, month, day);
144  SetDate(year, month, value);
145 }
146 
147 inline int Date::GetHours() const
148 {
149  int hour = 0, minutes = 0, seconds = 0;
150 
151  GetTime(hour, minutes, seconds);
152 
153  return hour;
154 }
155 
156 inline void Date::SetHours(int value)
157 {
158  int hour = 0, minutes = 0, seconds = 0;
159 
160  GetTime(hour, minutes, seconds);
161  SetTime(value, minutes, seconds);
162 }
163 
164 inline int Date::GetMinutes() const
165 {
166  int hour = 0, minutes = 0, seconds = 0;
167 
168  GetTime(hour, minutes, seconds);
169 
170  return minutes;
171 }
172 
173 inline void Date::SetMinutes(int value)
174 {
175  int hour = 0, minutes = 0, seconds = 0;
176 
177  GetTime(hour, minutes, seconds);
178  SetTime(hour, value, seconds);
179 }
180 
181 inline int Date::GetSeconds() const
182 {
183  int hour = 0, minutes = 0, seconds = 0;
184 
185  GetTime(hour, minutes, seconds);
186 
187  return seconds;
188 }
189 
190 inline void Date::SetSeconds(int value)
191 {
192  int hour = 0, minutes = 0, seconds = 0;
193 
194  GetTime(hour, minutes, seconds);
195  SetTime(hour, minutes, value);
196 }
197 
198 inline int Date::DaysBetween(const Date& other) const
199 {
200  return core::Check(OCI_DateDaysBetween(*this, other));
201 }
202 
203 inline void Date::SetDate(int year, int month, int day)
204 {
205  core::Check(OCI_DateSetDate(*this, year, month, day));
206 }
207 
208 inline void Date::SetTime(int hour, int min, int sec)
209 {
210  core::Check(OCI_DateSetTime(*this, hour, min , sec));
211 }
212 
213 inline void Date::SetDateTime(int year, int month, int day, int hour, int min, int sec)
214 {
215  core::Check(OCI_DateSetDateTime(*this, year, month, day, hour, min , sec));
216 }
217 
218 inline void Date::GetDate(int &year, int &month, int &day) const
219 {
220  core::Check(OCI_DateGetDate(*this, &year, &month, &day));
221 }
222 
223 inline void Date::GetTime(int &hour, int &min, int &sec) const
224 {
225  core::Check(OCI_DateGetTime(*this, &hour, &min , &sec));
226 }
227 
228 inline void Date::GetDateTime(int &year, int &month, int &day, int &hour, int &min, int &sec) const
229 {
230  core::Check(OCI_DateGetDateTime(*this, &year, &month, &day, &hour, &min , &sec));
231 }
232 
233 inline void Date::AddDays(int days)
234 {
235  core::Check(OCI_DateAddDays(*this, days));
236 }
237 
238 inline void Date::AddMonths(int months)
239 {
240  OCI_DateAddMonths(*this, months);
241 }
242 
243 inline Date Date::NextDay(const ostring& day) const
244 {
245  Date result = Clone();
246 
247  core::Check(OCI_DateNextDay(result, day.c_str()));
248 
249  return result;
250 }
251 
252 inline Date Date::LastDay() const
253 {
254  Date result = Clone();
255 
256  core::Check(OCI_DateLastDay(result));
257 
258  return result;
259 }
260 
261 inline void Date::ChangeTimeZone(const ostring& tzSrc, const ostring& tzDst)
262 {
263  core::Check(OCI_DateZoneToZone(*this, tzSrc.c_str(), tzDst.c_str()));
264 }
265 
266 inline void Date::FromString(const ostring& str, const ostring& format)
267 {
268  core::Check(OCI_DateFromText(*this, str.c_str(), format.empty() ? Environment::GetFormat(FormatDate).c_str() : format.c_str()));
269 }
270 
271 inline ostring Date::ToString(const ostring& format) const
272 {
273  if (!IsNull())
274  {
275  const size_t size = OCI_SIZE_BUFFER;
276 
277  core::ManagedBuffer<otext> buffer(static_cast<size_t>(size + 1));
278 
279  core::Check(OCI_DateToText(*this, format.c_str(), static_cast<int>(size), buffer));
280 
281  return core::MakeString(static_cast<const otext *>(buffer));
282  }
283 
284  return OCI_STRING_NULL;
285 }
286 
287 inline ostring Date::ToString() const
288 {
290 }
291 
293 {
294  return *this += 1;
295 }
296 
298 {
299  Date result = Clone();
300 
301  *this += 1;
302 
303  return result;
304 }
305 
307 {
308  return *this -= 1;
309 }
310 
312 {
313  Date result = Clone();
314 
315  *this -= 1;
316 
317  return result;
318 }
319 
320 inline Date Date::operator + (int value) const
321 {
322  Date result = Clone();
323  return result += value;
324 }
325 
326 inline Date Date::operator - (int value) const
327 {
328  Date result = Clone();
329  return result -= value;
330 }
331 
332 inline Date& Date::operator += (int value)
333 {
334  AddDays(value);
335  return *this;
336 }
337 
338 inline Date& Date::operator -= (int value)
339 {
340  AddDays(-value);
341  return *this;
342 }
343 
344 inline bool Date::operator == (const Date& other) const
345 {
346  return Compare(other) == 0;
347 }
348 
349 inline bool Date::operator != (const Date& other) const
350 {
351  return !(*this == other);
352 }
353 
354 inline bool Date::operator > (const Date& other) const
355 {
356  return Compare(other) > 0;
357 }
358 
359 inline bool Date::operator < (const Date& other) const
360 {
361  return Compare(other) < 0;
362 }
363 
364 inline bool Date::operator >= (const Date& other) const
365 {
366  const int res = Compare(other);
367 
368  return res == 0 || res > 0;
369 }
370 
371 inline bool Date::operator <= (const Date& other) const
372 {
373  const int res = Compare(other);
374 
375  return res == 0 || res < 0;
376 }
377 
378 }
Object identifying the SQL data type DATE.
Definition: types.hpp:2678
Date & operator-=(int value)
Decrement the date by the given number of days.
Definition: Date.hpp:338
Date NextDay(const ostring &day) const
Return the date of next day of the week, after the current date object.
Definition: Date.hpp:243
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:266
bool operator>=(const Date &other) const
Indicates if the current date value is superior or equal to the given date value.
Definition: Date.hpp:364
bool operator>(const Date &other) const
Indicates if the current date value is superior to the given date value.
Definition: Date.hpp:354
void GetTime(int &hour, int &min, int &sec) const
Extract time parts.
Definition: Date.hpp:223
int GetDay() const
Return the date day value.
Definition: Date.hpp:130
void GetDateTime(int &year, int &month, int &day, int &hour, int &min, int &sec) const
Extract the date and time parts.
Definition: Date.hpp:228
bool operator==(const Date &other) const
Indicates if the current date value is equal to the given date value.
Definition: Date.hpp:344
void SetSeconds(int value)
Set the date seconds value.
Definition: Date.hpp:190
void GetDate(int &year, int &month, int &day) const
Extract the date parts.
Definition: Date.hpp:218
bool operator<(const Date &other) const
Indicates if the current date value is inferior to the given date value.
Definition: Date.hpp:359
void SetDay(int value)
Set the date day value.
Definition: Date.hpp:139
int GetHours() const
Return the date hours value.
Definition: Date.hpp:147
void SetMinutes(int value)
Set the date minutes value.
Definition: Date.hpp:173
static Date SysDate()
Return the current system date time.
Definition: Date.hpp:64
void AddDays(int days)
Add or subtract days.
Definition: Date.hpp:233
bool operator!=(const Date &other) const
Indicates if the current date value is not equal the given date value.
Definition: Date.hpp:349
ostring ToString() const override
Convert the date value to a string using default format OCI_STRING_FORMAT_DATE.
Definition: Date.hpp:287
void SetTime(int hour, int min, int sec)
Set the time part.
Definition: Date.hpp:208
Date operator-(int value) const
Return a new date holding the current date value decremented by the given number of days.
Definition: Date.hpp:326
int GetMinutes() const
Return the date minutes value.
Definition: Date.hpp:164
Date & operator+=(int value)
Increment the date by the given number of days.
Definition: Date.hpp:332
void SetHours(int value)
Set the date hours value.
Definition: Date.hpp:156
bool operator<=(const Date &other) const
Indicates if the current date value is inferior or equal to the given date value.
Definition: Date.hpp:371
int GetMonth() const
Return the date month value.
Definition: Date.hpp:113
Date Clone() const
Clone the current instance to a new one performing deep copy.
Definition: Date.hpp:75
void SetMonth(int value)
Set the date month value.
Definition: Date.hpp:122
void AddMonths(int months)
Add or subtract months.
Definition: Date.hpp:238
int GetSeconds() const
Return the date seconds value.
Definition: Date.hpp:181
Date & operator--()
Decrement the date by 1 day.
Definition: Date.hpp:306
Date LastDay() const
Return the last day of month from the current date object.
Definition: Date.hpp:252
Date operator+(int value) const
Return a new date holding the current date value incremented by the given number of days.
Definition: Date.hpp:320
void SetDate(int year, int month, int day)
Set the date part.
Definition: Date.hpp:203
Date & operator++()
Increment the date by 1 day.
Definition: Date.hpp:292
bool IsValid() const
Check if the given date is valid.
Definition: Date.hpp:91
void SetDateTime(int year, int month, int day, int hour, int min, int sec)
Set the date and time part.
Definition: Date.hpp:213
Date(bool create=false)
Create an empty null Date object.
Definition: Date.hpp:28
int DaysBetween(const Date &other) const
Return the number of days with the given date.
Definition: Date.hpp:198
void SetYear(int value)
Set the date year value.
Definition: Date.hpp:105
int GetYear() const
Return the date year value.
Definition: Date.hpp:96
void ChangeTimeZone(const ostring &tzSrc, const ostring &tzDst)
Convert the date from one zone to another zone.
Definition: Date.hpp:261
static ostring GetFormat(FormatType formatType)
Return the format string for implicit string conversions of the given type.
Internal usage. Interface for handling ownership and relationship of a C API handle.
Definition: core.hpp:325
Internal usage. Provide a buffer class with RAII capabilities.
Definition: core.hpp:197
struct OCI_Date OCI_Date
Oracle internal date representation.
Definition: types.h:279
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_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_DateGetDate(OCI_Date *date, int *year, int *month, int *day)
Extract the date part from a date handle.
OCI_SYM_PUBLIC int OCI_API OCI_DateCompare(OCI_Date *date, OCI_Date *date2)
Compares two date handles.
OCI_SYM_PUBLIC OCI_Date *OCI_API OCI_DateCreate(OCI_Connection *con)
Create a local date object.
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.
OCI_SYM_PUBLIC int OCI_API OCI_DateDaysBetween(OCI_Date *date, OCI_Date *date2)
Return the number of days betWeen two dates.
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_DateSetTime(OCI_Date *date, int hour, int min, int sec)
Set the time portion if the given date handle.
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_DateAddMonths(OCI_Date *date, int nb)
Add or subtract months to a date handle.
OCI_SYM_PUBLIC boolean OCI_API OCI_DateAddDays(OCI_Date *date, int nb)
Add or subtract days to a date handle.
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.
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.
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 int OCI_API OCI_DateCheck(OCI_Date *date)
Check if the given date is valid.
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.
OCI_SYM_PUBLIC boolean OCI_API OCI_DateSysDate(OCI_Date *date)
Return the current system date/time into the 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.
static T Check(T result)
Internal usage. Checks if the last OCILIB function call has raised an error. If so,...
Definition: Utils.hpp:53
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
OCILIB ++ Namespace.
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
@ FormatDate
Definition: types.hpp:329