[Python-checkins] python/nondist/sandbox/datetime obj_datetimetz.c,1.9,1.10

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Fri, 13 Dec 2002 22:03:20 -0800


Update of /cvsroot/python/python/nondist/sandbox/datetime
In directory sc8-pr-cvs1:/tmp/cvs-serv28083

Modified Files:
	obj_datetimetz.c 
Log Message:
datetimetz class methods now(), utcnow(), fromtimestamp(),
utcfromtimestamp() work by magic via deleting the paste-n-edit code for
them.


Index: obj_datetimetz.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_datetimetz.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** obj_datetimetz.c	14 Dec 2002 05:45:49 -0000	1.9
--- obj_datetimetz.c	14 Dec 2002 06:03:17 -0000	1.10
***************
*** 16,19 ****
--- 16,20 ----
  	return self->tzinfo;
  }
+ 
  static PyGetSetDef datetimetz_getset[] = {
  	{"tzinfo", (getter)datetimetz_tzinfo},
***************
*** 59,189 ****
  }
  
- /* TM_FUNC is the shared type of localtime() and gmtime(). */
- typedef struct tm *(*TM_FUNC)(const time_t *timer);
- 
- /* Internal helper.
-  * Build datetime from a time_t and a distinct count of microseconds.
-  * Pass localtime or gmtime for f, to control the interpretation of timet.
-  */
- static PyObject *
- datetimetz_from_timet_and_us(PyObject *cls, TM_FUNC f, time_t timet, long us)
- {
- 	struct tm *tm;
- 	PyObject *result = NULL;
- 
- 	tm = f(&timet);
- 	if (tm)
- 		result = PyObject_CallFunction(cls, "iiiiiil",
- 					       tm->tm_year + 1900,
- 					       tm->tm_mon + 1,
- 					       tm->tm_mday,
- 					       tm->tm_hour,
- 					       tm->tm_min,
- 					       tm->tm_sec,
- 					       us);
- 	else
- 		PyErr_SetString(PyExc_ValueError,
- 				"timestamp out of range for "
- 				"platform localtime()/gmtime() function");
- 	return result;
- }
- 
- /* Internal helper.
-  * Build datetime from a Python timestamp.  Pass localtime or gmtime for f,
-  * to control the interpretation of the timestamp.  Since a double doesn't
-  * have enough bits to cover a datetime's full range of precision, it's
-  * better to call datetimetz_from_timet_and_us provided you have a way
-  * to get that much precision (e.g., C time() isn't good enough).
-  */
- static PyObject *
- datetimetz_from_timestamp(PyObject *cls, TM_FUNC f, double timestamp)
- {
- 	time_t timet = (time_t)timestamp;
- 	long us = (long)((timestamp - (double)timet) * 1e6);
- 
- 	return datetimetz_from_timet_and_us(cls, f, timet, us);
- }
- 
- /* Internal helper.
-  * Build most accurate possible datetime for current time.  Pass localtime or
-  * gmtime for f as appropriate.
-  */
- static PyObject *
- datetimetz_best_possible(PyObject *cls, TM_FUNC f)
- {
- #ifdef HAVE_GETTIMEOFDAY
- 	struct timeval t;
- 
- #ifdef GETTIMEOFDAY_NO_TZ
- 	gettimeofday(&t);
- #else
- 	gettimeofday(&t, (struct timezone *)NULL);
- #endif
- 	return datetimetz_from_timet_and_us(cls, f, t.tv_sec, t.tv_usec);
- 
- #else	/* ! HAVE_GETTIMEOFDAY */
- 	/* No flavor of gettimeofday exists on this platform.  Python's
- 	 * time.time() does a lot of other platform tricks to get the
- 	 * best time it can on the platform, and we're not going to do
- 	 * better than that (if we could, the better code would belong
- 	 * in time.time()!)  We're limited by the precision of a double,
- 	 * though.
- 	 */
- 	PyObject *time;
- 	double dtime;
- 
- 	time = time_time();
-     	if (time == NULL)
-     		return NULL;
- 	dtime = PyFloat_AsDouble(time);
- 	Py_DECREF(time);
- 	if (dtime == -1.0 && PyErr_Occurred())
- 		return NULL;
- 	return datetimetz_from_timestamp(cls, f, dtime);
- #endif	/* ! HAVE_GETTIMEOFDAY */
- }
- 
- /* Return new local datetime from timestamp (Python timestamp -- a double). */
- static PyObject *
- datetimetz_fromtimestamp(PyObject *cls, PyObject *args)
- {
- 	double timestamp;
- 	PyObject *result = NULL;
- 
- 	if (PyArg_ParseTuple(args, "d:fromtimestamp", &timestamp))
- 		result = datetimetz_from_timestamp(cls, localtime, timestamp);
- 	return result;
- }
- 
- /* Return new UTC datetime from timestamp (Python timestamp -- a double). */
- static PyObject *
- datetimetz_utcfromtimestamp(PyObject *cls, PyObject *args)
- {
- 	double timestamp;
- 	PyObject *result = NULL;
- 
- 	if (PyArg_ParseTuple(args, "d:utcfromtimestamp", &timestamp))
- 		result = datetimetz_from_timestamp(cls, gmtime, timestamp);
- 	return result;
- }
- 
- /* Return best possible local time -- this isn't constrained by the
-  * precision of a timestamp.
-  */
- static PyObject *
- datetimetz_now(PyObject *cls, PyObject *dummy)
- {
- 	return datetimetz_best_possible(cls, localtime);
- }
- 
- /* Return best possible UTC time -- this isn't constrained by the
-  * precision of a timestamp.
-  */
- static PyObject *
- datetimetz_utcnow(PyObject *cls, PyObject *dummy)
- {
- 	return datetimetz_best_possible(cls, gmtime);
- }
- 
  /* Return new datetime from date and time arguments. */
  static PyObject *
--- 60,63 ----
***************
*** 541,561 ****
  static PyMethodDef datetimetz_methods[] = {
  	/* Class methods: */
! 	{"now",         (PyCFunction)datetimetz_now,
! 	 METH_NOARGS | METH_CLASS,
! 	 PyDoc_STR("Return a new datetime representing local day and time.")},
! 
! 	{"utcnow",         (PyCFunction)datetimetz_utcnow,
! 	 METH_NOARGS | METH_CLASS,
! 	 PyDoc_STR("Return a new datetime representing UTC day and time.")},
! 
! 	{"fromtimestamp", (PyCFunction)datetimetz_fromtimestamp,
! 	 METH_VARARGS | METH_CLASS,
! 	 PyDoc_STR("timestamp -> local datetime from a POSIX timestamp "
! 	 	   "(like time.time()).")},
! 
! 	{"utcfromtimestamp", (PyCFunction)datetimetz_utcfromtimestamp,
! 	 METH_VARARGS | METH_CLASS,
! 	 PyDoc_STR("timestamp -> UTC datetime from a POSIX timestamp "
! 	 	   "(like time.time()).")},
  
  	{"combine", (PyCFunction)datetimetz_combine,
--- 415,419 ----
  static PyMethodDef datetimetz_methods[] = {
  	/* Class methods: */
! 	/* Inherited: now(), utcnow(), fromtimestamp(), utcfromtimestamp(). */
  
  	{"combine", (PyCFunction)datetimetz_combine,