[Python-checkins] python/nondist/sandbox/datetime datetime.c,1.55,1.56 doc.txt,1.44,1.45 obj_timetz.c,1.4,1.5

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Wed, 11 Dec 2002 12:58:00 -0800


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

Modified Files:
	datetime.c doc.txt obj_timetz.c 
Log Message:
Brrrrrrrrrr.


Index: datetime.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.c,v
retrieving revision 1.55
retrieving revision 1.56
diff -C2 -d -r1.55 -r1.56
*** datetime.c	11 Dec 2002 18:54:10 -0000	1.55
--- datetime.c	11 Dec 2002 20:57:57 -0000	1.56
***************
*** 693,696 ****
--- 693,779 ----
  }
  
+ /* Helpers for dealing with calling tzinfo methods. */
+ 
+ /* Call tzinfo.utcoffset(tzinfoarg), and extract an integer from the
+  * result.  tzinfo must be an instance of the tzinfo class.  If utcoffset()
+  * returns None, get_utcoffset returns 0.  If uctoffset() doesn't return
+  * a Python int or long, TypeError is raised and this returns -1.  If
+  * utcoffset() returns an int outside the legitimate range for a UTC offset,
+  * ValueError is raised and this returns -1.
+  */
+ static long
+ get_utcoffset(PyObject *tzinfo, PyObject *tzinfoarg)
+ {
+ 	PyObject *u;
+ 	long result = -1;
+ 
+ 	assert(tzinfo != NULL);
+ 	assert(PyTZInfo_Check(tzinfo));
+ 	assert(tzinfoarg != NULL);
+ 
+ 	u = PyObject_CallMethod(tzinfo, "utcoffset", "O", tzinfoarg);
+ 	if (u == NULL)
+ 		return -1;
+ 
+ 	if (u == Py_None) {
+ 		result = 0;
+ 		goto Done;
+ 	}
+ 
+ 	if (PyInt_Check(u))
+ 		result = PyInt_AS_LONG(u);
+ 	else if (PyLong_Check(u))
+ 		result = PyLong_AsLong(u);
+ 	else {
+ 		PyErr_SetString(PyExc_TypeError,
+ 				"utcoffset() must return None or int or long");
+ 		goto Done;
+ 	}
+ 
+ 	if (result < -1439 || result > 1439) {
+ 		PyErr_Format(PyExc_ValueError,
+ 			     "utcoffset() returned %ld; must be in "
+ 			     "-1439 .. 1439",
+ 			     result);
+ 		result = -1;
+ 	}
+ 
+ Done:
+ 	Py_DECREF(u);
+ 	return result;
+ }
+ 
+ /* Add an hours & minutes UTC offset string to buf.  buf has no more than
+  * buflen bytes remaining.  The UTC offset is gotten by calling
+  * tzinfo.uctoffset(tzinfoarg).  If that returns None, nothing is done.
+  * Else the returned value is checked for sanity (an integer in range),
+  * and if that's OK it's converted to an hours & minutes string of the
+  * form
+  *   sign HH sep MM
+  * Returns 0 if everything is OK.  If the return value from utcoffset() is
+  * bogus, an appropriate exception is set and -1 is returned.
+  */
+ static int
+ format_timezone(char *buf, int buflen, const char *sep,
+ 		PyObject *tzinfo, PyObject *tzinfoarg)
+ {
+ 	long offset;
+ 	long hours;
+ 	long minutes;
+ 	char sign;
+ 
+ 	offset = get_utcoffset(tzinfo, tzinfoarg);
+ 	if (offset == -1 && PyErr_Occurred())
+ 		return -1;
+ 	sign = '+';
+ 	if (offset < 0) {
+ 		sign = '-';
+ 		offset = - offset;
+ 	}
+ 	hours = divmod(offset, 60, &minutes);
+ 	PyOS_snprintf(buf, buflen, "%c%02d%s%02d", sign, hours, sep, minutes);
+ 	return 0;
+ }
+ 
  static PyObject *date_unpickler_object = NULL;
  static PyObject *datetime_unpickler_object = NULL;

Index: doc.txt
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/doc.txt,v
retrieving revision 1.44
retrieving revision 1.45
diff -C2 -d -r1.44 -r1.45
*** doc.txt	11 Dec 2002 18:54:10 -0000	1.44
--- doc.txt	11 Dec 2002 20:57:58 -0000	1.45
***************
*** 712,717 ****
      is intended to be the total offset from UTC; for example, if a
      tzinfo object represents both time zone and DST adjustments,
!     utcoffset() should return their sum.  Return None if the UTC offset
!     isn't known.
  
    - tzname(dt)
--- 712,719 ----
      is intended to be the total offset from UTC; for example, if a
      tzinfo object represents both time zone and DST adjustments,
!     utcoffset() should return their sum.  If the UTC offset isn't known,
!     return None.  Else the value returned must be an int (or long), in
!     the range -1439 to 1439 inclusive (1440 = 24*60; the magnitude of
!     the offset must be less than one day).
  
    - tzname(dt)

Index: obj_timetz.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_timetz.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** obj_timetz.c	11 Dec 2002 19:29:26 -0000	1.4
--- obj_timetz.c	11 Dec 2002 20:57:58 -0000	1.5
***************
*** 57,78 ****
  timetz_repr(PyDateTime_TimeTZ *self)
  {
! 	char buffer[100];
! 	char *typename = self->ob_type->tp_name;
! 	int h = TIME_GET_HOUR(self);
! 	int m = TIME_GET_MINUTE(self);
! 	int s = TIME_GET_SECOND(self);
! 	int us = TIME_GET_MICROSECOND(self);
  
! 	if (us)
! 		PyOS_snprintf(buffer, sizeof(buffer),
! 			      "%s(%d, %d, %d, %d)", typename, h, m, s, us);
! 	else if (s)
! 		PyOS_snprintf(buffer, sizeof(buffer),
! 			      "%s(%d, %d, %d)", typename, h, m, s);
! 	else
! 		PyOS_snprintf(buffer, sizeof(buffer),
! 			      "%s(%d, %d)", typename, h, m);
! 	return PyString_FromString(buffer);
! }
  
  static PyObject *
--- 57,102 ----
  timetz_repr(PyDateTime_TimeTZ *self)
  {
! 	PyObject *r;
! 	PyObject *result = time_repr((PyDateTime_Time *)self);
  
! 	if (result == NULL)
! 		return NULL;
! 	if (self->tzinfo == Py_None)
! 		return result;
! 	/* We have to append  tzinfo=repr(tzinfo). */
! 	/* Get rid of the trailing ')'. */
! 	assert(PyString_AsString(result)[PyString_Size(result)-1] == ')');
! 	r = PyString_FromStringAndSize(PyString_AsString(result),
! 				       PyString_Size(result) - 1);
! 	Py_DECREF(result);
! 	if (r == NULL)
! 		return NULL;
! 	result = r;
! 
! 	/* Append ", tzinfo=". */
! 	r = PyString_FromString(", tzinfo=");
! 	if (r == NULL) {
! 		Py_DECREF(result);
! 		return NULL;
! 	}
! 	PyString_ConcatAndDel(&result, r);
! 
! 	/* Append repr(tzinfo). */
!  	r = PyObject_Repr(self->tzinfo);
! 	if (r == NULL) {
! 		Py_DECREF(result);
! 		return NULL;
! 	}
! 	PyString_ConcatAndDel(&result, r);
! 
! 	/* Add a closing paren. */
! 	r = PyString_FromString(")");
! 	if (r == NULL) {
! 		Py_DECREF(result);
! 		return NULL;
! 	}
! 	PyString_ConcatAndDel(&result, r);
! 	return result;
!  }
  
  static PyObject *