[Python-checkins] CVS: python/dist/src/Modules timemodule.c,2.104,2.105

Thomas Wouters twouters@users.sourceforge.net
Fri, 19 Jan 2001 15:16:58 -0800


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

Modified Files:
	timemodule.c 
Log Message:

Make the 'time' argument to the timemodule functions strftime, asctime,
ctime, gmtime and localtime optional, defaulting to 'the current time' in
all cases. Adjust docs, add news item. Also convert all argument-handling to
METH_VARARGS. Closes SF patch #103265.



Index: timemodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/timemodule.c,v
retrieving revision 2.104
retrieving revision 2.105
diff -C2 -r2.104 -r2.105
*** timemodule.c	2000/12/12 22:42:30	2.104
--- timemodule.c	2001/01/19 23:16:56	2.105
***************
*** 110,114 ****
  {
  	double secs;
! 	if (!PyArg_NoArgs(args))
  		return NULL;
  	secs = floattime();
--- 110,114 ----
  {
  	double secs;
! 	if (!PyArg_ParseTuple(args, ":time"))
  		return NULL;
  	secs = floattime();
***************
*** 139,143 ****
  time_clock(PyObject *self, PyObject *args)
  {
! 	if (!PyArg_NoArgs(args))
  		return NULL;
  	return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
--- 139,143 ----
  time_clock(PyObject *self, PyObject *args)
  {
! 	if (!PyArg_ParseTuple(args, ":clock"))
  		return NULL;
  	return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
***************
*** 154,158 ****
  	LARGE_INTEGER now, diff, rem;
  
! 	if (!PyArg_NoArgs(args))
  		return NULL;
  
--- 154,158 ----
  	LARGE_INTEGER now, diff, rem;
  
! 	if (!PyArg_ParseTuple(args, ":clock"))
  		return NULL;
  
***************
*** 193,197 ****
  {
  	double secs;
! 	if (!PyArg_Parse(args, "d", &secs))
  		return NULL;
  	if (floatsleep(secs) != 0)
--- 193,197 ----
  {
  	double secs;
! 	if (!PyArg_ParseTuple(args, "d:sleep", &secs))
  		return NULL;
  	if (floatsleep(secs) != 0)
***************
*** 245,249 ****
  {
  	double when;
! 	if (!PyArg_Parse(args, "d", &when))
  		return NULL;
  	return time_convert((time_t)when, gmtime);
--- 245,251 ----
  {
  	double when;
! 	if (PyTuple_Size(args) == 0)
! 		when = floattime();
! 	if (!PyArg_ParseTuple(args, "|d:gmtime", &when))
  		return NULL;
  	return time_convert((time_t)when, gmtime);
***************
*** 251,257 ****
  
  static char gmtime_doc[] =
! "gmtime(seconds) -> tuple\n\
  \n\
! Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a. GMT).";
  
  static PyObject *
--- 253,260 ----
  
  static char gmtime_doc[] =
! "gmtime([seconds]) -> tuple\n\
  \n\
! Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.\n\
! GMT).  When 'seconds' is not passed in, convert the current time instead.";
  
  static PyObject *
***************
*** 259,263 ****
  {
  	double when;
! 	if (!PyArg_Parse(args, "d", &when))
  		return NULL;
  	return time_convert((time_t)when, localtime);
--- 262,268 ----
  {
  	double when;
! 	if (PyTuple_Size(args) == 0)
! 		when = floattime();
! 	if (!PyArg_ParseTuple(args, "|d:localtime", &when))
  		return NULL;
  	return time_convert((time_t)when, localtime);
***************
*** 265,270 ****
  
  static char localtime_doc[] =
! "localtime(seconds) -> tuple\n\
! Convert seconds since the Epoch to a time tuple expressing local time.";
  
  static int
--- 270,276 ----
  
  static char localtime_doc[] =
! "localtime([seconds]) -> tuple\n\
! Convert seconds since the Epoch to a time tuple expressing local time.\n\
! When 'seconds' is not passed in, convert the current time instead.";
  
  static int
***************
*** 315,319 ****
  time_strftime(PyObject *self, PyObject *args)
  {
! 	PyObject *tup;
  	struct tm buf;
  	const char *fmt;
--- 321,325 ----
  time_strftime(PyObject *self, PyObject *args)
  {
! 	PyObject *tup = NULL;
  	struct tm buf;
  	const char *fmt;
***************
*** 323,330 ****
  
  	memset((void *) &buf, '\0', sizeof(buf));
  
! 	if (!PyArg_ParseTuple(args, "sO:strftime", &fmt, &tup) 
! 	    || !gettmarg(tup, &buf))
  		return NULL;
  	fmtlen = strlen(fmt);
  
--- 329,342 ----
  
  	memset((void *) &buf, '\0', sizeof(buf));
+ 
+ 	if (!PyArg_ParseTuple(args, "s|O:strftime", &fmt, &tup))
+ 		return NULL;
  
! 	if (tup == NULL) {
! 		time_t tt = time(NULL);
! 		buf = *localtime(&tt);
! 	} else if (!gettmarg(tup, &buf))
  		return NULL;
+ 	
  	fmtlen = strlen(fmt);
  
***************
*** 354,361 ****
  
  static char strftime_doc[] =
! "strftime(format, tuple) -> string\n\
  \n\
  Convert a time tuple to a string according to a format specification.\n\
! See the library reference manual for formatting codes.";
  #endif /* HAVE_STRFTIME */
  
--- 366,374 ----
  
  static char strftime_doc[] =
! "strftime(format[, tuple]) -> string\n\
  \n\
  Convert a time tuple to a string according to a format specification.\n\
! See the library reference manual for formatting codes. When the time tuple\n\
! is not present, current time as returned by localtime() is used.";
  #endif /* HAVE_STRFTIME */
  
***************
*** 402,411 ****
  time_asctime(PyObject *self, PyObject *args)
  {
! 	PyObject *tup;
  	struct tm buf;
  	char *p;
! 	if (!PyArg_ParseTuple(args, "O:asctime", &tup))
  		return NULL;
! 	if (!gettmarg(tup, &buf))
  		return NULL;
  	p = asctime(&buf);
--- 415,427 ----
  time_asctime(PyObject *self, PyObject *args)
  {
! 	PyObject *tup = NULL;
  	struct tm buf;
  	char *p;
! 	if (!PyArg_ParseTuple(args, "|O:asctime", &tup))
  		return NULL;
! 	if (tup == NULL) {
! 		time_t tt = time(NULL);
! 		buf = *localtime(&tt);
! 	} else if (!gettmarg(tup, &buf))
  		return NULL;
  	p = asctime(&buf);
***************
*** 416,422 ****
  
  static char asctime_doc[] =
! "asctime(tuple) -> string\n\
  \n\
! Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.";
  
  static PyObject *
--- 432,440 ----
  
  static char asctime_doc[] =
! "asctime([tuple]) -> string\n\
  \n\
! Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n\
! When the time tuple is not present, current time as returned by localtime()\n\
! is used.";
  
  static PyObject *
***************
*** 426,432 ****
  	time_t tt;
  	char *p;
! 	if (!PyArg_Parse(args, "d", &dt))
! 		return NULL;
! 	tt = (time_t)dt;
  #if defined(macintosh) && defined(USE_GUSI204)
  	tt = tt + GUSI_TO_MSL_EPOCH;
--- 444,455 ----
  	time_t tt;
  	char *p;
! 	
! 	if (PyTuple_Size(args) == 0)
! 		tt = time(NULL);
! 	else {
! 		if (!PyArg_ParseTuple(args, "|d:ctime", &dt))
! 			return NULL;
! 		tt = (time_t)dt;
! 	}
  #if defined(macintosh) && defined(USE_GUSI204)
  	tt = tt + GUSI_TO_MSL_EPOCH;
***************
*** 446,450 ****
  \n\
  Convert a time in seconds since the Epoch to a string in local time.\n\
! This is equivalent to asctime(localtime(seconds)).";
  
  #ifdef HAVE_MKTIME
--- 469,474 ----
  \n\
  Convert a time in seconds since the Epoch to a string in local time.\n\
! This is equivalent to asctime(localtime(seconds)). When the time tuple is\n\
! not present, current time as returned by localtime() is used.";
  
  #ifdef HAVE_MKTIME
***************
*** 480,492 ****
  
  static PyMethodDef time_methods[] = {
! 	{"time",	time_time, METH_OLDARGS, time_doc},
  #ifdef HAVE_CLOCK
! 	{"clock",	time_clock, METH_OLDARGS, clock_doc},
  #endif
! 	{"sleep",	time_sleep, METH_OLDARGS, sleep_doc},
! 	{"gmtime",	time_gmtime, METH_OLDARGS, gmtime_doc},
! 	{"localtime",	time_localtime, METH_OLDARGS, localtime_doc},
  	{"asctime",	time_asctime, METH_VARARGS, asctime_doc},
! 	{"ctime",	time_ctime, METH_OLDARGS, ctime_doc},
  #ifdef HAVE_MKTIME
  	{"mktime",	time_mktime, METH_VARARGS, mktime_doc},
--- 504,516 ----
  
  static PyMethodDef time_methods[] = {
! 	{"time",	time_time, METH_VARARGS, time_doc},
  #ifdef HAVE_CLOCK
! 	{"clock",	time_clock, METH_VARARGS, clock_doc},
  #endif
! 	{"sleep",	time_sleep, METH_VARARGS, sleep_doc},
! 	{"gmtime",	time_gmtime, METH_VARARGS, gmtime_doc},
! 	{"localtime",	time_localtime, METH_VARARGS, localtime_doc},
  	{"asctime",	time_asctime, METH_VARARGS, asctime_doc},
! 	{"ctime",	time_ctime, METH_VARARGS, ctime_doc},
  #ifdef HAVE_MKTIME
  	{"mktime",	time_mktime, METH_VARARGS, mktime_doc},