def age(curdate): """ turns a datetime into an age string. :param curdate: datetime object :rtype: unicode :returns: unicode words describing age """ from datetime import datetime from webhelpers.date import time_ago_in_words _ = lambda s:s if not curdate: return '' agescales = [(_(u"year"), 3600 * 24 * 365), (_(u"month"), 3600 * 24 * 30), (_(u"day"), 3600 * 24), (_(u"hour"), 3600), (_(u"minute"), 60), (_(u"second"), 1), ] age = datetime.now() - curdate age_seconds = (age.days * agescales[2][1]) + age.seconds pos = 1 for scale in agescales: if scale[1] <= age_seconds: if pos == 6:pos = 5 return '%s %s' % (time_ago_in_words(curdate, agescales[pos][0]), _('ago')) pos += 1 return _(u'just now')
def timestamp(self): today = datetime.date.today() yesterday = today - datetime.timedelta(days=1) if self.time.date() == today: return time_ago_in_words(self.time, granularity="minute") + " ago" elif self.time.date() == yesterday: return self.time.strftime("yesterday at %I:%M%p").lower() else: return self.time.strftime("%a %b %d, %Y")
def created_in_words(self): #TODO # value /= 1000 # if value > 365 * 24 * 60 * 60: # granularity = 'month' # else: # granularity = 'day' granularity = 'day' return time_ago_in_words(self.created, granularity=granularity, round=True)
def time_ago(from_time): if not from_time: return None if isinstance(from_time, basestring): from_time = api_datestr_to_datetime(from_time) time_ago = time_ago_in_words(from_time, granularity='minute', round=True) match = re.match("^(less than ){0,1}\d+ [a-z]+", time_ago) if match: part = match.group(0) part = part.replace("minute", "min") part = part.replace("second", "sec") return part else: user_log.error("Failed to shorten time_ago:"+time_ago) return time_ago
def profile(request): session = request.session user = UserData.get_user(session['email']) if user[0]: print user[1].user_reg_date user_dict = { 'email': user[1].user_email, 'name': user[1].user_name, 'reg_date': user[1].user_reg_date.ctime(), 'last_log_in': time_ago_in_words(user[1].user_last_logged_on, granularity='minute'), 'country': user[1].user_country } profile_name = user[1].user_name return {'user_dict': user_dict, 'profile_name': profile_name} else: return {}
def age(curdate): """ turns a datetime into an age string. :param curdate: datetime object :rtype: unicode :returns: unicode words describing age """ from datetime import datetime from webhelpers.date import time_ago_in_words _ = lambda s: s if not curdate: return '' agescales = [ (_(u"year"), 3600 * 24 * 365), (_(u"month"), 3600 * 24 * 30), (_(u"day"), 3600 * 24), (_(u"hour"), 3600), (_(u"minute"), 60), (_(u"second"), 1), ] age = datetime.now() - curdate age_seconds = (age.days * agescales[2][1]) + age.seconds pos = 1 for scale in agescales: if scale[1] <= age_seconds: if pos == 6: pos = 5 return '%s %s' % (time_ago_in_words(curdate, agescales[pos][0]), _('ago')) pos += 1 return _(u'just now')
def created_in_words(self): return time_ago_in_words(self.created)
def time_ago_in_words_from_str(date_str, granularity='month'): if date_str: return date.time_ago_in_words(date_str_to_datetime(date_str), granularity=granularity) else: return _('Unknown')
def last_seen_time_ago(self): return time_ago_in_words(self.last_seen)
def getTimeWords(self): return time_ago_in_words(self.getTime())
def created_in_words(self): return time_ago_in_words(self.created, granularity="minute")
def time_ago_in_words_from_str(date_str, granularity='month'): return date.time_ago_in_words(date_str_to_datetime(date_str), granularity=granularity)