[Python-checkins] python/nondist/sandbox/datetime datetime.c,1.66,1.67

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Fri, 13 Dec 2002 12:40:49 -0800


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

Modified Files:
	datetime.c 
Log Message:
Introduced a helper function to sort out which objects are and aren't
offset-aware (e.g., a datetimetz isn't necessarily offset-aware:  it's
offset-naive if it has a None tzinfo object, or even if it has a non-
None tzinfo object but tzinfo.utcoffset(self) returns None).


Index: datetime.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.c,v
retrieving revision 1.66
retrieving revision 1.67
diff -C2 -d -r1.66 -r1.67
*** datetime.c	13 Dec 2002 17:20:24 -0000	1.66
--- datetime.c	13 Dec 2002 20:40:39 -0000	1.67
***************
*** 610,613 ****
--- 610,670 ----
  }
  
+ typedef enum {
+ 	      /* an exception has been set; the caller should pass it on */
+ 	      OFFSET_ERROR,
+ 
+ 	      /* type isn't date, datetime, datetimetz subclass, time, or
+ 	       * timetz subclass
+ 	       */
+ 	      OFFSET_UNKNOWN,
+ 
+ 	      /* date,
+ 	       * datetime,
+ 	       * datetimetz with None tzinfo,
+ 	       * datetimetz where utcoffset() return None
+ 	       * time,
+ 	       * timetz with None tzinfo,
+ 	       * timetz where utcoffset() returns None
+ 	       */
+ 	      OFFSET_NAIVE,
+ 
+ 	      /* timetz where utcoffset() doesn't return None,
+ 	       * datetimetz where utcoffset() doesn't return None
+ 	       */
+ 	      OFFSET_AWARE,
+ } naivety;
+ 
+ /* Classify a datetime object as to whether it's naive or offset-aware.  See
+  * the "naivety" typedef for details.  If the type is aware, *offset is set
+  * to minutes east of UTC (as returned by the tzinfo.utcoffset() method).
+  */
+ static naivety
+ classify_object(PyObject *op, long *offset)
+ {
+ 	int none;
+ 	PyObject *tzinfo;
+ 
+ 	if (PyDateTime_CheckExact(op) ||
+ 	    PyTime_CheckExact(op) ||
+ 	    PyDate_CheckExact(op))
+ 		return OFFSET_NAIVE;
+ 
+ 	tzinfo = NULL;
+ 	if (PyDateTimeTZ_Check(op))
+ 		tzinfo = ((PyDateTime_DateTimeTZ *)op)->tzinfo;
+ 	else if (PyTimeTZ_Check(op))
+ 		tzinfo = ((PyDateTime_TimeTZ *)op)->tzinfo;
+ 
+ 	if (tzinfo == Py_None)
+ 		return OFFSET_NAIVE;
+ 	if (tzinfo == NULL)
+ 		return OFFSET_UNKNOWN;
+ 
+ 	*offset = call_utcoffset(tzinfo, op, &none);
+ 	if (*offset == -1 && PyErr_Occurred())
+ 		return OFFSET_ERROR;
+ 	return none ? OFFSET_NAIVE : OFFSET_AWARE;
+ }
+ 
  /* ---------------------------------------------------------------------------
   * String format helpers.