[Zodb-checkins] CVS: ZODB3/Persistence - TimeStamp.c:1.3.2.2

Tim Peters tim.one at comcast.net
Mon Jul 7 18:07:15 EDT 2003


Update of /cvs-repository/ZODB3/Persistence
In directory cvs.zope.org:/tmp/cvs-serv19279/Persistence

Modified Files:
      Tag: zodb33-devel-branch
	TimeStamp.c 
Log Message:
Give the timestamp type a str() method.
Remove the test that insists both .second() and .seconds() exist, and
are aliases -- TOOWTDI, and no other component accessor here has two
spellings.


=== ZODB3/Persistence/TimeStamp.c 1.3.2.1 => 1.3.2.2 ===
--- ZODB3/Persistence/TimeStamp.c:1.3.2.1	Tue Jul  1 15:28:11 2003
+++ ZODB3/Persistence/TimeStamp.c	Mon Jul  7 17:06:40 2003
@@ -1,14 +1,14 @@
 /*****************************************************************************
 
   Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
-  
+
   This software is subject to the provisions of the Zope Public License,
   Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
   WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
   FOR A PARTICULAR PURPOSE
-  
+
  ****************************************************************************/
 
 #include "Python.h"
@@ -17,7 +17,7 @@
 PyObject *TimeStamp_FromDate(int, int, int, int, int, double);
 PyObject *TimeStamp_FromString(const char *);
 
-static char TimeStampModule_doc[] = 
+static char TimeStampModule_doc[] =
 "A 64-bit TimeStamp used as a ZODB serial number.\n";
 
 typedef struct {
@@ -28,7 +28,7 @@
 /* The first dimension of the arrays below is non-leapyear / leapyear */
 
 static char month_len[2][12]={
-  {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, 
+  {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
   {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
 };
 
@@ -60,7 +60,7 @@
     double d, s;
 
     y -= 1900;
-  
+
     d = (y - 1) * 365;
     if (y > 0) {
         s = 1.0;
@@ -83,14 +83,14 @@
 {
     struct tm *t;
     time_t z=0;
-  
+
     t = gmtime(&z);
     if (t == NULL) {
 	PyErr_SetString(PyExc_SystemError, "gmtime failed");
 	return -1;
     }
 
-    gmoff = TimeStamp_abst(t->tm_year+1900, t->tm_mon, t->tm_mday - 1, 
+    gmoff = TimeStamp_abst(t->tm_year+1900, t->tm_mon, t->tm_mday - 1,
 			   t->tm_hour * 60 + t->tm_min, t->tm_sec);
 
     return 0;
@@ -127,15 +127,19 @@
 }
 
 typedef struct {
-    int y, m, d, mi;
+    /* XXX reverse-engineer what's in these things and comment them */
+    int y;
+    int m;
+    int d;
+    int mi;
 } TimeStampParts;
-  
-static void 
+
+static void
 TimeStamp_unpack(TimeStamp *self, TimeStampParts *p)
 {
     unsigned long v;
 
-    v = (self->data[0] * 16777216 + self->data[1] * 65536 
+    v = (self->data[0] * 16777216 + self->data[1] * 65536
 	 + self->data[2] * 256 + self->data[3]);
     p->y = v / 535680 + 1900;
     p->m = (v % 535680) / 44640 + 1;
@@ -215,6 +219,22 @@
 }
 
 static PyObject *
+TimeStamp_str(TimeStamp *self)
+{
+    char buf[128];
+    TimeStampParts p;
+    int len;
+
+    TimeStamp_unpack(self, &p);
+    len =sprintf(buf, "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%09.6f",
+	         p.y, p.m, p.d, p.mi / 60, p.mi % 60,
+	         TimeStamp_sec(self));
+
+    return PyString_FromStringAndSize(buf, len);
+}
+
+
+static PyObject *
 TimeStamp_laterThan(TimeStamp *self, PyObject *obj)
 {
     TimeStamp *o = NULL;
@@ -292,7 +312,7 @@
     0,					/* tp_as_mapping */
     (hashfunc)TimeStamp_hash,		/* tp_hash */
     0,					/* tp_call */
-    0,					/* tp_str */
+    (reprfunc)TimeStamp_str,		/* tp_str */
     0,					/* tp_getattro */
     0,					/* tp_setattro */
     0,					/* tp_as_buffer */
@@ -329,14 +349,14 @@
     }
 
 PyObject *
-TimeStamp_FromDate(int year, int month, int day, int hour, int min, 
+TimeStamp_FromDate(int year, int month, int day, int hour, int min,
 		   double sec)
 {
     TimeStamp *ts = NULL;
     int d;
     unsigned int v;
 
-    if (year < 1900) 
+    if (year < 1900)
 	return PyErr_Format(PyExc_ValueError,
 			    "year must be greater than 1900: %d", year);
     CHECK_RANGE(month, 1, 12);
@@ -412,5 +432,3 @@
     TimeStamp_type.ob_type = &PyType_Type;
     TimeStamp_type.tp_getattro = PyObject_GenericGetAttr;
 }
- 
-




More information about the Zodb-checkins mailing list