[Python-checkins] python/dist/src/Modules datetimemodule.c,1.14,1.15

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Tue, 24 Dec 2002 23:40:57 -0800


Update of /cvsroot/python/python/dist/src/Modules
In directory sc8-pr-cvs1:/tmp/cvs-serv24666/python/Modules

Modified Files:
	datetimemodule.c 
Log Message:
Implemented datetime.astimezone() and datetimetz.astimezone().


Index: datetimemodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/datetimemodule.c,v
retrieving revision 1.14
retrieving revision 1.15
diff -C2 -d -r1.14 -r1.15
*** datetimemodule.c	24 Dec 2002 05:41:27 -0000	1.14
--- datetimemodule.c	25 Dec 2002 07:40:55 -0000	1.15
***************
*** 601,604 ****
--- 601,616 ----
  }
  
+ /* self is a datetimetz.  Replace its tzinfo member. */
+ void
+ replace_tzinfo(PyObject *self, PyObject *newtzinfo)
+ {
+ 	assert(self != NULL);
+ 	assert(PyDateTimeTZ_Check(self));
+ 	assert(check_tzinfo_subclass(newtzinfo) >= 0);
+ 	Py_INCREF(newtzinfo);
+ 	Py_DECREF(((PyDateTime_DateTimeTZ *)self)->tzinfo);
+ 	((PyDateTime_DateTimeTZ *)self)->tzinfo = newtzinfo;
+ }
+ 
  /* Internal helper.
   * Call getattr(tzinfo, name)(tzinfoarg), and extract an int from the
***************
*** 2916,2923 ****
  	if (result && PyTimeTZ_Check(time) && PyDateTimeTZ_Check(result)) {
  		/* Copy the tzinfo field. */
! 		PyObject *tzinfo = ((PyDateTime_TimeTZ *)time)->tzinfo;
! 		Py_INCREF(tzinfo);
! 		Py_DECREF(((PyDateTime_DateTimeTZ *)result)->tzinfo);
! 		((PyDateTime_DateTimeTZ *)result)->tzinfo = tzinfo;
  	}
  	return result;
--- 2928,2932 ----
  	if (result && PyTimeTZ_Check(time) && PyDateTimeTZ_Check(result)) {
  		/* Copy the tzinfo field. */
! 		replace_tzinfo(result, ((PyDateTime_TimeTZ *)time)->tzinfo);
  	}
  	return result;
***************
*** 3248,3251 ****
--- 3257,3278 ----
  
  static PyObject *
+ datetime_astimezone(PyDateTime_DateTime *self, PyObject *args, PyObject *kw)
+ {
+ 	PyObject *tzinfo;
+ 	static char *keywords[] = {"tz", NULL};
+ 
+ 	if (! PyArg_ParseTupleAndKeywords(args, kw, "O:astimezone", keywords,
+ 					  &tzinfo))
+ 		return NULL;
+ 	if (check_tzinfo_subclass(tzinfo) < 0)
+ 		return NULL;
+ 	return new_datetimetz(GET_YEAR(self), GET_MONTH(self), GET_DAY(self),
+ 			      DATE_GET_HOUR(self), DATE_GET_MINUTE(self),
+ 			      DATE_GET_SECOND(self),
+ 			      DATE_GET_MICROSECOND(self),
+ 			      tzinfo);
+ }
+ 
+ static PyObject *
  datetime_timetuple(PyDateTime_DateTime *self)
  {
***************
*** 3398,3401 ****
--- 3425,3431 ----
  	 PyDoc_STR("Return datetime with new specified fields.")},
  
+ 	{"astimezone",  (PyCFunction)datetime_astimezone, METH_KEYWORDS,
+ 	 PyDoc_STR("tz -> datetimetz with same date & time, and tzinfo=tz\n")},
+ 
  	{"__setstate__", (PyCFunction)datetime_setstate, METH_O,
  	 PyDoc_STR("__setstate__(state)")},
***************
*** 4399,4416 ****
   */
  
- /* Internal helper.
-  * self is a datetimetz.  Replace its tzinfo member.
-  */
- void
- replace_tzinfo(PyObject *self, PyObject *newtzinfo)
- {
- 	assert(self != NULL);
- 	assert(newtzinfo != NULL);
- 	assert(PyDateTimeTZ_Check(self));
- 	Py_INCREF(newtzinfo);
- 	Py_DECREF(((PyDateTime_DateTimeTZ *)self)->tzinfo);
- 	((PyDateTime_DateTimeTZ *)self)->tzinfo = newtzinfo;
- }
- 
  static char *datetimetz_kws[] = {
  	"year", "month", "day", "hour", "minute", "second",
--- 4429,4432 ----
***************
*** 4698,4701 ****
--- 4714,4764 ----
  
  static PyObject *
+ datetimetz_astimezone(PyDateTime_DateTimeTZ *self, PyObject *args,
+ 		      PyObject *kw)
+ {
+ 	int y = GET_YEAR(self);
+ 	int m = GET_MONTH(self);
+ 	int d = GET_DAY(self);
+ 	int hh = DATE_GET_HOUR(self);
+ 	int mm = DATE_GET_MINUTE(self);
+ 	int ss = DATE_GET_SECOND(self);
+ 	int us = DATE_GET_MICROSECOND(self);
+ 
+ 	PyObject *tzinfo;
+ 	static char *keywords[] = {"tz", NULL};
+ 
+ 	if (! PyArg_ParseTupleAndKeywords(args, kw, "O:astimezone", keywords,
+ 					  &tzinfo))
+ 		return NULL;
+ 	if (check_tzinfo_subclass(tzinfo) < 0)
+ 		return NULL;
+ 
+ 	if (tzinfo != Py_None && self->tzinfo != Py_None) {
+ 		int none;
+ 		int selfoffset;
+ 		selfoffset = call_utcoffset(self->tzinfo,
+ 					    (PyObject *)self,
+ 					    &none);
+ 	        if (selfoffset == -1 && PyErr_Occurred())
+ 	        	return NULL;
+ 	        if (! none) {
+ 			int tzoffset;
+ 	        	tzoffset = call_utcoffset(tzinfo,
+ 	        				  (PyObject *)self,
+ 	        				  &none);
+ 	        	if (tzoffset == -1 && PyErr_Occurred())
+ 	        		return NULL;
+ 	        	if (! none) {
+ 	        		mm -= selfoffset - tzoffset;
+ 	        		if (normalize_datetime(&y, &m, &d,
+ 	        				       &hh, &mm, &ss, &us) < 0)
+ 	        			return NULL;
+ 	        	}
+ 	        }
+ 	}
+ 	return new_datetimetz(y, m, d, hh, mm, ss, us, tzinfo);
+ }
+ 
+ static PyObject *
  datetimetz_timetuple(PyDateTime_DateTimeTZ *self)
  {
***************
*** 4908,4911 ****
--- 4971,4977 ----
  	{"replace",     (PyCFunction)datetimetz_replace,	METH_KEYWORDS,
  	 PyDoc_STR("Return datetimetz with new specified fields.")},
+ 
+ 	{"astimezone",  (PyCFunction)datetimetz_astimezone, METH_KEYWORDS,
+ 	 PyDoc_STR("tz -> convert to local time in new timezone tz\n")},
  
  	{"__setstate__", (PyCFunction)datetimetz_setstate, METH_O,