OCILIB (C Driver for Oracle) 3.9.1
D:/Perso/dev/ocilib/ocilib/src/timestamp.c
00001 /*
00002     +-----------------------------------------------------------------------------------------+
00003     |                                                                                         |
00004     |                               OCILIB - C Driver for Oracle                              |
00005     |                                                                                         |
00006     |                                (C Wrapper for Oracle OCI)                               |
00007     |                                                                                         |
00008     |                              Website : http://www.ocilib.net                            |
00009     |                                                                                         |
00010     |             Copyright (c) 2007-2011 Vincent ROGIER <vince.rogier@ocilib.net>            |
00011     |                                                                                         |
00012     +-----------------------------------------------------------------------------------------+
00013     |                                                                                         |
00014     |             This library is free software; you can redistribute it and/or               |
00015     |             modify it under the terms of the GNU Lesser General Public                  |
00016     |             License as published by the Free Software Foundation; either                |
00017     |             version 2 of the License, or (at your option) any later version.            |
00018     |                                                                                         |
00019     |             This library is distributed in the hope that it will be useful,             |
00020     |             but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00021     |             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU           |
00022     |             Lesser General Public License for more details.                             |
00023     |                                                                                         |
00024     |             You should have received a copy of the GNU Lesser General Public            |
00025     |             License along with this library; if not, write to the Free                  |
00026     |             Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.          |
00027     |                                                                                         |
00028     +-----------------------------------------------------------------------------------------+
00029 */
00030 
00031 /* --------------------------------------------------------------------------------------------- *
00032  * $Id: timestamp.c, v 3.9.1 2011-07-08 00:00 Vincent Rogier $
00033  * --------------------------------------------------------------------------------------------- */
00034 
00035 #include "ocilib_internal.h"
00036 
00037 /* ********************************************************************************************* *
00038  *                             PRIVATE FUNCTIONS
00039  * ********************************************************************************************* */
00040 
00041 /* --------------------------------------------------------------------------------------------- *
00042  * OCI_TimestampInit
00043  * --------------------------------------------------------------------------------------------- */
00044 
00045 OCI_Timestamp * OCI_TimestampInit
00046 (
00047     OCI_Connection *con,
00048     OCI_Timestamp **ptmsp,
00049     OCIDateTime    *buffer,
00050     ub4             type
00051 )
00052 {
00053     OCI_Timestamp *tmsp = NULL;
00054 
00055 #if OCI_VERSION_COMPILE >= OCI_9_0
00056 
00057     boolean res = TRUE;
00058 
00059     OCI_CHECK(ptmsp == NULL, NULL);
00060 
00061     if (*ptmsp == NULL)
00062     {
00063         *ptmsp = (OCI_Timestamp *) OCI_MemAlloc(OCI_IPC_TIMESTAMP, sizeof(*tmsp), (size_t) 1, TRUE);
00064     }
00065 
00066     if (*ptmsp != NULL)
00067     {
00068         tmsp = *ptmsp;
00069 
00070         tmsp->con    = con;
00071         tmsp->handle = buffer;
00072         tmsp->type   = type;
00073 
00074         /* get the right error handle */
00075 
00076         if (con != NULL)
00077         {
00078             tmsp->err = con->err;
00079             tmsp->env = con->env;
00080         }
00081         else
00082         {
00083             tmsp->err = OCILib.err;
00084             tmsp->env = OCILib.env;
00085         }
00086 
00087         /* allocate buffer if needed */
00088 
00089         if ((tmsp->handle == NULL) || (tmsp->hstate == OCI_OBJECT_ALLOCATED_ARRAY))
00090         {
00091             ub4 htype = 0;
00092 
00093             if (tmsp->type == OCI_TIMESTAMP)
00094             {
00095                 htype = OCI_DTYPE_TIMESTAMP;
00096             }
00097             else if (tmsp->type == OCI_TIMESTAMP_TZ)
00098             {
00099                 htype = OCI_DTYPE_TIMESTAMP_TZ;
00100             }
00101             else if (tmsp->type == OCI_TIMESTAMP_LTZ)
00102             {
00103                 htype = OCI_DTYPE_TIMESTAMP_LTZ;
00104             }
00105 
00106             if (tmsp->hstate != OCI_OBJECT_ALLOCATED_ARRAY)
00107             {
00108                 res = (OCI_SUCCESS == OCI_DescriptorAlloc((dvoid  *) tmsp->env,
00109                                                           (dvoid **) (void *) &tmsp->handle,
00110                                                           (ub4     ) htype, (size_t) 0,
00111                                                           (dvoid **) NULL));
00112                 tmsp->hstate = OCI_OBJECT_ALLOCATED;
00113             }
00114 
00115         }
00116         else
00117         {
00118             tmsp->hstate = OCI_OBJECT_FETCHED_CLEAN;
00119         }
00120     }
00121     else
00122     {
00123         res = FALSE;
00124     }
00125 
00126     /* check for failure */
00127 
00128     if (res == FALSE)
00129     {
00130         OCI_TimestampFree(tmsp);
00131         tmsp = NULL;
00132     }
00133 
00134 #else
00135 
00136     OCI_NOT_USED(con);
00137     OCI_NOT_USED(type);
00138     OCI_NOT_USED(buffer);
00139     OCI_NOT_USED(ptmsp);
00140 
00141 #endif
00142 
00143     return tmsp;
00144 }
00145 
00146 /* ********************************************************************************************* *
00147  *                            PUBLIC FUNCTIONS
00148  * ********************************************************************************************* */
00149 
00150 /* --------------------------------------------------------------------------------------------- *
00151  * OCI_TimestampCreate
00152  * --------------------------------------------------------------------------------------------- */
00153 
00154 OCI_Timestamp * OCI_API OCI_TimestampCreate
00155 (
00156     OCI_Connection *con,
00157     unsigned int    type
00158 )
00159 {
00160     OCI_Timestamp *tmsp = NULL;
00161 
00162     OCI_CHECK_INITIALIZED(NULL);
00163 
00164     OCI_CHECK_TIMESTAMP_ENABLED(con, NULL);
00165 
00166 #if OCI_VERSION_COMPILE >= OCI_9_0
00167 
00168     tmsp = OCI_TimestampInit(con, &tmsp, NULL, type);
00169 
00170 #else
00171 
00172     OCI_NOT_USED(type);
00173 
00174 #endif
00175 
00176     OCI_RESULT(tmsp != NULL);
00177 
00178     return tmsp;
00179 }
00180 
00181 /* --------------------------------------------------------------------------------------------- *
00182  * OCI_TimestampFree
00183  * --------------------------------------------------------------------------------------------- */
00184 
00185 boolean OCI_API OCI_TimestampFree
00186 (
00187     OCI_Timestamp *tmsp
00188 )
00189 {
00190     OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp, FALSE);
00191 
00192     OCI_CHECK_TIMESTAMP_ENABLED(tmsp->con, FALSE);
00193 
00194 #if OCI_VERSION_COMPILE >= OCI_9_0
00195 
00196     OCI_CHECK_OBJECT_FETCHED(tmsp, FALSE);
00197 
00198     if (tmsp->hstate == OCI_OBJECT_ALLOCATED)
00199     {
00200         ub4 htype = 0;
00201 
00202         if (tmsp->type == OCI_TIMESTAMP)
00203         {
00204             htype = OCI_DTYPE_TIMESTAMP;
00205         }
00206         else if (tmsp->type == OCI_TIMESTAMP_TZ)
00207         {
00208             htype = OCI_DTYPE_TIMESTAMP_TZ;
00209         }
00210         else if (tmsp->type == OCI_TIMESTAMP_LTZ)
00211         {
00212             htype = OCI_DTYPE_TIMESTAMP_LTZ;
00213         }
00214 
00215         OCI_DescriptorFree((dvoid *) tmsp->handle, htype);
00216     }
00217 
00218     if (tmsp->hstate != OCI_OBJECT_ALLOCATED_ARRAY)
00219     {
00220         OCI_FREE(tmsp);
00221     }
00222 
00223 #endif
00224 
00225     OCI_RESULT(TRUE);
00226 
00227     return TRUE;
00228 }
00229 
00230 /* --------------------------------------------------------------------------------------------- *
00231  * OCI_TimestampArrayCreate
00232  * --------------------------------------------------------------------------------------------- */
00233 
00234 OCI_Timestamp ** OCI_API OCI_TimestampArrayCreate
00235 (
00236     OCI_Connection *con,
00237     unsigned int    type,
00238     unsigned int    nbelem
00239 )
00240 {
00241     OCI_Array *arr        = NULL;
00242     OCI_Timestamp **tmsps = NULL;
00243     unsigned int htype    = 0;
00244 
00245     OCI_CHECK_TIMESTAMP_ENABLED(con, NULL);
00246 
00247 #if OCI_VERSION_COMPILE >= OCI_9_0
00248 
00249     if (type == OCI_TIMESTAMP)
00250     {
00251         htype = OCI_DTYPE_TIMESTAMP;
00252     }
00253     else if (type == OCI_TIMESTAMP_TZ)
00254     {
00255         htype = OCI_DTYPE_TIMESTAMP_TZ;
00256     }
00257     else if (type == OCI_TIMESTAMP_LTZ)
00258     {
00259         htype = OCI_DTYPE_TIMESTAMP_LTZ;
00260     }
00261 
00262     arr = OCI_ArrayCreate(con, nbelem, OCI_CDT_TIMESTAMP, type,sizeof(OCIDateTime *), 
00263                           sizeof(OCI_Timestamp), htype, NULL);
00264 
00265     if (arr != NULL)
00266     {
00267         tmsps = (OCI_Timestamp **) arr->tab_obj;
00268     }
00269 
00270 #else
00271 
00272     OCI_NOT_USED(type);
00273     OCI_NOT_USED(nbelem);
00274 
00275 #endif
00276 
00277     return tmsps;
00278 }
00279 
00280 /* --------------------------------------------------------------------------------------------- *
00281  * OCI_TimestampArrayFree
00282  * --------------------------------------------------------------------------------------------- */
00283 
00284 boolean OCI_API OCI_TimestampArrayFree
00285 (
00286     OCI_Timestamp **tmsps
00287 )
00288 {
00289     return OCI_ArrayFreeFromHandles((void **) tmsps);
00290 }
00291 
00292 /* --------------------------------------------------------------------------------------------- *
00293  * OCI_TimestampGetType
00294  * --------------------------------------------------------------------------------------------- */
00295 
00296 unsigned int OCI_API OCI_TimestampGetType
00297 (
00298     OCI_Timestamp *tmsp
00299 )
00300 {
00301     OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp, OCI_UNKNOWN);
00302 
00303     OCI_CHECK_TIMESTAMP_ENABLED(tmsp->con, OCI_UNKNOWN);
00304 
00305     OCI_RESULT(TRUE);
00306 
00307     return tmsp->type;
00308 }
00309 
00310 /* --------------------------------------------------------------------------------------------- *
00311  * OCI_DateZoneToZone
00312  * --------------------------------------------------------------------------------------------- */
00313 
00314 boolean OCI_API OCI_TimestampAssign
00315 (
00316     OCI_Timestamp *tmsp,
00317     OCI_Timestamp *tmsp_src
00318 )
00319 {
00320     boolean res = TRUE;
00321 
00322     OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp,     FALSE);
00323     OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp_src, FALSE);
00324 
00325     OCI_CHECK_TIMESTAMP_ENABLED(tmsp->con, FALSE);
00326 
00327 #if OCI_VERSION_COMPILE >= OCI_9_0
00328 
00329     OCI_CALL4
00330     (
00331         res, tmsp->err, tmsp->con,
00332 
00333         OCIDateTimeAssign((dvoid *) tmsp->env, tmsp->err,
00334                           tmsp_src->handle, tmsp->handle)
00335     )
00336 
00337 #endif
00338 
00339     OCI_RESULT(res);
00340 
00341     return res;
00342 }
00343 
00344 /* --------------------------------------------------------------------------------------------- *
00345  * OCI_TimestampCheck
00346  * --------------------------------------------------------------------------------------------- */
00347 
00348 int OCI_API OCI_TimestampCheck
00349 (
00350     OCI_Timestamp *tmsp
00351 )
00352 {
00353     boolean res = TRUE;
00354     ub4 value   = 0;
00355 
00356     OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp, value);
00357 
00358     OCI_CHECK_TIMESTAMP_ENABLED(tmsp->con, value);
00359 
00360 #if OCI_VERSION_COMPILE >= OCI_9_0
00361 
00362     OCI_CALL4
00363     (
00364         res, tmsp->err, tmsp->con,
00365 
00366         OCIDateTimeCheck((dvoid *) tmsp->env, tmsp->err, tmsp->handle, &value)
00367     )
00368 
00369 #endif
00370 
00371     OCI_RESULT(res);
00372 
00373     return (int) value;
00374 }
00375 
00376 /* --------------------------------------------------------------------------------------------- *
00377  * OCI_TimestampCompare
00378  * --------------------------------------------------------------------------------------------- */
00379 
00380 int OCI_API OCI_TimestampCompare
00381 (
00382     OCI_Timestamp *tmsp,
00383     OCI_Timestamp *tmsp2
00384 )
00385 {
00386     boolean res = TRUE;
00387     sword value = OCI_ERROR;
00388 
00389     OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp,  value);
00390     OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp2, value);
00391 
00392     OCI_CHECK_TIMESTAMP_ENABLED(tmsp->con, FALSE);
00393 
00394 #if OCI_VERSION_COMPILE >= OCI_9_0
00395 
00396     OCI_CALL4
00397     (
00398         res, tmsp->err, tmsp->con,
00399 
00400         OCIDateTimeCompare((dvoid *) tmsp->env, tmsp->err,
00401                            tmsp2->handle, tmsp2->handle, &value)
00402     )
00403 
00404 #endif
00405 
00406     OCI_RESULT(res);
00407 
00408     return (int) value;
00409 }
00410 
00411 /* --------------------------------------------------------------------------------------------- *
00412  * OCI_TimestampConstruct
00413  * --------------------------------------------------------------------------------------------- */
00414 
00415 boolean OCI_API OCI_TimestampConstruct
00416 (
00417     OCI_Timestamp *tmsp,
00418     int            year,
00419     int            month,
00420     int            day,
00421     int            hour,
00422     int            min,
00423     int            sec,
00424     int            fsec,
00425     const mtext   *timezone
00426 )
00427 {
00428     boolean res = TRUE;
00429 
00430     OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp, FALSE);
00431 
00432     OCI_CHECK_TIMESTAMP_ENABLED(tmsp->con, FALSE);
00433 
00434 #if OCI_VERSION_COMPILE >= OCI_9_0
00435 
00436     OCI_CALL4
00437     (
00438         res, tmsp->err, tmsp->con,
00439 
00440         OCIDateTimeConstruct((dvoid *) tmsp->env, tmsp->err,
00441                              tmsp->handle,
00442                              (sb2) year, (ub1) month, (ub1) day,
00443                              (ub1) hour, (ub1) min,(ub1) sec,
00444                              (ub4) fsec, (OraText *) timezone,
00445                              (size_t) (timezone ? mtextsize(timezone) : 0))
00446     )
00447 
00448 #else
00449 
00450     OCI_NOT_USED(year);
00451     OCI_NOT_USED(month);
00452     OCI_NOT_USED(day);
00453     OCI_NOT_USED(hour);
00454     OCI_NOT_USED(min);
00455     OCI_NOT_USED(sec);
00456     OCI_NOT_USED(fsec);
00457     OCI_NOT_USED(timezone);
00458 
00459 #endif
00460 
00461     OCI_RESULT(res);
00462 
00463     return res;
00464 }
00465 
00466 /* --------------------------------------------------------------------------------------------- *
00467  * OCI_TimestampConvert
00468  * --------------------------------------------------------------------------------------------- */
00469 
00470 boolean OCI_API OCI_TimestampConvert
00471 (
00472     OCI_Timestamp *tmsp,
00473     OCI_Timestamp *tmsp_src
00474 )
00475 {
00476     boolean res = TRUE;
00477 
00478     OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp,     FALSE);
00479     OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp_src, FALSE);
00480 
00481     OCI_CHECK_TIMESTAMP_ENABLED(tmsp->con, FALSE);
00482 
00483 #if OCI_VERSION_COMPILE >= OCI_9_0
00484 
00485     OCI_CALL4
00486     (
00487         res, tmsp->err, tmsp->con,
00488 
00489         OCIDateTimeConvert((dvoid *) tmsp->env, tmsp->err,
00490                            tmsp_src->handle, tmsp->handle)
00491     )
00492 
00493 #endif
00494 
00495     OCI_RESULT(res);
00496 
00497     return res;
00498 }
00499 
00500 /* --------------------------------------------------------------------------------------------- *
00501  * OCI_TimestampFromText
00502  * --------------------------------------------------------------------------------------------- */
00503 
00504 boolean OCI_API OCI_TimestampFromText
00505 (
00506     OCI_Timestamp *tmsp,
00507     const mtext   *str,
00508     const mtext   *fmt
00509 )
00510 {
00511     boolean res = TRUE;
00512     void *ostr1 = NULL;
00513     void *ostr2 = NULL;
00514     int osize1  = -1;
00515     int osize2  = -1;
00516 
00517     OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp, FALSE);
00518     OCI_CHECK_PTR(OCI_IPC_STRING, str,  FALSE);
00519     OCI_CHECK_PTR(OCI_IPC_STRING, fmt,  FALSE);
00520 
00521     OCI_CHECK_TIMESTAMP_ENABLED(tmsp->con, FALSE);
00522 
00523 #if OCI_VERSION_COMPILE >= OCI_9_0
00524 
00525     ostr1 = OCI_GetInputMetaString(str, &osize1);
00526     ostr2 = OCI_GetInputMetaString(fmt, &osize2);
00527 
00528     OCI_CALL4
00529     (
00530         res, tmsp->err, tmsp->con,
00531 
00532         OCIDateTimeFromText((dvoid *) tmsp->env, tmsp->err,
00533                             (OraText *) ostr1, (size_t) osize1,
00534                             (OraText *) ostr2, (ub1) osize2,
00535                             (OraText *) NULL, (size_t) 0,
00536                             tmsp->handle)
00537     )
00538 
00539     OCI_ReleaseMetaString(ostr1);
00540     OCI_ReleaseMetaString(ostr2);
00541 
00542 #else
00543 
00544     OCI_NOT_USED(ostr1);
00545     OCI_NOT_USED(ostr2);
00546     OCI_NOT_USED(osize1);
00547     OCI_NOT_USED(osize2);
00548 
00549 #endif
00550 
00551     OCI_RESULT(res);
00552 
00553     return res;
00554 }
00555 
00556 /* --------------------------------------------------------------------------------------------- *
00557  * OCI_TimestampToText
00558  * --------------------------------------------------------------------------------------------- */
00559 
00560 boolean OCI_API OCI_TimestampToText
00561 (
00562     OCI_Timestamp *tmsp,
00563     const mtext   *fmt,
00564     int            size,
00565     mtext         *str,
00566     int            precision
00567 )
00568 {
00569     boolean res = TRUE;
00570     void *ostr1 = NULL;
00571     void *ostr2 = NULL;
00572     int osize1  = size * (int) sizeof(mtext);
00573     int osize2  = -1;
00574 
00575     OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp, FALSE);
00576     OCI_CHECK_PTR(OCI_IPC_STRING, str,  FALSE);
00577     OCI_CHECK_PTR(OCI_IPC_STRING, fmt,  FALSE);
00578 
00579     /* init output buffer in case of OCI failure */
00580 
00581     str[0] = 0;
00582 
00583     OCI_CHECK_TIMESTAMP_ENABLED(tmsp->con, FALSE);
00584 
00585 #if OCI_VERSION_COMPILE >= OCI_9_0
00586 
00587     ostr1 = OCI_GetInputMetaString(str, &osize1);
00588     ostr2 = OCI_GetInputMetaString(fmt, &osize2);
00589 
00590     OCI_CALL4
00591     (
00592         res, tmsp->err, tmsp->con,
00593 
00594         OCIDateTimeToText((dvoid *) tmsp->env, tmsp->err,
00595                           tmsp->handle, (OraText *) ostr2,
00596                           (ub1) osize2, (ub1) precision,
00597                           (OraText *) NULL, (size_t) 0,
00598                           (ub4*) &osize1, (OraText *) ostr1)
00599 
00600     )
00601 
00602     OCI_GetOutputMetaString(ostr1, str, &osize1);
00603 
00604     OCI_ReleaseMetaString(ostr1);
00605     OCI_ReleaseMetaString(ostr2);
00606 
00607     /* set null string terminator */
00608 
00609     str[osize1/ (int) sizeof(mtext)] = 0;
00610 
00611 #else
00612 
00613     OCI_NOT_USED(ostr1);
00614     OCI_NOT_USED(ostr2);
00615     OCI_NOT_USED(osize1);
00616     OCI_NOT_USED(osize2);
00617     OCI_NOT_USED(precision);
00618 
00619 #endif
00620 
00621     OCI_RESULT(res);
00622 
00623     return res;
00624 }
00625 
00626 /* --------------------------------------------------------------------------------------------- *
00627  * OCI_TimestampGetDate
00628  * --------------------------------------------------------------------------------------------- */
00629 
00630 boolean OCI_API OCI_TimestampGetDate
00631 (
00632     OCI_Timestamp *tmsp,
00633     int           *year,
00634     int           *month,
00635     int           *day
00636 )
00637 {
00638     boolean res = TRUE;
00639     sb2 yr      = 0;
00640     ub1 mt      = 0;
00641     ub1 dy      = 0;
00642 
00643     OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp, FALSE);
00644 
00645     OCI_CHECK_PTR(OCI_IPC_INT, year,  FALSE);
00646     OCI_CHECK_PTR(OCI_IPC_INT, month, FALSE);
00647     OCI_CHECK_PTR(OCI_IPC_INT, day,   FALSE);
00648 
00649     OCI_CHECK_TIMESTAMP_ENABLED(tmsp->con, FALSE);
00650 
00651     *year  = 0;
00652     *month = 0;
00653     *day   = 0;
00654 
00655 #if OCI_VERSION_COMPILE >= OCI_9_0
00656 
00657     OCI_CALL4
00658     (
00659         res, tmsp->err, tmsp->con,
00660 
00661         OCIDateTimeGetDate((dvoid *) tmsp->env, tmsp->err, tmsp->handle,
00662                            &yr, &mt, &dy)
00663     )
00664 
00665     *year  = (int) yr;
00666     *month = (int) mt;
00667     *day   = (int) dy;
00668 
00669 #else
00670 
00671     OCI_NOT_USED(year);
00672     OCI_NOT_USED(month);
00673     OCI_NOT_USED(day);
00674     OCI_NOT_USED(yr);
00675     OCI_NOT_USED(mt);
00676     OCI_NOT_USED(dy);
00677 
00678 #endif
00679 
00680     OCI_RESULT(res);
00681 
00682     return res;
00683 }
00684 
00685 /* --------------------------------------------------------------------------------------------- *
00686  * OCI_TimestampGetTime
00687  * --------------------------------------------------------------------------------------------- */
00688 
00689 boolean OCI_API OCI_TimestampGetTime
00690 (
00691     OCI_Timestamp *tmsp,
00692     int           *hour,
00693     int           *min,
00694     int           *sec,
00695     int           *fsec
00696 )
00697 {
00698     boolean res = TRUE;
00699 
00700     ub1 hr = 0;
00701     ub1 mn = 0;
00702     ub1 sc = 0;
00703     ub4 fs = 0;
00704 
00705     OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp, FALSE);
00706 
00707     OCI_CHECK_PTR(OCI_IPC_INT, hour, FALSE);
00708     OCI_CHECK_PTR(OCI_IPC_INT, min,  FALSE);
00709     OCI_CHECK_PTR(OCI_IPC_INT, sec,  FALSE);
00710     OCI_CHECK_PTR(OCI_IPC_INT, fsec, FALSE);
00711 
00712     OCI_CHECK_TIMESTAMP_ENABLED(tmsp->con, FALSE);
00713 
00714     *hour = 0;
00715     *min  = 0;
00716     *sec  = 0;
00717     *fsec = 0;
00718 
00719 #if OCI_VERSION_COMPILE >= OCI_9_0
00720 
00721     OCI_CALL4
00722     (
00723         res, tmsp->err, tmsp->con,
00724 
00725         OCIDateTimeGetTime((dvoid *) tmsp->env, tmsp->err, tmsp->handle,
00726                            &hr, &mn, &sc, &fs)
00727     )
00728 
00729     *hour = (int) hr;
00730     *min  = (int) mn;
00731     *sec  = (int) sc;
00732     *fsec = (int) fs;
00733 
00734 #else
00735 
00736     OCI_NOT_USED(hour);
00737     OCI_NOT_USED(min);
00738     OCI_NOT_USED(sec);
00739     OCI_NOT_USED(fsec);
00740     OCI_NOT_USED(hr);
00741     OCI_NOT_USED(mn);
00742     OCI_NOT_USED(sc);
00743     OCI_NOT_USED(fs);
00744 
00745 #endif
00746 
00747     OCI_RESULT(res);
00748 
00749     return res;
00750 }
00751 
00752 /* --------------------------------------------------------------------------------------------- *
00753  * OCI_TimestampGetDateTime
00754  * --------------------------------------------------------------------------------------------- */
00755 
00756 boolean OCI_API OCI_TimestampGetDateTime
00757 (
00758     OCI_Timestamp *tmsp,
00759     int           *year,
00760     int           *month,
00761     int           *day,
00762     int           *hour,
00763     int           *min,
00764     int           *sec,
00765     int           *fsec
00766 )
00767 {
00768     return (OCI_TimestampGetDate(tmsp, year, month, day) &&
00769             OCI_TimestampGetTime(tmsp, hour, min, sec, fsec));
00770 }
00771 
00772 /* --------------------------------------------------------------------------------------------- *
00773  * OCI_TimestampGetTimeZoneName
00774  * --------------------------------------------------------------------------------------------- */
00775 
00776 boolean OCI_API OCI_TimestampGetTimeZoneName
00777 (
00778     OCI_Timestamp *tmsp,
00779     int            size,
00780     mtext         *str
00781 )
00782 {
00783     boolean res = TRUE;
00784     void *ostr  = NULL;
00785     int osize   = size * (int) sizeof(mtext);
00786 
00787     OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp, FALSE);
00788     OCI_CHECK_PTR(OCI_IPC_STRING, str, FALSE);
00789 
00790     OCI_CHECK_TIMESTAMP_ENABLED(tmsp->con, FALSE);
00791 
00792 #if OCI_VERSION_COMPILE >= OCI_9_0
00793 
00794     ostr = OCI_GetInputMetaString(str, &osize);
00795 
00796     OCI_CALL4
00797     (
00798         res, tmsp->err, tmsp->con,
00799 
00800         OCIDateTimeGetTimeZoneName((dvoid *) tmsp->env, tmsp->err, tmsp->handle,
00801                                    (ub1*) ostr, (ub4*) &osize)
00802     )
00803 
00804     OCI_GetOutputMetaString(ostr, str, &osize);
00805 
00806     OCI_ReleaseMetaString(ostr);
00807 
00808     /* set null string terminator */
00809 
00810     str[osize/ (int) sizeof(mtext)] = 0;
00811 
00812 #else
00813 
00814     OCI_NOT_USED(str);
00815     OCI_NOT_USED(size);
00816     OCI_NOT_USED(ostr);
00817     OCI_NOT_USED(osize);
00818 
00819 #endif
00820 
00821     OCI_RESULT(res);
00822 
00823     return res;
00824 }
00825 
00826 /* --------------------------------------------------------------------------------------------- *
00827  * OCI_TimestampGetTimeZoneOffset
00828  * --------------------------------------------------------------------------------------------- */
00829 
00830 boolean OCI_API OCI_TimestampGetTimeZoneOffset
00831 (
00832     OCI_Timestamp *tmsp,
00833     int           *hour,
00834     int           *min
00835 )
00836 {
00837     boolean res = TRUE;
00838 
00839     OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp, FALSE);
00840     OCI_CHECK_PTR(OCI_IPC_INT, hour, FALSE);
00841     OCI_CHECK_PTR(OCI_IPC_INT, min, FALSE);
00842 
00843     OCI_CHECK_TIMESTAMP_ENABLED(tmsp->con, FALSE);
00844 
00845 #if OCI_VERSION_COMPILE >= OCI_9_0
00846 
00847     OCI_CALL4
00848     (
00849         res, tmsp->err, tmsp->con,
00850 
00851         OCIDateTimeGetTimeZoneOffset((dvoid *) tmsp->env, tmsp->err,
00852                                      tmsp->handle, (sb1*) hour, (sb1*) min)
00853     )
00854 
00855 #else
00856 
00857     OCI_NOT_USED(hour);
00858     OCI_NOT_USED(min);
00859 
00860 #endif
00861 
00862     OCI_RESULT(res);
00863 
00864     return res;
00865 }
00866 
00867 /* --------------------------------------------------------------------------------------------- *
00868  * OCI_TimestampIntervalAdd
00869  * --------------------------------------------------------------------------------------------- */
00870 
00871 boolean OCI_API OCI_TimestampIntervalAdd
00872 (
00873     OCI_Timestamp *tmsp,
00874     OCI_Interval  *itv
00875 )
00876 {
00877     boolean res        = TRUE;
00878     OCI_Timestamp *tmp = NULL;
00879 
00880     OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp, FALSE);
00881     OCI_CHECK_PTR(OCI_IPC_INTERVAL, itv,  FALSE);
00882 
00883     OCI_CHECK_TIMESTAMP_ENABLED(tmsp->con, FALSE);
00884 
00885 #if OCI_VERSION_COMPILE >= OCI_9_0
00886 
00887     /* OCIDateTimeIntervalAdd() fails if timestamps is not OCI_TIMESTAMP_TZ */
00888 
00889     if ((res == TRUE) && (tmsp->type != OCI_TIMESTAMP_TZ))
00890     {
00891         tmp = OCI_TimestampCreate(tmsp->con, OCI_TIMESTAMP_TZ);
00892 
00893         res = OCI_TimestampConvert(tmp, tmsp);
00894     }
00895     else
00896     {
00897         tmp = tmsp;
00898     }
00899 
00900     OCI_CALL4
00901     (
00902         res, tmsp->err, tmsp->con,
00903 
00904         OCIDateTimeIntervalAdd((dvoid *) tmp->env, tmp->err, tmp->handle,
00905                                itv->handle, tmp->handle)
00906     )
00907 
00908     /* converting back */
00909 
00910     if ((res == TRUE) && (tmsp->type != OCI_TIMESTAMP_TZ))
00911     {
00912         res = OCI_TimestampConvert(tmsp, tmp);
00913 
00914         OCI_TimestampFree(tmp);
00915     }
00916 
00917 #else
00918 
00919     OCI_NOT_USED(tmp);
00920 
00921 #endif
00922 
00923     OCI_RESULT(res);
00924 
00925     return res;
00926 }
00927 
00928 /* --------------------------------------------------------------------------------------------- *
00929  * OCI_TimestampIntervalSub
00930  * --------------------------------------------------------------------------------------------- */
00931 
00932 boolean OCI_API OCI_TimestampIntervalSub
00933 (
00934     OCI_Timestamp *tmsp,
00935     OCI_Interval  *itv
00936 )
00937 {
00938     boolean res        = TRUE;
00939     OCI_Timestamp *tmp = NULL;
00940 
00941     OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp, FALSE);
00942     OCI_CHECK_PTR(OCI_IPC_INTERVAL, itv,  FALSE);
00943 
00944     OCI_CHECK_TIMESTAMP_ENABLED(tmsp->con, FALSE);
00945 
00946 #if OCI_VERSION_COMPILE >= OCI_9_0
00947 
00948     /* OCIDateTimeIntervalSub() fails if timestamps is not OCI_TIMESTAMP_TZ */
00949 
00950     if ((res == TRUE) && (tmsp->type != OCI_TIMESTAMP_TZ))
00951     {
00952         tmp = OCI_TimestampCreate(tmsp->con, OCI_TIMESTAMP_TZ);
00953 
00954         res = OCI_TimestampConvert(tmp, tmsp);
00955     }
00956     else
00957     {
00958         tmp = tmsp;
00959     }
00960 
00961     OCI_CALL4
00962     (
00963         res, tmsp->err, tmsp->con,
00964 
00965         OCIDateTimeIntervalSub((dvoid *) tmp->env, tmp->err, tmp->handle,
00966                                itv->handle, tmp->handle)
00967     )
00968 
00969     /* converting back */
00970 
00971     if ((res == TRUE) && (tmsp->type != OCI_TIMESTAMP_TZ))
00972     {
00973         res = OCI_TimestampConvert(tmsp, tmp);
00974 
00975         OCI_TimestampFree(tmp);
00976     }
00977 
00978 #else
00979 
00980     OCI_NOT_USED(tmp);
00981 
00982 #endif
00983 
00984     OCI_RESULT(res);
00985 
00986     return res;
00987 }
00988 
00989 /* --------------------------------------------------------------------------------------------- *
00990  * OCI_TimestampSubtract
00991  * --------------------------------------------------------------------------------------------- */
00992 
00993 boolean OCI_API OCI_TimestampSubtract
00994 (
00995     OCI_Timestamp *tmsp,
00996     OCI_Timestamp *tmsp2,
00997     OCI_Interval  *itv
00998 )
00999 {
01000     boolean res = TRUE;
01001 
01002     OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp,  FALSE);
01003     OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp2, FALSE);
01004     OCI_CHECK_PTR(OCI_IPC_INTERVAL, itv,   FALSE);
01005 
01006     OCI_CHECK_TIMESTAMP_ENABLED(tmsp->con, FALSE);
01007 
01008 #if OCI_VERSION_COMPILE >= OCI_9_0
01009 
01010     OCI_CALL4
01011     (
01012         res, tmsp->err, tmsp->con,
01013 
01014         OCIDateTimeSubtract((dvoid *) tmsp->env, tmsp->err, tmsp->handle,
01015                             tmsp2->handle, itv->handle)
01016     )
01017 
01018 #endif
01019 
01020     OCI_RESULT(res);
01021 
01022     return res;
01023 }
01024 
01025 /* --------------------------------------------------------------------------------------------- *
01026  * OCI_TimestampSysTimestamp
01027  * --------------------------------------------------------------------------------------------- */
01028 
01029 boolean OCI_API OCI_TimestampSysTimestamp
01030 (
01031     OCI_Timestamp *tmsp
01032 )
01033 {
01034     boolean res         = TRUE;
01035     OCI_Timestamp *tmp  = NULL;
01036     OCIDateTime *handle = NULL;
01037 
01038     OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp, FALSE);
01039 
01040     OCI_CHECK_TIMESTAMP_ENABLED(tmsp->con, FALSE);
01041 
01042 #if OCI_VERSION_COMPILE >= OCI_9_0
01043 
01044     /* Filling a timestamp handle of type OCI_TIMESTAMP with
01045        OCIDateTimeSysTimestamp() can lead later to an error ORA-01483 when
01046        binding the given timestamp to some SQL Statement (Oracle BUG).
01047        The only way to avoid that is to pass to OCIDateTimeSysTimestamp()
01048        a timestamp handle of type OCI_TIMESTAMP_TZ and convert it back to
01049        OCI_TIMESTAMP if needed
01050     */
01051 
01052     if ((res == TRUE) && (tmsp->type == OCI_TIMESTAMP))
01053     {
01054         tmp = OCI_TimestampCreate(tmsp->con, OCI_TIMESTAMP_TZ);
01055 
01056         handle = tmp->handle;
01057     }
01058     else
01059     {
01060         handle = tmsp->handle;
01061     }
01062 
01063     OCI_CALL4
01064     (
01065         res, tmsp->err, tmsp->con,
01066 
01067         OCIDateTimeSysTimeStamp((dvoid *) tmsp->env, tmsp->err, handle)
01068     )
01069 
01070     if ((res == TRUE) && (tmsp->type == OCI_TIMESTAMP))
01071     {
01072         res = OCI_TimestampConvert(tmsp, tmp);
01073 
01074         OCI_TimestampFree(tmp);
01075     }
01076 
01077 #else
01078 
01079     OCI_NOT_USED(tmp);
01080     OCI_NOT_USED(handle);
01081 
01082 #endif
01083 
01084     OCI_RESULT(res);
01085 
01086     return res;
01087 }
01088 
01089 /* --------------------------------------------------------------------------------------------- *
01090  * OCI_TimestampToCTime
01091  * --------------------------------------------------------------------------------------------- */
01092 
01093 boolean OCI_API OCI_TimestampToCTime
01094 (
01095     OCI_Timestamp *tmsp,
01096     struct tm     *ptm,
01097     time_t        *pt
01098 )
01099 {
01100     boolean res = TRUE;
01101     time_t time = (time_t) -1;
01102     int msec    = 0;
01103     struct tm t;
01104 
01105     OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp, FALSE);
01106 
01107     OCI_CHECK_TIMESTAMP_ENABLED(tmsp->con, FALSE);
01108 
01109     res = OCI_TimestampGetDateTime(tmsp, &t.tm_year, &t.tm_mon, &t.tm_mday,
01110                                    &t.tm_hour, &t.tm_min, &t.tm_sec,
01111                                    &msec);
01112 
01113     if (res == TRUE)
01114     {
01115         t.tm_year -= 1900;
01116         t.tm_mon  -= 1;
01117         t.tm_wday  = 0;
01118         t.tm_yday  = 0;
01119         t.tm_isdst = -1;
01120 
01121         time = mktime(&t);
01122 
01123         if (ptm != NULL)
01124         {
01125             memcpy(ptm, &t, sizeof(t));
01126         }
01127 
01128         if (pt != NULL)
01129         {
01130             *pt = time;
01131         }
01132     }
01133 
01134     OCI_RESULT(res);
01135 
01136     return (time != (time_t) -1);
01137 }
01138 
01139 /* --------------------------------------------------------------------------------------------- *
01140  * OCI_TimestampFromCTime
01141  * --------------------------------------------------------------------------------------------- */
01142 
01143 boolean OCI_API OCI_TimestampFromCTime
01144 (
01145     OCI_Timestamp *tmsp,
01146     struct tm     *ptm,
01147     time_t         t
01148 )
01149 {
01150     boolean res = TRUE;
01151 
01152     OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp, FALSE);
01153 
01154     OCI_CHECK_TIMESTAMP_ENABLED(tmsp->con, FALSE);
01155 
01156     if (ptm == NULL && t == (time_t) 0)
01157     {
01158         OCI_ExceptionNullPointer(OCI_IPC_TM);
01159 
01160         return FALSE;
01161     }
01162 
01163     if (ptm == NULL)
01164     {
01165         ptm = localtime(&t);
01166     }
01167 
01168     if (ptm != NULL)
01169     {
01170         res =  OCI_TimestampConstruct(tmsp,
01171                                       ptm->tm_year + 1900,
01172                                       ptm->tm_mon  + 1,
01173                                       ptm->tm_mday,
01174                                       ptm->tm_hour,
01175                                       ptm->tm_min,
01176                                       ptm->tm_sec,
01177                                       (int) 0,
01178                                       (const mtext *) NULL);
01179     }
01180     else
01181     {
01182         OCI_ExceptionNullPointer(OCI_IPC_TM);
01183 
01184         res = FALSE;
01185     }
01186 
01187     OCI_RESULT(res);
01188 
01189     return res;
01190 }