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