Add support for Python 2.3 datetime classes.

This commit is contained in:
adustman
2003-07-10 15:58:56 +00:00
parent b6509148a2
commit 03a0fcdd1f
2 changed files with 8 additions and 81 deletions

View File

@ -1,5 +1,7 @@
0.9.3a2 0.9.3a2
* Add some support for Python 2.3 datetime classes.
* Removed some casts in _mysql_init() that caused problems with * Removed some casts in _mysql_init() that caused problems with
Python 2.3. The type for tp_free was changed from destructor Python 2.3. The type for tp_free was changed from destructor
to freefunc. Removing the casts make both 2.2 and 2.3 happy. to freefunc. Removing the casts make both 2.2 and 2.3 happy.

View File

@ -3,93 +3,18 @@
This module provides some Date and Time classes for dealing with MySQL data. This module provides some Date and Time classes for dealing with MySQL data.
""" """
from time import strftime, localtime
from _mysql import string_literal from _mysql import string_literal
try: try:
try: from pytimes import *
# new packaging
from mx.DateTime import Date, Time, Timestamp, ISO, \
DateTimeType, DateTimeDeltaType
except ImportError: except ImportError:
# old packaging, deprecated try:
from DateTime import Date, Time, Timestamp, ISO, \ from mxdatetimes import *
DateTimeType, DateTimeDeltaType
def DateFromTicks(ticks):
"""Convert UNIX ticks into a mx.DateTime.Date."""
return apply(Date, localtime(ticks)[:3])
def TimeFromTicks(ticks):
"""Convert UNIX ticks into a mx.DateTime.Time."""
return apply(Time, localtime(ticks)[3:6])
def TimestampFromTicks(ticks):
"""Convert UNIX ticks into a mx.DateTime.Timestamp."""
return apply(Timestamp, localtime(ticks)[:6])
def format_DATE(d):
"""Format a DateTime object as an ISO date."""
return d.strftime("%Y-%m-%d")
def format_TIME(d):
"""Format a DateTime object as a time value."""
return d.strftime("%H:%M:%S")
def format_TIMESTAMP(d):
"""Format a DateTime object as an ISO timestamp."""
return d.strftime("%Y-%m-%d %H:%M:%S")
def DateTime_or_None(s):
try: return ISO.ParseDateTime(s)
except: return None
def TimeDelta_or_None(s):
try: return ISO.ParseTimeDelta(s)
except: return None
def Date_or_None(s):
try: return ISO.ParseDate(s)
except: return None
except ImportError: except ImportError:
# no DateTime? We'll muddle through somehow. # no DateTime? We'll muddle through somehow.
from stringtimes import *
DateTimeDeltaType = "DateTimeDeltaType"
DateTimeType = "DateTimeType"
def Date(year, month, day):
"""Construct an ISO date string."""
return "%04d-%02d-%02d" % (year, month, day)
def Time(hour, min, sec):
"""Construct a TIME string."""
return "%02d:%02d:%02d" % (hour, min, sec)
def Timestamp(year, month, day, hour, min, sec):
"""Construct an ISO timestamp."""
return "%04d-%02d-%02d %02d:%02d:%02d" % \
(year, month, day, hour, min, sec)
def DateFromTicks(ticks):
"""Convert UNIX ticks to ISO date format."""
return strftime("%Y-%m-%d", localtime(ticks))
def TimeFromTicks(ticks):
"""Convert UNIX ticks to time format."""
return strftime("%H:%M:%S", localtime(ticks))
def TimestampFromTicks(ticks):
"""Convert UNIX ticks to ISO timestamp format."""
return strftime("%Y-%m-%d %H:%M:%S", localtime(ticks))
def format_DATE(d):
"""Format a date as a date (does nothing, you don't have mx.DateTime)."""
return d
format_TIME = format_TIMESTAMP = format_DATE
TimeDelta_or_None = Date_or_None = DateTime_or_None = format_DATE
def DateTime2literal(d, c): def DateTime2literal(d, c):
"""Format a DateTime object as an ISO timestamp.""" """Format a DateTime object as an ISO timestamp."""