tzset()

Clark C . Evans cce at clarkevans.com
Wed Jul 3 20:47:58 EDT 2002


On Wed, Jul 03, 2002 at 01:35:10PM -0400, Clark C . Evans wrote:
| On Thu, 16 Jun 1994 at 13:01:14 +0200 Guido van Rossum wrote:
| | The most pragmatic way to solve this is probably to add a function
| | tzset("string") to the time module which forces TZ="string" in the
| | process's current environment and then calls the C tzset() again (and
| | reinitializes the Python variables that describe the timezone).
| |
| 
| This is a great idea; was it ever done?

Here is a snippet module which does the above, only it
returns a tuple (timezone_abbrev, gmt offset )
share & enjoy, clark

...

#include <Python.h>
#include <assert.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
PyObject * offset(PyObject *self,PyObject *args) {
   time_t now; struct tm a; char  *tznm; PyObject *pyobj;
   if(1 != PyTuple_Size(args)) { PyErr_BadArgument(); return NULL; }
   pyobj = PyTuple_GetItem(args,0);
   if(!pyobj) return NULL;
   if(!PyString_Check(pyobj)) { PyErr_BadArgument(); return NULL; }
   (void) setenv("TZ",PyString_AsString(pyobj),1); tzset();
   now = time( NULL );
   (void)localtime_r( &now, &a );
   tznm = tzname[a.tm_isdst];
   pyobj = Py_BuildValue("(sl)",tznm,a.tm_gmtoff/60);
   return pyobj;
}
PyMethodDef methods[] = {{"offset",offset,METH_VARARGS},{NULL, NULL}};
void inittztool(void) {
    (void) Py_InitModule("tztool", methods);
}





More information about the Python-list mailing list