[Zope] - Convert database date to Zope date
Jim Fulton
jim.fulton@digicool.com
Wed, 06 Jan 1999 17:18:12 -0500
skip@calendar.com wrote:
>
> Michael> How does one convert a database date value to a Zope date
> Michael> value. I'm trying to use a date value retrieved from a query of
> Michael> a MySQL database in the var command with the date formatting
> Michael> options. If I just use the raw value from the query, I get an
> Michael> error. Is there a way to convert this to a Zope date data type?
>
> I don't know sure what sort of value MySQL returns,
I was wondering that myself. Does MySQL have any sort of Date type?
> but in my application I
> store timestamps as floating point values (such as returned by
> time.time()). I was able to access them from DTML by converting them to
> DateTime objects, e.g.:
>
> e.start = DateTime.DateTime(e.start)
There really should be access to the DateTime constructor
from DTML. I'll add it to '_', so in the next release
you'll be able to:
<!--#var "_.DateTime(v)"-->
Note that the DateTime class is documented in
appendix A of the DTML manual.
> then access it as appropriate from DTML:
>
> <!--#var start fmt=aDay--> ...
>
> I was a bit disappointed to discover there is no exposure of strftime
> functionality through DateTime objects. The static date formats provided
> don't meet my needs. To generate a date in the format I want I wind up
> making at least four #var references per date. If there's no strong
> objection, I will add an ftime (or strftime) method to DateTime objects that
> can at least be called through a var tag with an expr attribute.
This is a good idea. I was thinking about this a long time ago
and thought I'd try to wire something like this into getattr so that
it would work with the fmt machinery. Something like:
class DateTime:
...
def strftime(self, format): ...
def __getattr__(self, name):
if '%' in name: return strftimeFormatter(self, name)
raise AttributeError, name
class strftimeFormatter:
def __init__(self, dt, format):
self._dt=dt
self._f=format
def __call__(self): return self._dt.strftime(self._f)
This should let you:
<!--#var someDateTime fmt="%Y/%m/%d"-->
I'd be happy to get a patch along these lines. :)
Jim
--
Jim Fulton mailto:jim@digicool.com
Technical Director (888) 344-4332 Python Powered!
Digital Creations http://www.digicool.com http://www.python.org
Under US Code Title 47, Sec.227(b)(1)(C), Sec.227(a)(2)(B) This email
address may not be added to any commercial mail list with out my
permission. Violation of my privacy with advertising or SPAM will
result in a suit for a MINIMUM of $500 damages/incident, $1500 for
repeats.