IMHO, the example's code is good, but not cool enough to be put in the book. Every programmer saw those ordinary if...elif...elif...else cliche in their first program language book before. People can probably write their own if...elif...else in less time, than STFW or RTFM to find our prettydate().
So, we need some extra flavor to compensate the time a guy spent to find our prettydate(). How about making it i18n-friendly? Something like this. class PrettyDate(object): def __init__(self,T): self.T=T # def __call__(self, d): dt = datetime.now() - d if dt.days >= 2*365: return self.T('%d years ago') % int(dt.days / 365) elif ...: ...... Then, in the model we can initialize an instance by: prettydate = PrettyDate(T) In controller we can do: prettydate(d) Of course developers need to localize their own language strings in their own environment. Just my $0.02 On Feb16, 1:21pm, Jason Brower <encomp...@gmail.com> wrote: > These examples would be a very good edition to the book > -- > J > > > > On Mon, 2010-02-15 at 16:19 -0800, mdipierro wrote: > > for lack of a better option I put it in tools for now. > > > On Feb 15, 5:56 pm, Richard <richar...@gmail.com> wrote: > > > added a few more cases: > > > > def prettydate(d): > > > try: > > > dt = datetime.now() - d > > > except: > > > return '' > > > if dt.days >= 2*365: > > > return '%d years ago' % int(dt.days / 365) > > > elif dt.days >= 365: > > > return '1 year ago' > > > elif dt.days >= 60: > > > return '%d months ago' % int(dt.days / 30) > > > elif dt.days > 21: > > > return '1 month ago' > > > elif dt.days >= 14: > > > return '%d weeks ago' % int(dt.days / 7) > > > elif dt.days >= 7: > > > return '1 week ago' > > > elif dt.days > 1: > > > return '%d days ago' % dt.days > > > elif dt.days == 1: > > > return '1 day ago' > > > elif dt.seconds >= 2*60*60: > > > return '%d hours ago' % int(dt.seconds / 3600) > > > elif dt.seconds >= 60*60: > > > return '1 hour ago' > > > elif dt.seconds >= 2*60: > > > return '%d minutes ago' % int(dt.seconds / 60) > > > elif dt.seconds >= 60: > > > return '1 minute ago' > > > elif dt.seconds > 1: > > > return '%d seconds ago' % dt.seconds > > > elif dt.seconds == 1: > > > return '1 second ago' > > > else: > > > return 'now' > > > > > On Feb 14, 3:13 pm, selecta <gr...@delarue-berlin.de> wrote: > > > > > > This is not 100% web2py related but I am sure if this will be answered > > > > > many of you will profit at some point from it. > > > > > > Is there a python module that helps you to display dates and times > > > > > nice e.g. > > > > > > just now (for within the last 5 minutes) > > > > > 2 hours ago > > > > > 2 days ago > > > > > 15th February 2009 > > > > > ... > > > > > > I guess somebody must have done that already, right? -- You received this message because you are subscribed to the Google Groups "web2py-users" group. To post to this group, send email to web...@googlegroups.com. To unsubscribe from this group, send email to web2py+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/web2py?hl=en.