[Zope] - strftime for DateTime
skip@calendar.com (Skip Montanaro)
skip@calendar.com (Skip Montanaro)
Wed, 6 Jan 1999 20:07:01 -0500
The appended context diff seems to do what Jim asked for. Here's a simple
session:
>>> import DateTime, time
>>> dt = DateTime.DateTime(time.time())
>>> dt.strftime("%Y-%m-%d")
'1999-01-07'
>>> getattr(dt, "%Y-%m-%d")
<DateTime.DateTime.strftimeFormatter instance at 8116570>
>>> getattr(dt, "%Y-%m-%d")()
'1999-01-07'
(Jim actually did all the work. I made sure the necessary time functions
were available at the module level and generated a context diff... :-)
Skip Montanaro | Mojam: "Uniting the World of Music" http://www.mojam.com/
skip@calendar.com | Musi-Cal: http://concerts.calendar.com/
518-372-5583
*** /tmp/DateTime.py.~1~ Wed Jan 6 20:00:11 1999
--- /tmp/DateTime.py Wed Jan 6 20:00:11 1999
***************
*** 101,107 ****
import sys,os,regex,DateTimeZone
from string import strip,split,upper,lower,atoi,atof,find,join
! from time import time,gmtime,localtime,asctime,tzname
from types import InstanceType,IntType,FloatType,StringType
--- 101,107 ----
import sys,os,regex,DateTimeZone
from string import strip,split,upper,lower,atoi,atof,find,join
! from time import time,gmtime,localtime,asctime,tzname,strftime,mktime
from types import InstanceType,IntType,FloatType,StringType
***************
*** 854,859 ****
--- 854,862 ----
def _validTime(self,h,m,s):
return h>=0 and h<=23 and m>=0 and m<=59 and s>=0 and s<=59
+ def __getattr__(self, name):
+ if '%' in name: return strftimeFormatter(self, name)
+ raise AttributeError, name
# Conversion and comparison methods
***************
*** 1117,1122 ****
--- 1120,1127 ----
"""Return the second"""
return self._second
+ def strftime(self, format):
+ return strftime(format, gmtime(self.timeTime()))
# General formats from previous DateTime
***************
*** 1294,1299 ****
--- 1299,1312 ----
"""Convert to floating-point number of days since Jan. 1, 1901 (gmt)"""
return float(self._d)
+
+ class strftimeFormatter:
+
+ def __init__(self, dt, format):
+ self._dt=dt
+ self._f=format
+
+ def __call__(self): return self._dt.strftime(self._f)
# Module methods