📄 timestamp.c
字号:
/*
+----------------------------------------------------------------------+
| |
| OCILIB - C Driver for Oracle |
| |
| (C Wrapper for Oracle OCI) |
| |
+----------------------------------------------------------------------+
| Website : http://ocilib.net |
+----------------------------------------------------------------------+
| Copyright (c) 2007-2009 Vincent ROGIER |
+----------------------------------------------------------------------+
| This library is free software; you can redistribute it and/or |
| modify it under the terms of the GNU Library General Public |
| License as published by the Free Software Foundation; either |
| version 2 of the License, or (at your option) any later version. |
| |
| This library is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| Library General Public License for more details. |
| |
| You should have received a copy of the GNU Library General Public |
| License along with this library; if not, write to the Free |
| Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
+----------------------------------------------------------------------+
| Author: Vincent ROGIER <vince.rogier@gmail.com> |
+----------------------------------------------------------------------+
*/
/* ------------------------------------------------------------------------ *
* $Id: timestamp.c, v 3.2.0 2009/04/20 00:00 Vince $
* ------------------------------------------------------------------------ */
#include "ocilib_internal.h"
/* ************************************************************************ *
* PRIVATE FUNCTIONS
* ************************************************************************ */
/* ------------------------------------------------------------------------ *
* OCI_TimestampInit
* ------------------------------------------------------------------------ */
OCI_Timestamp * OCI_TimestampInit(OCI_Connection *con, OCI_Timestamp **ptmsp,
OCIDateTime *buffer, ub4 type)
{
OCI_Timestamp *tmsp = NULL;
#if OCI_VERSION_COMPILE >= OCI_9
boolean res = TRUE;
OCI_CHECK(ptmsp == NULL, NULL);
if (*ptmsp == NULL)
*ptmsp = (OCI_Timestamp *) OCI_MemAlloc(OCI_IPC_TIMESTAMP, sizeof(*tmsp),
1, TRUE);
if (*ptmsp != NULL)
{
tmsp = *ptmsp;
tmsp->con = con;
tmsp->handle = buffer;
tmsp->type = type;
/* get the right error handle */
if (con != NULL)
tmsp->err = con->err;
else
tmsp->err = OCILib.err;
/* allocate buffer if needed */
if (tmsp != NULL && tmsp->handle == NULL)
{
ub4 htype = 0;
tmsp->hstate = OCI_OBJECT_ALLOCATED;
if (tmsp->type == OCI_TIMESTAMP)
htype = OCI_DTYPE_TIMESTAMP;
else if (tmsp->type == OCI_TIMESTAMP_TZ)
htype = OCI_DTYPE_TIMESTAMP_TZ;
else if (tmsp->type == OCI_TIMESTAMP_LTZ)
htype = OCI_DTYPE_TIMESTAMP_LTZ;
res = (OCI_SUCCESS == OCI_DescriptorAlloc((dvoid *) OCILib.env,
(dvoid **) (void *) &tmsp->handle,
(ub4 ) htype, (size_t) 0,
(dvoid **) NULL));
}
else
tmsp->hstate = OCI_OBJECT_FETCHED_CLEAN;
}
else
res = FALSE;
/* check for failure */
if (res == FALSE)
{
OCI_TimestampFree(tmsp);
tmsp = NULL;
}
#else
OCI_NOT_USED(con);
OCI_NOT_USED(type);
OCI_NOT_USED(buffer);
OCI_NOT_USED(ptmsp);
#endif
return tmsp;
}
/* ************************************************************************ *
* PUBLIC FUNCTIONS
* ************************************************************************ */
/* ------------------------------------------------------------------------ *
* OCI_TimestampCreate
* ------------------------------------------------------------------------ */
OCI_Timestamp * OCI_API OCI_TimestampCreate(OCI_Connection *con,
unsigned int type)
{
OCI_Timestamp *tmsp = NULL;
OCI_CHECK_INITIALIZED(NULL);
OCI_CHECK_TIMESTAMP_ENABLED(con, NULL);
#if OCI_VERSION_COMPILE >= OCI_9
tmsp = OCI_TimestampInit(con, &tmsp, NULL, type);
#else
OCI_NOT_USED(type);
#endif
OCI_RESULT(tmsp != NULL);
return tmsp;
}
/* ------------------------------------------------------------------------ *
* OCI_TimestampFree
* ------------------------------------------------------------------------ */
boolean OCI_API OCI_TimestampFree(OCI_Timestamp *tmsp)
{
OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp, FALSE);
OCI_CHECK_TIMESTAMP_ENABLED(tmsp->con, FALSE);
#if OCI_VERSION_COMPILE >= OCI_9
OCI_CHECK_OBJECT_FETCHED(tmsp, FALSE);
if (tmsp->hstate == OCI_OBJECT_ALLOCATED)
{
ub4 htype = 0;
if (tmsp->type == OCI_TIMESTAMP)
htype = OCI_DTYPE_TIMESTAMP;
else if (tmsp->type == OCI_TIMESTAMP_TZ)
htype = OCI_DTYPE_TIMESTAMP_TZ;
else if (tmsp->type == OCI_TIMESTAMP_LTZ)
htype = OCI_DTYPE_TIMESTAMP_LTZ;
OCI_DescriptorFree((dvoid *) tmsp->handle, htype);
}
OCI_FREE(tmsp);
#endif
OCI_RESULT(TRUE);
return TRUE;
}
/* ------------------------------------------------------------------------ *
* OCI_TimestampGetType
* ------------------------------------------------------------------------ */
unsigned int OCI_API OCI_TimestampGetType(OCI_Timestamp *tmsp)
{
OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp, OCI_UNKNOWN);
OCI_CHECK_TIMESTAMP_ENABLED(tmsp->con, OCI_UNKNOWN);
OCI_RESULT(TRUE);
return tmsp->type;
}
/* ------------------------------------------------------------------------ *
* OCI_DateZoneToZone
* ------------------------------------------------------------------------ */
boolean OCI_API OCI_TimestampAssign(OCI_Timestamp *tmsp, OCI_Timestamp *tmsp_src)
{
boolean res = TRUE;
OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp, FALSE);
OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp_src, FALSE);
OCI_CHECK_TIMESTAMP_ENABLED(tmsp->con, FALSE);
#if OCI_VERSION_COMPILE >= OCI_9
OCI_CALL4
(
res, tmsp->err, tmsp->con,
OCIDateTimeAssign((dvoid *) OCILib.env, tmsp->err,
tmsp_src->handle, tmsp->handle)
)
#endif
OCI_RESULT(res);
return res;
}
/* ------------------------------------------------------------------------ *
* OCI_TimestampCheck
* ------------------------------------------------------------------------ */
int OCI_API OCI_TimestampCheck(OCI_Timestamp *tmsp)
{
boolean res = TRUE;
ub4 value = 0;
OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp, value);
OCI_CHECK_TIMESTAMP_ENABLED(tmsp->con, value);
#if OCI_VERSION_COMPILE >= OCI_9
OCI_CALL4
(
res, tmsp->err, tmsp->con,
OCIDateTimeCheck((dvoid *) OCILib.env, tmsp->err, tmsp->handle, &value)
)
#endif
OCI_RESULT(res);
return (int) value;
}
/* ------------------------------------------------------------------------ *
* OCI_TimestampCompare
* ------------------------------------------------------------------------ */
int OCI_API OCI_TimestampCompare(OCI_Timestamp *tmsp, OCI_Timestamp *tmsp2)
{
boolean res = TRUE;
sword value = OCI_ERROR;
OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp, value);
OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp2, value);
OCI_CHECK_TIMESTAMP_ENABLED(tmsp->con, FALSE);
#if OCI_VERSION_COMPILE >= OCI_9
OCI_CALL4
(
res, tmsp->err, tmsp->con,
OCIDateTimeCompare((dvoid *) OCILib.env, tmsp->err,
tmsp2->handle, tmsp2->handle, &value)
)
#endif
OCI_RESULT(res);
return (int) value;
}
/* ------------------------------------------------------------------------ *
* OCI_TimestampConstruct
* ------------------------------------------------------------------------ */
boolean OCI_API OCI_TimestampConstruct(OCI_Timestamp *tmsp, int year,int month,
int day, int hour, int min, int sec,
int fsec, const mtext *timezone)
{
boolean res = TRUE;
OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp, FALSE);
OCI_CHECK_TIMESTAMP_ENABLED(tmsp->con, FALSE);
#if OCI_VERSION_COMPILE >= OCI_9
OCI_CALL4
(
res, tmsp->err, tmsp->con,
OCIDateTimeConstruct((dvoid *) OCILib.env, tmsp->err,
tmsp->handle,
(sb2) year, (ub1) month, (ub1) day,
(ub1) hour, (ub1) min,(ub1) sec,
(ub4) fsec, (OraText *) timezone,
(size_t) (timezone ? mtextsize(timezone) : 0))
)
#else
OCI_NOT_USED(year);
OCI_NOT_USED(month);
OCI_NOT_USED(day);
OCI_NOT_USED(hour);
OCI_NOT_USED(min);
OCI_NOT_USED(sec);
OCI_NOT_USED(fsec);
OCI_NOT_USED(timezone);
#endif
OCI_RESULT(res);
return res;
}
/* ------------------------------------------------------------------------ *
* OCI_TimestampConvert
* ------------------------------------------------------------------------ */
boolean OCI_API OCI_TimestampConvert(OCI_Timestamp *tmsp, OCI_Timestamp *tmsp_src)
{
boolean res = TRUE;
OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp, FALSE);
OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp_src, FALSE);
OCI_CHECK_TIMESTAMP_ENABLED(tmsp->con, FALSE);
#if OCI_VERSION_COMPILE >= OCI_9
OCI_CALL4
(
res, tmsp->err, tmsp->con,
OCIDateTimeConvert((dvoid *) OCILib.env, tmsp->err,
tmsp_src->handle, tmsp->handle)
)
#endif
OCI_RESULT(res);
return res;
}
/* ------------------------------------------------------------------------ *
* OCI_TimestampFromText
* ------------------------------------------------------------------------ */
boolean OCI_API OCI_TimestampFromText(OCI_Timestamp *tmsp, const mtext *str,
const mtext *fmt)
{
boolean res = TRUE;
void *ostr1 = NULL;
void *ostr2 = NULL;
int osize1 = -1;
int osize2 = -1;
OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp, FALSE);
OCI_CHECK_PTR(OCI_IPC_STRING, str, FALSE);
OCI_CHECK_PTR(OCI_IPC_STRING, fmt, FALSE);
OCI_CHECK_TIMESTAMP_ENABLED(tmsp->con, FALSE);
#if OCI_VERSION_COMPILE >= OCI_9
ostr1 = OCI_GetInputMetaString(str, &osize1);
ostr2 = OCI_GetInputMetaString(fmt, &osize2);
OCI_CALL4
(
res, tmsp->err, tmsp->con,
OCIDateTimeFromText((dvoid *) OCILib.env, tmsp->err,
(OraText *) ostr1, (size_t) osize1,
(OraText *) ostr2, (ub1) osize2,
(OraText *) NULL, (size_t) 0,
tmsp->handle)
)
OCI_ReleaseMetaString(ostr1);
OCI_ReleaseMetaString(ostr2);
#else
OCI_NOT_USED(ostr1);
OCI_NOT_USED(ostr2);
OCI_NOT_USED(osize1);
OCI_NOT_USED(osize2);
#endif
OCI_RESULT(res);
return res;
}
/* ------------------------------------------------------------------------ *
* OCI_TimestampToText
* ------------------------------------------------------------------------ */
boolean OCI_API OCI_TimestampToText(OCI_Timestamp *tmsp, const mtext *fmt,
int size, mtext *str, int precision)
{
boolean res = TRUE;
void *ostr1 = NULL;
void *ostr2 = NULL;
int osize1 = size*sizeof(mtext);
int osize2 = -1;
OCI_CHECK_PTR(OCI_IPC_TIMESTAMP, tmsp, FALSE);
OCI_CHECK_PTR(OCI_IPC_STRING, str, FALSE);
OCI_CHECK_PTR(OCI_IPC_STRING, fmt, FALSE);
/* init output buffer in case of OCI failure */
str[0] = 0;
OCI_CHECK_TIMESTAMP_ENABLED(tmsp->con, FALSE);
#if OCI_VERSION_COMPILE >= OCI_9
ostr1 = OCI_GetInputMetaString(str, &osize1);
ostr2 = OCI_GetInputMetaString(fmt, &osize2);
OCI_CALL4
(
res, tmsp->err, tmsp->con,
OCIDateTimeToText((dvoid *) OCILib.env, tmsp->err,
tmsp->handle, (OraText *) ostr2,
(ub1) osize2, (ub1) precision,
(OraText *) NULL, (size_t) 0,
(ub4*) &osize1, (OraText *) ostr1)
)
OCI_GetOutputMetaString(ostr1, str, &osize1);
OCI_ReleaseMetaString(ostr1);
OCI_ReleaseMetaString(ostr2);
/* set null string terminator */
str[osize1/sizeof(mtext)] = 0;
#else
OCI_NOT_USED(ostr1);
OCI_NOT_USED(ostr2);
OCI_NOT_USED(osize1);
OCI_NOT_USED(osize2);
OCI_NOT_USED(precision);
#endif
OCI_RESULT(res);
return res;
}
/* ------------------------------------------------------------------------ *
* OCI_TimestampGetDate
* ------------------------------------------------------------------------ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -