📄 date.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: date.c, v 3.2.0 2009/04/20 00:00 Vince $
* ------------------------------------------------------------------------ */
#include "ocilib_internal.h"
/* ************************************************************************ *
* PRIVATE FUNCTIONS
* ************************************************************************ */
/* ------------------------------------------------------------------------ *
* OCI_DateInit
* ------------------------------------------------------------------------ */
OCI_Date * OCI_DateInit(OCI_Connection *con, OCI_Date **pdate, OCIDate *buffer,
boolean allocate, boolean ansi)
{
OCI_Date *date = NULL;
boolean res = TRUE;
OCI_CHECK(pdate == NULL, NULL);
if (*pdate == NULL)
*pdate = (OCI_Date *) OCI_MemAlloc(OCI_IPC_DATE, sizeof(*date), 1, TRUE);
if (*pdate != NULL)
{
date = *pdate;
date->con = con;
/* get the right error handle */
if (con != NULL)
date->err = con->err;
else
date->err = OCILib.err;
/* allocate buffer if needed */
if ((date->handle == NULL) && ((allocate == TRUE) || (ansi == TRUE)))
{
date->allocated = TRUE;
if (allocate == TRUE)
date->hstate = OCI_OBJECT_ALLOCATED;
date->handle = (OCIDate *) OCI_MemAlloc(OCI_IPC_OCIDATE,
sizeof(*date->handle),
1, TRUE);
res = (date->handle != NULL);
}
else
{
date->hstate = OCI_OBJECT_FETCHED_CLEAN;
date->handle = buffer;
}
/* if the input buffer is an SQLT_DAT buffer, we need to convert it */
if ((ansi == TRUE) && (buffer != NULL))
{
unsigned char *d = (unsigned char *) buffer;
date->handle->OCIDateYYYY = (sb2) (((d[0] - 100) * 100) + (d[1] - 100));
date->handle->OCIDateMM = (ub1) d[2];
date->handle->OCIDateDD = (ub1) d[3];
date->handle->OCIDateTime.OCITimeHH = (ub1) (d[4] - 1);
date->handle->OCIDateTime.OCITimeMI = (ub1) (d[5] - 1);
date->handle->OCIDateTime.OCITimeSS = (ub1) (d[6] - 1);
}
}
else
res = FALSE;
/* check for failure */
if (res == FALSE)
{
OCI_DateFree(date);
date = NULL;
}
return date;
}
/* ************************************************************************ *
* PUBLIC FUNCTIONS
* ************************************************************************ */
/* ------------------------------------------------------------------------ *
* OCI_DateCreate
* ------------------------------------------------------------------------ */
OCI_Date * OCI_API OCI_DateCreate(OCI_Connection *con)
{
OCI_Date *date = NULL;
OCI_CHECK_INITIALIZED(NULL);
date = OCI_DateInit(con, &date, NULL, TRUE, FALSE);
OCI_RESULT(date != NULL);
return date;
}
/* ------------------------------------------------------------------------ *
* OCI_DateFree
* ------------------------------------------------------------------------ */
boolean OCI_API OCI_DateFree(OCI_Date *date)
{
OCI_CHECK_PTR(OCI_IPC_DATE, date, FALSE);
OCI_CHECK_OBJECT_FETCHED(date, FALSE);
if (date->allocated == TRUE)
OCI_FREE(date->handle);
OCI_FREE(date);
OCI_RESULT(TRUE);
return TRUE;
}
/* ------------------------------------------------------------------------ *
* OCI_DateAddDays
* ------------------------------------------------------------------------ */
boolean OCI_API OCI_DateAddDays(OCI_Date *date, int nb)
{
boolean res = TRUE;
OCI_CHECK_PTR(OCI_IPC_DATE, date, FALSE);
OCI_CALL4
(
res, date->err, date->con,
OCIDateAddDays(date->err, date->handle, (sb4) nb, date->handle)
)
OCI_RESULT(res);
return res;
}
/* ------------------------------------------------------------------------ *
* OCI_DateAddMonths
* ------------------------------------------------------------------------ */
boolean OCI_API OCI_DateAddMonths(OCI_Date *date, int nb)
{
boolean res = TRUE;
OCI_CHECK_PTR(OCI_IPC_DATE, date, FALSE);
OCI_CALL4
(
res, date->err, date->con,
OCIDateAddMonths(date->err, date->handle, (sb4) nb, date->handle)
)
OCI_RESULT(res);
return res;
}
/* ------------------------------------------------------------------------ *
* OCI_DateAssign
* ------------------------------------------------------------------------ */
boolean OCI_API OCI_DateAssign(OCI_Date *date, OCI_Date *date_src)
{
boolean res = TRUE;
OCI_CHECK_PTR(OCI_IPC_DATE, date, FALSE);
OCI_CHECK_PTR(OCI_IPC_DATE, date_src, FALSE);
OCI_CALL4
(
res, date->err, date->con,
OCIDateAssign(date->err, date_src->handle, date->handle)
)
OCI_RESULT(res);
return res;
}
/* ------------------------------------------------------------------------ *
* OCI_DateCheck
* ------------------------------------------------------------------------ */
int OCI_API OCI_DateCheck(OCI_Date *date)
{
boolean res = TRUE;
uword valid = 0;
OCI_CHECK_PTR(OCI_IPC_DATE, date, OCI_ERROR);
OCI_CALL4
(
res, date->err, date->con,
OCIDateCheck(date->err, date->handle, &valid)
)
OCI_RESULT(res);
return (int) valid;
}
/* ------------------------------------------------------------------------ *
* OCI_DateCompare
* ------------------------------------------------------------------------ */
int OCI_API OCI_DateCompare(OCI_Date *date, OCI_Date *date2)
{
boolean res = TRUE;
sword value = -1;
OCI_CHECK_PTR(OCI_IPC_DATE, date, -1);
OCI_CALL4
(
res, date->err, date->con,
OCIDateCompare(date->err, date->handle, date2->handle, &value)
)
OCI_RESULT(res);
return (int) value;
}
/* ------------------------------------------------------------------------ *
* OCI_DateDaysBetween
* ------------------------------------------------------------------------ */
int OCI_API OCI_DateDaysBetween(OCI_Date *date, OCI_Date *date2)
{
boolean res = TRUE;
sb4 nb = 0;
OCI_CHECK_PTR(OCI_IPC_DATE, date, OCI_ERROR);
OCI_CHECK_PTR(OCI_IPC_DATE, date2, OCI_ERROR);
OCI_CALL4
(
res, date->err, date->con,
OCIDateDaysBetween(date->err, date->handle, date2->handle, &nb)
)
OCI_RESULT(res);
return (sb4) nb;
}
/* ------------------------------------------------------------------------ *
* OCI_DateFromText
* ------------------------------------------------------------------------ */
boolean OCI_API OCI_DateFromText(OCI_Date *date, const mtext *str,
const mtext *fmt)
{
void *ostr1 = NULL;
void *ostr2 = NULL;
int osize1 = -1;
int osize2 = -1;
boolean res = TRUE;
OCI_CHECK_PTR(OCI_IPC_DATE, date, FALSE);
OCI_CHECK_PTR(OCI_IPC_STRING, str, FALSE);
OCI_CHECK_PTR(OCI_IPC_STRING, fmt, FALSE);
ostr1 = OCI_GetInputMetaString(str, &osize1);
ostr2 = OCI_GetInputMetaString(fmt, &osize2);
OCI_CALL4
(
res, date->err, date->con,
OCIDateFromText(date->err,
(oratext *) ostr1, (ub4) osize1,
(oratext *) ostr2, (ub1) osize2,
(oratext *) NULL, (ub4) 0, date->handle)
)
OCI_ReleaseMetaString(ostr1);
OCI_ReleaseMetaString(ostr2);
OCI_RESULT(res);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -