• Main Page
  • Modules
  • Data Structures
  • Files
  • File List

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

Generated on Mon Dec 13 2010 22:32:02 for OCILIB (C Driver for Oracle) by  doxygen 1.7.2