DateTime formatting with strftime: patch
Here's a working patch to make formatting of a DateTime instance work as generally expected, when you use <dtml-var "_.DateTime()" fmt="%d %G %z"> or whatever. It could be made more efficient by compiling the two regex (regexes? regexen? regular expressions :-) ) into class attributes. Here's the method I changed: def strftime(self, format): diff=_tzoffset(self._tz, self._t) format = ts_regex.gsub('\(^\|[^%]\)%Z', '\\1'+self._tz, format) format = ts_regex.gsub('\(^\|[^%]\)%z', '\\1%+05d' % (diff/36), format) return strftime(format, gmtime(self.timeTime()+diff)) Instead of just calling gmtime with the time as held in the DateTime instace, it subverts time.strftime into formatting the time shifted according to the instance's timezone. Because time.strftime won't format the timezone information correctly, this is done using regular expressions before the format specification string is passed to time.strftime. -- Steve Alexander Software Engineer Cat-Box limited http://www.cat-box.net *** lib/python/DateTime/DateTime.old.py Sun Jul 23 20:03:04 2000 --- lib/python/DateTime/DateTime.py Mon Jul 24 14:01:37 2000 *************** *** 1376,1382 **** return millis def strftime(self, format): ! return strftime(format, gmtime(self.timeTime())) # General formats from previous DateTime def Date(self): --- 1376,1385 ---- return millis def strftime(self, format): ! diff=_tzoffset(self._tz, self._t) ! format = ts_regex.gsub('\(^\|[^%]\)%Z', '\\1'+self._tz, format) ! format = ts_regex.gsub('\(^\|[^%]\)%z', '\\1%+05d' % (diff/36), format) ! return strftime(format, gmtime(self.timeTime()+diff)) # General formats from previous DateTime def Date(self):
This is now in the Collector, as bug number 1457. http://classic.zope.org:8080/Collector/1457/view Incidentally, http://classic.zope.org:8080/Collector/1409/view is another patch to the same problem. However, that patch doesn't address the root of the problem, which is that DateTime instances are associated with a particular timezone, and users expect all formatting to occur with respect to that particular timezone. The fix in collector #1409 makes all strftime formatting happen in the local timezone of the Zope installation, not the timezone given in the particular DateTime instance being formatted. Steve Alexander wrote:
Here's a working patch to make formatting of a DateTime instance work as generally expected, when you use
<dtml-var "_.DateTime()" fmt="%d %G %z">
or whatever.
It could be made more efficient by compiling the two regex (regexes? regexen? regular expressions :-) ) into class attributes.
There's a problem with this optimisation -- see my other posting to zope-dev today. -- Steve Alexander Software Engineer Cat-Box limited http://www.cat-box.net
participants (1)
-
Steve Alexander