OCILIB (C and C++ Driver for Oracle)  4.7.3
Open source and cross platform Oracle Driver delivering efficient access to Oracle databases.
Timestamp.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 // ReSharper disable CppClangTidyHicppUseEqualsDefault
26 // ReSharper disable CppClangTidyModernizeUseEqualsDefault
27 
28 namespace ocilib
29 {
30 
32 {
33 }
34 
36 {
37  Acquire(core::Check(OCI_TimestampCreate(nullptr, type)), reinterpret_cast<HandleFreeFunc>(OCI_TimestampFree), nullptr, nullptr);
38 }
39 
40 inline Timestamp::Timestamp(TimestampType type, const ostring& data, const ostring& format)
41 {
42  Acquire(core::Check(OCI_TimestampCreate(nullptr, type)), reinterpret_cast<HandleFreeFunc>(OCI_TimestampFree), nullptr, nullptr);
43  FromString(data, format);
44 }
45 
46 inline Timestamp::Timestamp(OCI_Timestamp *pTimestamp, core::Handle *parent)
47 {
48  Acquire(pTimestamp, nullptr, nullptr, parent);
49 }
50 
52 {
53  Timestamp result(GetType());
54 
55  core::Check(OCI_TimestampAssign(result, *this));
56 
57  return result;
58 }
59 
60 inline int Timestamp::Compare(const Timestamp& other) const
61 {
62  return core::Check(OCI_TimestampCompare(*this, other));
63 }
64 
66 {
67  return TimestampType(static_cast<TimestampType::Type>(core::Check(OCI_TimestampGetType(*this))));
68 }
69 
70 inline void Timestamp::SetDateTime(int year, int month, int day, int hour, int min, int sec, int fsec, const ostring& timeZone)
71 {
72  core::Check(OCI_TimestampConstruct(*this, year, month, day, hour, min,sec, fsec, timeZone.c_str()));
73 }
74 
75 inline void Timestamp::Convert(const Timestamp& other)
76 {
77  core::Check(OCI_TimestampConvert(*this, other));
78 }
79 
80 inline bool Timestamp::IsValid() const
81 {
82  return (core::Check(OCI_TimestampCheck(*this)) == 0);
83 }
84 
85 inline int Timestamp::GetYear() const
86 {
87  int year, month, day;
88 
89  GetDate(year, month, day);
90 
91  return year;
92 }
93 
94 inline void Timestamp::SetYear(int value)
95 {
96  int year, month, day;
97 
98  GetDate(year, month, day);
99  SetDate(value, month, day);
100 }
101 
102 inline int Timestamp::GetMonth() const
103 {
104  int year, month, day;
105 
106  GetDate(year, month, day);
107 
108  return month;
109 }
110 
111 inline void Timestamp::SetMonth(int value)
112 {
113  int year, month, day;
114 
115  GetDate(year, month, day);
116  SetDate(year, value, day);
117 }
118 
119 inline int Timestamp::GetDay() const
120 {
121  int year, month, day;
122 
123  GetDate(year, month, day);
124 
125  return day;
126 }
127 
128 inline void Timestamp::SetDay(int value)
129 {
130  int year, month, day;
131 
132  GetDate(year, month, day);
133  SetDate(year, month, value);
134 }
135 
136 inline int Timestamp::GetHours() const
137 {
138  int hour, minutes, seconds, milliseconds;
139 
140  GetTime(hour, minutes, seconds, milliseconds);
141 
142  return hour;
143 }
144 
145 inline void Timestamp::SetHours(int value)
146 {
147  int hour, minutes, seconds, milliseconds;
148 
149  GetTime(hour, minutes, seconds, milliseconds);
150  SetTime(value, minutes, seconds, milliseconds);
151 }
152 
153 inline int Timestamp::GetMinutes() const
154 {
155  int hour, minutes, seconds, milliseconds;
156 
157  GetTime(hour, minutes, seconds, milliseconds);
158 
159  return minutes;
160 }
161 
162 inline void Timestamp::SetMinutes(int value)
163 {
164  int hour = 0, minutes = 0, seconds = 0, milliseconds = 0;
165 
166  GetTime(hour, minutes, seconds, milliseconds);
167  SetTime(hour, value, seconds, milliseconds);
168 }
169 
170 inline int Timestamp::GetSeconds() const
171 {
172  int hour = 0, minutes = 0, seconds = 0, milliseconds = 0;
173 
174  GetTime(hour, minutes, seconds, milliseconds);
175 
176  return seconds;
177 }
178 
179 inline void Timestamp::SetSeconds(int value)
180 {
181  int hour = 0, minutes = 0, seconds = 0, milliseconds = 0;
182 
183  GetTime(hour, minutes, seconds, milliseconds);
184  SetTime(hour, minutes, value, milliseconds);
185 }
186 
187 inline int Timestamp::GetMilliSeconds() const
188 {
189  int hour = 0, minutes = 0, seconds = 0, milliseconds = 0;
190 
191  GetTime(hour, minutes, seconds, milliseconds);
192 
193  return milliseconds;
194 }
195 
196 inline void Timestamp::SetMilliSeconds(int value)
197 {
198  int hour = 0, minutes = 0, seconds = 0, milliseconds = 0;
199 
200  GetTime(hour, minutes, seconds, milliseconds);
201  SetTime(hour, minutes, seconds, value);
202 }
203 
204 inline void Timestamp::GetDate(int &year, int &month, int &day) const
205 {
206  core::Check(OCI_TimestampGetDate(*this, &year, &month, &day));
207 }
208 
209 inline void Timestamp::GetTime(int &hour, int &min, int &sec, int &fsec) const
210 {
211  core::Check(OCI_TimestampGetTime(*this, &hour, &min, &sec, &fsec));
212 }
213 
214 inline void Timestamp::GetDateTime(int &year, int &month, int &day, int &hour, int &min, int &sec, int &fsec) const
215 {
216  core::Check(OCI_TimestampGetDateTime(*this, &year, &month, &day, &hour, &min, &sec, &fsec));
217 }
218 
219 inline void Timestamp::SetDate(int year, int month, int day)
220 {
221  int tmpYear = 0, tmpMonth = 0, tempDay = 0, hour = 0, minutes = 0, seconds = 0, milliseconds = 0;
222 
223  GetDateTime(tmpYear, tmpMonth, tempDay, hour, minutes, seconds, milliseconds);
224  SetDateTime(year, month, day, hour, minutes, seconds, milliseconds);
225 }
226 
227 inline void Timestamp::SetTime(int hour, int min, int sec, int fsec)
228 {
229  int year = 0, month = 0, day = 0, tmpHour = 0, tmpMinutes = 0, tmpSeconds = 0, tmpMilliseconds = 0;
230 
231  GetDateTime(year, month, day, tmpHour, tmpMinutes, tmpSeconds, tmpMilliseconds);
232  SetDateTime(year, month, day, hour, min, sec, fsec);
233 }
234 
235 inline void Timestamp::SetTimeZone(const ostring& timeZone)
236 {
237  if (GetType() == WithTimeZone)
238  {
239  int year = 0, month = 0, day = 0, hour = 0, minutes = 0, seconds = 0, milliseconds = 0;
240 
241  GetDateTime(year, month, day, hour, minutes, seconds, milliseconds);
242  SetDateTime(year, month, day, hour, minutes, seconds, milliseconds, timeZone);
243  }
244 }
245 
247 {
248  if (GetType() != NoTimeZone)
249  {
250  const size_t size = OCI_SIZE_BUFFER;
251 
252  core::ManagedBuffer<otext> buffer(static_cast<size_t>(size + 1));
253 
254  core::Check(OCI_TimestampGetTimeZoneName(*this, static_cast<int>(size), buffer) == TRUE);
255 
256  return core::MakeString(static_cast<const otext *>(buffer));
257  }
258 
259  return ostring();
260 }
261 
262 inline void Timestamp::GetTimeZoneOffset(int &hour, int &min) const
263 {
264  core::Check(OCI_TimestampGetTimeZoneOffset(*this, &hour, &min));
265 }
266 
267 inline void Timestamp::Substract(const Timestamp &lsh, const Timestamp &rsh, Interval& result)
268 {
269  core::Check(OCI_TimestampSubtract(lsh, rsh, result));
270 }
271 
273 {
274  Timestamp result(type);
275 
277 
278  return result;
279 }
280 
281 inline void Timestamp::FromString(const ostring& data, const ostring& format)
282 {
283  core::Check(OCI_TimestampFromText(*this, data.c_str(), format.empty() ? Environment::GetFormat(FormatTimestamp).c_str() : format.c_str()));
284 }
285 
286 inline ostring Timestamp::ToString(const ostring& format, int precision = OCI_STRING_DEFAULT_PREC) const
287 {
288  if (!IsNull())
289  {
290  const size_t size = OCI_SIZE_BUFFER;
291 
292  core::ManagedBuffer<otext> buffer(static_cast<size_t>(size + 1));
293 
294  core::Check(OCI_TimestampToText(*this, format.c_str(), static_cast<int>(size), buffer, precision));
295 
296  return core::MakeString(static_cast<const otext *>(buffer));
297  }
298 
299  return OCI_STRING_NULL;
300 }
301 
303 {
304  return ToString(Environment::GetFormat(FormatTimestamp), OCI_STRING_DEFAULT_PREC);
305 }
306 
308 {
309  return *this += 1;
310 }
311 
313 {
314  Timestamp result = Clone();
315 
316  *this += 1;
317 
318  return result;
319 }
320 
322 {
323  return *this -= 1;
324 }
325 
327 {
328  Timestamp result = Clone();
329 
330  *this -= 1;
331 
332  return result;
333 }
334 
335 inline Timestamp Timestamp::operator + (int value) const
336 {
337  Timestamp result = Clone();
338  Interval interval(Interval::DaySecond);
339  interval.SetDay(1);
340  return result += value;
341 }
342 
343 inline Timestamp Timestamp::operator - (int value) const
344 {
345  Timestamp result = Clone();
346  Interval interval(Interval::DaySecond);
347  interval.SetDay(1);
348  return result -= value;
349 }
350 
352 {
353  Interval interval(Interval::DaySecond);
354  core::Check(OCI_TimestampSubtract(*this, other, interval));
355  return interval;
356 }
357 
358 inline Timestamp Timestamp::operator + (const Interval& other) const
359 {
360  Timestamp result = Clone();
361  return result += other;
362 }
363 
364 inline Timestamp Timestamp::operator - (const Interval& other) const
365 {
366  Timestamp result = Clone();
367  return result -= other;
368 }
369 
371 {
372  core::Check(OCI_TimestampIntervalAdd(*this, other));
373  return *this;
374 }
375 
377 {
378  core::Check(OCI_TimestampIntervalSub(*this, other));
379  return *this;
380 }
381 
383 {
384  Interval interval(Interval::DaySecond);
385  interval.SetDay(value);
386  return *this += interval;
387 }
388 
390 {
391  Interval interval(Interval::DaySecond);
392  interval.SetDay(value);
393  return *this -= interval;
394 }
395 
396 inline bool Timestamp::operator == (const Timestamp& other) const
397 {
398  return Compare(other) == 0;
399 }
400 
401 inline bool Timestamp::operator != (const Timestamp& other) const
402 {
403  return (!(*this == other));
404 }
405 
406 inline bool Timestamp::operator > (const Timestamp& other) const
407 {
408  return (Compare(other) > 0);
409 }
410 
411 inline bool Timestamp::operator < (const Timestamp& other) const
412 {
413  return (Compare(other) < 0);
414 }
415 
416 inline bool Timestamp::operator >= (const Timestamp& other) const
417 {
418  const int res = Compare(other);
419 
420  return (res == 0 || res < 0);
421 }
422 
423 inline bool Timestamp::operator <= (const Timestamp& other) const
424 {
425  const int res = Compare(other);
426 
427  return (res == 0 || res > 0);
428 }
429 
430 }
TimestampType GetType() const
Return the type of the given timestamp object.
Definition: Timestamp.hpp:65
OCI_SYM_PUBLIC boolean OCI_API OCI_TimestampGetDateTime(OCI_Timestamp *tmsp, int *year, int *month, int *day, int *hour, int *min, int *sec, int *fsec)
Extract the date and time parts from a date handle.
bool operator>(const Timestamp &other) const
Indicates if the current Timestamp value is superior to the given Timestamp value.
Definition: Timestamp.hpp:406
OCI_SYM_PUBLIC boolean OCI_API OCI_TimestampFree(OCI_Timestamp *tmsp)
Free an OCI_Timestamp handle.
Timestamp & operator-=(int value)
Decrement the Timestamp by the given number of days.
Definition: Timestamp.hpp:389
Internal usage. Interface for handling ownership and relationship of a C API handle.
Definition: core.hpp:312
int GetYear() const
Return the timestamp year value.
Definition: Timestamp.hpp:85
void GetDateTime(int &year, int &month, int &day, int &hour, int &min, int &sec, int &fsec) const
Extract date and time parts.
Definition: Timestamp.hpp:214
OCI_SYM_PUBLIC boolean OCI_API OCI_TimestampConvert(OCI_Timestamp *tmsp, OCI_Timestamp *tmsp_src)
Convert one timestamp value from one type to another.
int GetSeconds() const
Return the timestamp seconds value.
Definition: Timestamp.hpp:170
OCI_SYM_PUBLIC boolean OCI_API OCI_TimestampToText(OCI_Timestamp *tmsp, const otext *fmt, int size, otext *str, int precision)
Convert a timestamp value from the given timestamp handle to a string.
OCILIB ++ Namespace.
int GetMonth() const
Return the timestamp month value.
Definition: Timestamp.hpp:102
void SetHours(int value)
Set the timestamp hours value.
Definition: Timestamp.hpp:145
Timestamp Clone() const
Clone the current instance to a new one performing deep copy.
Definition: Timestamp.hpp:51
static ostring GetFormat(FormatType formatType)
Return the format string for implicit string conversions of the given type.
void Convert(const Timestamp &other)
Convert the current timestamp to the type of the given timestamp.
Definition: Timestamp.hpp:75
core::Enum< TimestampTypeValues > TimestampType
Type of timestamp.
Definition: types.hpp:3525
OCI_SYM_PUBLIC unsigned int OCI_API OCI_TimestampGetType(OCI_Timestamp *tmsp)
Return the type of the given Timestamp object.
Timestamp & operator+=(int value)
Increment the Timestamp by the given number of days.
Definition: Timestamp.hpp:382
OCI_SYM_PUBLIC int OCI_API OCI_TimestampCheck(OCI_Timestamp *tmsp)
Check if the given timestamp is valid.
bool operator!=(const Timestamp &other) const
Indicates if the current Timestamp value is not equal the given Timestamp value.
Definition: Timestamp.hpp:401
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
int GetMinutes() const
Return the timestamp minutes value.
Definition: Timestamp.hpp:153
bool operator<(const Timestamp &other) const
Indicates if the current Timestamp value is inferior to the given Timestamp value.
Definition: Timestamp.hpp:411
bool operator>=(const Timestamp &other) const
Indicates if the current Timestamp value is superior or equal to the given Timestamp value...
Definition: Timestamp.hpp:416
OCI_SYM_PUBLIC int OCI_API OCI_TimestampCompare(OCI_Timestamp *tmsp, OCI_Timestamp *tmsp2)
Compares two timestamp handles.
struct OCI_Timestamp OCI_Timestamp
Oracle internal timestamp representation.
Definition: types.h:277
OCI_SYM_PUBLIC boolean OCI_API OCI_TimestampIntervalAdd(OCI_Timestamp *tmsp, OCI_Interval *itv)
Add an interval value to a timestamp value of a timestamp handle.
Timestamp & operator++()
Increment the timestamp by 1 day.
Definition: Timestamp.hpp:307
Timestamp operator-(int value) const
Return a new Timestamp holding the current Timestamp value decremented by the given number of days...
Definition: Timestamp.hpp:343
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
OCI_SYM_PUBLIC boolean OCI_API OCI_TimestampGetDate(OCI_Timestamp *tmsp, int *year, int *month, int *day)
Extract the date part from a timestamp handle.
void FromString(const ostring &data, const ostring &format=OCI_STRING_FORMAT_DATE)
Assign to the timestamp object the value provided by the input date time string.
Definition: Timestamp.hpp:281
void SetDateTime(int year, int month, int day, int hour, int min, int sec, int fsec, const ostring &timeZone=OTEXT(""))
Set the timestamp value from given date time parts.
Definition: Timestamp.hpp:70
OCI_SYM_PUBLIC OCI_Timestamp *OCI_API OCI_TimestampCreate(OCI_Connection *con, unsigned int type)
Create a local Timestamp instance.
void SetMinutes(int value)
Set the timestamp minutes value.
Definition: Timestamp.hpp:162
void GetDate(int &year, int &month, int &day) const
Extract the date parts.
Definition: Timestamp.hpp:204
OCI_SYM_PUBLIC boolean OCI_API OCI_TimestampGetTime(OCI_Timestamp *tmsp, int *hour, int *min, int *sec, int *fsec)
Extract the time portion from a timestamp handle.
Template Enumeration template class providing some type safety to some extends for manipulating enume...
Definition: core.hpp:117
OCI_SYM_PUBLIC boolean OCI_API OCI_TimestampAssign(OCI_Timestamp *tmsp, OCI_Timestamp *tmsp_src)
Assign the value of a timestamp handle to another one.
OCI_SYM_PUBLIC boolean OCI_API OCI_TimestampGetTimeZoneName(OCI_Timestamp *tmsp, int size, otext *str)
Return the time zone name of a timestamp handle.
Timestamp & operator--()
Decrement the Timestamp by 1 day.
Definition: Timestamp.hpp:321
bool operator<=(const Timestamp &other) const
Indicates if the current Timestamp value is inferior or equal to the given Timestamp value...
Definition: Timestamp.hpp:423
void SetTimeZone(const ostring &timeZone)
Set the given time zone to the timestamp.
Definition: Timestamp.hpp:235
Object identifying the SQL data type INTERVAL.
Definition: types.hpp:3091
ostring GetTimeZone() const
Return the name of the current time zone.
Definition: Timestamp.hpp:246
Internal usage. Provide a buffer class with RAII capabilities.
Definition: core.hpp:196
void SetSeconds(int value)
Set the timestamp seconds value.
Definition: Timestamp.hpp:179
void SetDay(int value)
Set the timestamp day value.
Definition: Timestamp.hpp:128
OCI_SYM_PUBLIC boolean OCI_API OCI_TimestampSubtract(OCI_Timestamp *tmsp, OCI_Timestamp *tmsp2, OCI_Interval *itv)
Store the difference of two timestamp handles into an interval handle.
OCI_SYM_PUBLIC boolean OCI_API OCI_TimestampFromText(OCI_Timestamp *tmsp, const otext *str, const otext *fmt)
Convert a string to a timestamp and store it in the given timestamp handle.
void SetMilliSeconds(int value)
Set the timestamp milliseconds value.
Definition: Timestamp.hpp:196
bool operator==(const Timestamp &other) const
Indicates if the current Timestamp value is equal to the given Timestamp value.
Definition: Timestamp.hpp:396
static void Substract(const Timestamp &lsh, const Timestamp &rsh, Interval &result)
Subtract the given two timestamp and store the result into the given Interval.
Definition: Timestamp.hpp:267
ostring ToString() const override
Convert the timestamp value to a string using default date format and no precision.
Definition: Timestamp.hpp:302
void SetMonth(int value)
Set the timestamp month value.
Definition: Timestamp.hpp:111
OCI_SYM_PUBLIC boolean OCI_API OCI_TimestampConstruct(OCI_Timestamp *tmsp, int year, int month, int day, int hour, int min, int sec, int fsec, const otext *time_zone)
Set a timestamp handle value.
void SetDate(int year, int month, int day)
Set the date part.
Definition: Timestamp.hpp:219
Timestamp()
Create an empty null timestamp instance.
Definition: Timestamp.hpp:31
void GetTime(int &hour, int &min, int &sec, int &fsec) const
Extract time parts.
Definition: Timestamp.hpp:209
void SetDay(int value)
Set the interval day value.
Definition: Interval.hpp:119
void SetYear(int value)
Set the timestamp year value.
Definition: Timestamp.hpp:94
int GetMilliSeconds() const
Return the timestamp seconds value.
Definition: Timestamp.hpp:187
OCI_SYM_PUBLIC boolean OCI_API OCI_TimestampSysTimestamp(OCI_Timestamp *tmsp)
Stores the system current date and time as a timestamp value with time zone into the timestamp handle...
void SetTime(int hour, int min, int sec, int fsec)
Set the time part.
Definition: Timestamp.hpp:227
static Timestamp SysTimestamp(TimestampType type=NoTimeZone)
return the current system timestamp
Definition: Timestamp.hpp:272
bool IsValid() const
Check if the given timestamp is valid.
Definition: Timestamp.hpp:80
Timestamp operator+(int value) const
Return a new Timestamp holding the current Timestamp value incremented by the given number of days...
Definition: Timestamp.hpp:335
OCI_SYM_PUBLIC boolean OCI_API OCI_TimestampIntervalSub(OCI_Timestamp *tmsp, OCI_Interval *itv)
Subtract an interval value from a timestamp value of a timestamp handle.
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
int GetDay() const
Return the timestamp day value.
Definition: Timestamp.hpp:119
Object identifying the SQL data type TIMESTAMP.
Definition: types.hpp:3490
OCI_SYM_PUBLIC boolean OCI_API OCI_TimestampGetTimeZoneOffset(OCI_Timestamp *tmsp, int *hour, int *min)
Return the time zone (hour, minute) portion of a timestamp handle.
void GetTimeZoneOffset(int &hour, int &min) const
Return the time zone (hour, minute) offsets.
Definition: Timestamp.hpp:262
int GetHours() const
Return the timestamp hours value.
Definition: Timestamp.hpp:136