Rev 1277: Rewrite time_from_cstring in Python. in http://people.samba.org/bzr/jelmer/bzr-svn/0.4

Jelmer Vernooij jelmer at samba.org
Sun Jun 22 19:53:16 BST 2008


At http://people.samba.org/bzr/jelmer/bzr-svn/0.4

------------------------------------------------------------
revno: 1277
revision-id: jelmer at samba.org-20080622185314-1teaotmb43lvteod
parent: jelmer at samba.org-20080622163713-wy6jr0thzomqyqlt
committer: Jelmer Vernooij <jelmer at samba.org>
branch nick: 0.4
timestamp: Sun 2008-06-22 20:53:14 +0200
message:
  Rewrite time_from_cstring in Python.
modified:
  core.c                         core.pyx-20080313210413-17k59slolpfe5kdq-1
  mapping.py                     mapping.py-20080128201303-6cp01phc0dmc0kiv-1
  properties.py                  util.py-20080502170127-o220e9py99vt69s6-1
  tests/test_core.py             test_core.py-20080603032119-q91zmret1lv84ay9-1
=== modified file 'core.c'
--- a/core.c	2008-06-22 16:37:13 +0000
+++ b/core.c	2008-06-22 18:53:14 +0000
@@ -27,56 +27,18 @@
 
 #include "util.h"
 
-/** Parse a Subversion time string and return a UNIX timestamp. */
-static PyObject *time_from_cstring(PyObject *self, PyObject *args)
-{
-    apr_time_t when;
-    apr_pool_t *pool;
-	char *data;
-
-	if (!PyArg_ParseTuple(args, "s", &data))
-		return NULL;
-
-    pool = Pool(NULL);
-	if (pool == NULL)
-		return NULL;
-    RUN_SVN_WITH_POOL(pool, svn_time_from_cstring(&when, data, pool));
-    apr_pool_destroy(pool);
-    return PyLong_FromLongLong(when);
-}
-
-typedef struct {
-	PyObject_HEAD
-	svn_config_t *item;
-} ConfigObject;
-
-PyTypeObject Config_Type = {
-	PyObject_HEAD_INIT(NULL) 0,
-	.tp_name = "core.Config",
-	.tp_basicsize = sizeof(ConfigObject),
-	.tp_dealloc = (destructor)PyObject_Del,
-};
-
-static PyMethodDef core_methods[] = {
-	{ "time_from_cstring", time_from_cstring, METH_VARARGS, NULL },
-	{ NULL, }
-};
-
 void initcore(void)
 {
 	static apr_pool_t *pool;
 	PyObject *mod;
 
-	if (PyType_Ready(&Config_Type) < 0)
-		return;
-
 	apr_initialize();
 	pool = Pool(NULL);
 	if (pool == NULL)
 		return;
 	svn_utf_initialize(pool);
 
-	mod = Py_InitModule3("core", core_methods, "Core functions");
+	mod = Py_InitModule3("core", NULL, "Core functions");
 	if (mod == NULL)
 		return;
 

=== modified file 'mapping.py'
--- a/mapping.py	2008-06-22 08:45:53 +0000
+++ b/mapping.py	2008-06-22 18:53:14 +0000
@@ -150,7 +150,7 @@
             pass
 
     if svn_revprops.has_key(properties.PROP_REVISION_DATE):
-        rev.timestamp = core.time_from_cstring(svn_revprops[properties.PROP_REVISION_DATE]) / 1000000.0
+        rev.timestamp = properties.time_from_cstring(svn_revprops[properties.PROP_REVISION_DATE]) / 1000000.0
     else:
         rev.timestamp = 0.0 # FIXME: Obtain repository creation time
     rev.timezone = None

=== modified file 'properties.py'
--- a/properties.py	2008-06-22 16:37:13 +0000
+++ b/properties.py	2008-06-22 18:53:14 +0000
@@ -28,6 +28,14 @@
             tm_sec, tm_wday, tm_yday, tm_isdst) = time.gmtime(timestamp / 1000000)
     return "%04d-%02d-%02dT%02d:%02d:%02d.%06dZ" % (tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_usec)
 
+def time_from_cstring(text):
+    import time
+    (basestr, usecstr) = text.split(".", 1)
+    assert usecstr[-1] == "Z"
+    tm_usec = int(usecstr[:-1])
+    tm = time.strptime(basestr, "%Y-%m-%dT%H:%M:%S")
+    return (long(time.mktime((tm[0], tm[1], tm[2], tm[3], tm[4], tm[5], tm[6], tm[7], -1)) - time.timezone) * 1000000 + tm_usec)
+
 
 PROP_EXECUTABLE = 'svn:executable'
 PROP_EXECUTABLE_VALUE = '*'

=== modified file 'tests/test_core.py'
--- a/tests/test_core.py	2008-06-22 16:37:13 +0000
+++ b/tests/test_core.py	2008-06-22 18:53:14 +0000
@@ -26,7 +26,7 @@
         self.assertIsInstance(core.SubversionException("foo", 1), Exception)
 
     def test_time_from_cstring(self):
-        self.assertEquals(1225704780716938L, core.time_from_cstring("2008-11-03T09:33:00.716938Z"))
+        self.assertEquals(1225704780716938L, properties.time_from_cstring("2008-11-03T09:33:00.716938Z"))
 
     def test_time_to_cstring(self):
         self.assertEquals("2008-11-03T09:33:00.716938Z", properties.time_to_cstring(1225704780716938L))




More information about the bazaar-commits mailing list