[Zope-dev] DateTime formatting with strftime: patch

Steve Alexander steve@cat-box.net
Mon, 24 Jul 2000 14:06:29 +0100


This is a multi-part message in MIME format.
--------------49CD36F8256B7E60F0F34930
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

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
--------------49CD36F8256B7E60F0F34930
Content-Type: text/plain; charset=us-ascii;
 name="datetime.pat"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="datetime.pat"

*** 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):

--------------49CD36F8256B7E60F0F34930--