1
/* typecast_mxdatetime.c - date and time typecasting functions to mx types
3
* Copyright (C) 2001-2003 Federico Di Gregorio <fog@debian.org>
5
* This file is part of the psycopg module.
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU General Public License
9
* as published by the Free Software Foundation; either version 2,
10
* or (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
#include "mxDateTime.h"
24
/* the pointer to the mxDateTime API is initialized by the module init code,
25
we just need to grab it */
26
extern mxDateTimeModule_APIObject *mxDateTimeP;
28
/** DATE - cast a date into mx.DateTime python object **/
31
typecast_MXDATE_cast(PyObject *s, PyObject *curs)
38
if (s == Py_None) {Py_INCREF(s); return s;}
40
str = PyString_AsString(s);
42
/* check for infinity */
43
if (!strcmp(str, "infinity") || !strcmp(str, "-infinity")) {
45
return mxDateTimeP->DateTime_FromDateAndTime(-999998,1,1, 0,0,0);
48
return mxDateTimeP->DateTime_FromDateAndTime(999999,12,31, 0,0,0);
52
Dprintf("typecast_MXDATE_cast: s = %s", str);
53
n = sscanf(str, "%d-%d-%d %d:%d:%lf", &y, &m, &d, &hh, &mm, &ss);
54
Dprintf("typecast_MXDATE_cast: date parsed, %d components", n);
56
if (n != 3 && n != 6) {
57
PyErr_SetString(DataError, "unable to parse date");
60
return mxDateTimeP->DateTime_FromDateAndTime(y, m, d, hh, mm, ss);
63
/** TIME - parse time into an mx.DateTime object **/
66
typecast_MXTIME_cast(PyObject *s, PyObject *curs)
72
if (s == Py_None) {Py_INCREF(s); return s;}
74
str = PyString_AsString(s);
76
Dprintf("typecast_MXTIME_cast: s = %s", str);
78
n = sscanf(str, "%d:%d:%lf", &hh, &mm, &ss);
79
Dprintf("typecast_MXTIME_cast: time parsed, %d components", n);
80
Dprintf("typecast_MXTIME_cast: hh = %d, mm = %d, ss = %f", hh, mm, ss);
83
PyErr_SetString(DataError, "unable to parse time");
87
return mxDateTimeP->DateTimeDelta_FromTime(hh, mm ,ss);
90
/** INTERVAL - parse an interval into an mx.DateTimeDelta **/
93
typecast_MXINTERVAL_cast(PyObject *s, PyObject *curs)
95
long years = 0, months = 0, days = 0, denominator = 1;
96
double hours = 0.0, minutes = 0.0, seconds = 0.0, hundredths = 0.0;
97
double v = 0.0, sign = 1.0;
101
if (s == Py_None) {Py_INCREF(s); return s;}
103
str = PyString_AsString(s);
104
Dprintf("typecast_MXINTERVAL_cast: s = %s", str);
113
case '0': case '1': case '2': case '3': case '4':
114
case '5': case '6': case '7': case '8': case '9':
115
v = v*10 + (double)*str - (double)'0';
116
Dprintf("typecast_MXINTERVAL_cast: v = %f", v);
119
Dprintf("typecast_MXINTERVAL_cast: denominator = %ld",
126
years = (long)(v*sign);
127
str = skip_until_space(str);
128
Dprintf("typecast_MXINTERVAL_cast: years = %ld, rest = %s",
130
v = 0.0; sign = 1.0; part = 1;
136
months = (long)(v*sign);
137
str = skip_until_space(str);
138
Dprintf("typecast_MXINTERVAL_cast: months = %ld, rest = %s",
140
v = 0.0; sign = 1.0; part = 2;
146
days = (long)(v*sign);
147
str = skip_until_space(str);
148
Dprintf("typecast_MXINTERVAL_cast: days = %ld, rest = %s",
150
v = 0.0; sign = 1.0; part = 3;
157
Dprintf("typecast_MXINTERVAL_cast: hours = %f", hours);
160
else if (part == 4) {
162
Dprintf("typecast_MXINTERVAL_cast: minutes = %f", minutes);
170
Dprintf("typecast_MXINTERVAL_cast: seconds = %f", seconds);
182
/* manage last value, be it minutes or seconds or hundredths of a second */
185
Dprintf("typecast_MXINTERVAL_cast: minutes = %f", minutes);
187
else if (part == 5) {
189
Dprintf("typecast_MXINTERVAL_cast: seconds = %f", seconds);
191
else if (part == 6) {
193
Dprintf("typecast_MXINTERVAL_cast: hundredths = %f", hundredths);
194
hundredths = hundredths/denominator;
195
Dprintf("typecast_MXINTERVAL_cast: fractions = %.20f", hundredths);
198
/* calculates seconds */
200
seconds = - (hundredths + seconds + minutes*60 + hours*3600);
203
seconds += hundredths + minutes*60 + hours*3600;
206
/* calculates days */
207
days += years*365 + months*30;
209
Dprintf("typecast_MXINTERVAL_cast: days = %ld, seconds = %f",
211
return mxDateTimeP->DateTimeDelta_FromDaysAndSeconds(days, seconds);
214
/* psycopg defaults to using mx types */
216
#ifdef PSYCOPG_DEFAULT_MXDATETIME
217
#define typecast_DATE_cast typecast_MXDATE_cast
218
#define typecast_TIME_cast typecast_MXTIME_cast
219
#define typecast_INTERVAL_cast typecast_MXINTERVAL_cast
220
#define typecast_DATETIME_cast typecast_MXDATE_cast