[egenix-users] date display behavior in mxdatetime 3

M.-A. Lemburg mal at egenix.com
Fri Feb 29 21:43:20 CET 2008


On 2008-02-29 21:15, M.-A. Lemburg wrote:
> On 2008-02-29 20:58, M.-A. Lemburg wrote:
>> On 2008-02-29 20:51, Eugene Chow wrote:
>>> I've noticed that date display behavior has changed in version 3 of
>>> mxdatetime in an undesirable way:
>>>
>>> Python 2.4.3 (#1, Apr  7 2006, 10:54:33)
>>> [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
>>> Type "help", "copyright", "credits" or "license" for more information.
>>>>>> import mx.DateTime
>>>>>> mx.DateTime.__version__
>>> '3.0.0'
>>>>>> d=mx.DateTime.Date(2008,1,-1)
>>>>>> d
>>> <mx.DateTime.DateTime object for '2008-01--1 00:00:00.00' at 63e90>
>>>>>> d.strftime()
>>> 'Thu Jan -1 00:00:00 2008'
>>>>>> d==mx.DateTime.Date(2008,1,31)
>>> True
>>> In version 2, the day is displayed as the actual last day of the month
>>> and not "-1".
>>>
>>> Was this change intentional?
>> No, that's a bug. Thanks for reporting it. We'll fix it in 3.0.1.

Sorry, false alarm on my part: this was already fixed in our repo. That's
why my recompile made the problem go away :-)

Here's the patch:

Index: mxDateTime.c
===================================================================
--- mxDateTime.c        (revision 22704)
+++ mxDateTime.c        (working copy)
@@ -12,7 +12,7 @@
 */

 /* Version number: Major.Minor.Patchlevel */
-#define MXDATETIME_VERSION "3.0.0"
+#define MXDATETIME_VERSION "3.0.1"

 /* Define this to aid in finding memory leaks */
 /*#define MAL_MEM_DEBUG*/
@@ -766,21 +766,25 @@
     return -1;
 }

-/* Calculate the absolute date, year offset and whether the year is a
-   leap year or not.

+/* Normalize the data and calculate the absolute date, year offset and
+   whether the year is a leap year or not.
+
    Returns -1 in case of an error, 0 otherwise.

 */

 static
-int mxDateTime_CalculateAbsDate(int year,
-                               int month,
-                               int day,
-                               int calendar,
-                               long *absdate_output,
-                               long *yearoffset_output,
-                               int *leap_output)
+int mxDateTime_NormalizedDate(int year,
+                             int month,
+                             int day,
+                             int calendar,
+                             long *absdate_output,
+                             long *yearoffset_output,
+                             int *leap_output,
+                             int *normalized_year,
+                             int *normalized_month,
+                             int *normalized_day)
 {
     int leap;
     long yearoffset, absdate;
@@ -792,7 +796,7 @@
                     year);

     /* Is it a leap year ? */
-    leap = mxDateTime_Leapyear(year,calendar);
+    leap = mxDateTime_Leapyear(year, calendar);

     /* Negative month values indicate months relative to the years end */
     if (month < 0)
@@ -816,9 +820,9 @@

     absdate = day + month_offset[leap][month - 1] + yearoffset;

-    DPRINTF("mxDateTime_SetFromDateAndTime: "
-           "yearoffset=%ld leap=%i absdate=%ld\n",
-           yearoffset,leap,absdate);
+    DPRINTF("mxDateTime_NormalizedDate: "
+           "year=%i month=%i day=%i yearoffset=%ld leap=%i absdate=%ld\n",
+           year, month, day, yearoffset, leap, absdate);

     if (absdate_output)
        *absdate_output = absdate;
@@ -826,12 +830,39 @@
        *yearoffset_output = yearoffset;
     if (leap_output)
        *leap_output = leap;
+    if (normalized_year)
+       *normalized_year = year;
+    if (normalized_month)
+       *normalized_month = month;
+    if (normalized_day)
+       *normalized_day = day;
     return 0;

  onError:
     return -1;
 }

+
+/* Return the absolute date of the given date in absdate.
+
+   Returns -1 in case of an error, 0 otherwise.
+
+*/
+
+static
+int mxDateTime_AbsDate(register int year,
+                      register int month,
+                      register int day,
+                      int calendar,
+                      long *absdate)
+{
+    return mxDateTime_NormalizedDate(year, month, day,
+                                    calendar,
+                                    absdate, NULL, NULL,
+                                    NULL, NULL, NULL);
+}
+
+
 /* Sets the date part of the DateTime object using the indicated
    calendar.

@@ -983,12 +1014,16 @@
     {
        long yearoffset,absdate;

-       if (mxDateTime_CalculateAbsDate(year, month, day, calendar,
-                                       &absdate, &yearoffset, NULL))
+       if (mxDateTime_NormalizedDate(year, month, day,
+                                     calendar,
+                                     &absdate, &yearoffset, NULL,
+                                     &year, &month, &day))
            goto onError;
        DPRINTF("mxDateTime_SetFromDateAndTime: "
-               "yearoffset=%ld absdate=%ld\n",
-               yearoffset,absdate);
+               "yearoffset=%ld absdate=%ld ",
+               "year=%i month=%i day=%i (normalized)\n",
+               yearoffset,absdate,
+               year,month,day);

        datetime->absdate = absdate;

@@ -3125,14 +3160,12 @@
            else if (mx_PyDate_Check(other->argument)) {
                /* DateTime - PyDate */
                long absdate;
-               mxDateTime_CalculateAbsDate(
-                   PyDateTime_GET_YEAR(other->argument),
-                   PyDateTime_GET_MONTH(other->argument),
-                   PyDateTime_GET_DAY(other->argument),
-                   MXDATETIME_GREGORIAN_CALENDAR,
-                   &absdate,
-                   NULL,
-                   NULL);
+               if (mxDateTime_AbsDate(PyDateTime_GET_YEAR(other->argument),
+                                      PyDateTime_GET_MONTH(other->argument),
+                                      PyDateTime_GET_DAY(other->argument),
+                                      MXDATETIME_GREGORIAN_CALENDAR,
+                                      &absdate))
+                   goto onError;
                return mxDateTimeDelta_FromDaysEx(
                    self->absdate - absdate,
                    self->abstime);
@@ -3146,14 +3179,12 @@
                  + (double)PyDateTime_DATE_GET_SECOND(other->argument)
                  + (double)PyDateTime_DATE_GET_MICROSECOND(other->argument) * 1e-6);
                long absdate;
-               mxDateTime_CalculateAbsDate(
-                   PyDateTime_GET_YEAR(other->argument),
-                   PyDateTime_GET_MONTH(other->argument),
-                   PyDateTime_GET_DAY(other->argument),
-                   MXDATETIME_GREGORIAN_CALENDAR,
-                   &absdate,
-                   NULL,
-                   NULL);
+               if (mxDateTime_AbsDate(PyDateTime_GET_YEAR(other->argument),
+                                      PyDateTime_GET_MONTH(other->argument),
+                                      PyDateTime_GET_DAY(other->argument),
+                                      MXDATETIME_GREGORIAN_CALENDAR,
+                                      &absdate))
+                   goto onError;
                return mxDateTimeDelta_FromDaysEx(
                    self->absdate - absdate,
                    self->abstime - abstime);



-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Feb 29 2008)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

:::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611



More information about the egenix-users mailing list