def timeleft(self): ''' Retunrs time left till start or passed since start (string) ''' dt_now = datetime.utcnow() # If event has not yet started if self.start_datetime > dt_now: delta = self.start_datetime - dt_now if (delta.days >=0): # Less then a minute if (delta.seconds <=60): ret = str(delta.seconds) + " seconds" # Using default timeutil. else: ret = timeuntil(self.start_datetime) return "Starts in " + ret else: # Event has already started # If event has not ended yet if self.end_datetime > dt_now: # Passed less then minutes. delta = dt_now - self.start_datetime if delta.seconds <= 60: #(delta.days == -1 and delta.seconds >= 86340): ret = ret = str(delta.seconds) + " seconds" #str(86400 - delta.seconds) + " seconds" + " ago" else: ret = timeuntil(datetime.utcnow(), self.start_datetime) + " ago" return "Started " + ret else: return "Ended"
def time_info(self): if self.previous_round and not self.start: return _('starts when %s is finished') % self.previous_round if self.is_not_yet_started(): return _('starts in %s') % timeuntil(self.start) if self.is_in_progress(): return _('ends in %s') % timeuntil(self.end) return _('ended %s ago') % timesince(self.end)
def get_expiration_date(self, obj): if not obj.expiration_date: return mark_safe('<span style="color:green">nunca expira</span>') if obj.expiration_date and obj.expiration_date <= datetime.now(): return mark_safe('<span style="color:orange">expiro hace %s</span>' % timesince(obj.expiration_date)) return mark_safe('<span style="color:green">expira en %s</span>' % timeuntil(obj.expiration_date))
def zeropush_new_job(job): # Get sellers matching seller title sellers = Seller.objects.filter(titles=job.title).exclude( user=job.buyer).all() # Get sellers within distance suitable_sellers = [] for seller in sellers: kwargs = { 'lat1': float(job.geoposition.latitude), 'long1': float(job.geoposition.longitude), 'lat2': float(seller.geoposition.latitude), 'long2': float(seller.geoposition.longitude), } if distance_in_kilometers(**kwargs) <= seller.distance: suitable_sellers.append(seller) # Get suitable user devices if len(suitable_sellers) > 0: devices = [] for seller in suitable_sellers: devices += seller.user.pushdevice_set.all() # Notify devices if len(devices) > 0: if job.start_asap: alert = _(u"{title} ASAP").format(title=job.title.title) else: alert = _(u"{title} in {start_on}").format( title=job.title.title, start_on=timeuntil(job.start_on)) print alert zeropush.notify_devices(devices, alert=alert)
def get_next_time(obj): """Get humanized form of next run.""" next_ = obj.next_time if next_ is None: return dst_date = now() + timedelta(seconds=next_) return timeuntil(dst_date)
def naturaltime(value): """ For date and time values shows how many seconds, minutes or hours ago compared to current timestamp returns representing string. """ if not isinstance(value, date): # datetime is a subclass of date return value now = datetime.now(utc if is_aware(value) else None) if value < now: delta = now - value if delta.days != 0: return pgettext( 'naturaltime', '%(delta)s ago' ) % {'delta': defaultfilters.timesince(value, now)} elif delta.seconds == 0: return _('now') elif delta.seconds < 60: return ungettext( # Translators: \\u00a0 is non-breaking space 'a second ago', '%(count)s\u00a0seconds ago', delta.seconds ) % {'count': delta.seconds} elif delta.seconds // 60 < 60: count = delta.seconds // 60 return ungettext( # Translators: \\u00a0 is non-breaking space 'a minute ago', '%(count)s\u00a0minutes ago', count ) % {'count': count} else: count = delta.seconds // 60 // 60 return ungettext( # Translators: \\u00a0 is non-breaking space 'an hour ago', '%(count)s\u00a0hours ago', count ) % {'count': count} else: delta = value - now if delta.days != 0: return pgettext( 'naturaltime', '%(delta)s from now' ) % {'delta': defaultfilters.timeuntil(value, now)} elif delta.seconds == 0: return _('now') elif delta.seconds < 60: return ungettext( # Translators: \\u00a0 is non-breaking space 'a second from now', '%(count)s\u00a0seconds from now', delta.seconds ) % {'count': delta.seconds} elif delta.seconds // 60 < 60: count = delta.seconds // 60 return ungettext( # Translators: \\u00a0 is non-breaking space 'a minute from now', '%(count)s\u00a0minutes from now', count ) % {'count': count} else: count = delta.seconds // 60 // 60 return ungettext( # Translators: \\u00a0 is non-breaking space 'an hour from now', '%(count)s\u00a0hours from now', count ) % {'count': count}
def timeuntil_or_forever(snippet_expire): ttl = snippet_expire - now() if ttl.days > NINETY_YEARS_IN_DAYS: # snippet TTL 'forever' is defined as 100 years, so if remaining TTL is more than # (90 * 365) days, we most probably got a snippet with TTL 'forever' return 'forever' return timeuntil(snippet_expire)
def get_publication_date(self, obj): if obj.publication_date and obj.publication_date <= timezone.now(): return mark_safe( '<span style="color:green">publicado hace %s</span>' % timesince(obj.publication_date)) return mark_safe('<span style="color:blue">se publica en %s</span>' % timeuntil(obj.publication_date))
def get_cfp_tiles(self, event, _now): result = [] max_deadline = event.cfp.max_deadline if max_deadline and _now < max_deadline: result.append( {'large': timeuntil(max_deadline), 'small': _('until the CfP ends')} ) if event.cfp.is_open: result.append({'url': event.cfp.urls.public, 'large': _('Go to CfP')}) return result
def get_expiration_date(self, obj): if not obj.expiration_date: return mark_safe('<span style="color:green">nunca expira</span>') if obj.expiration_date and obj.expiration_date <= timezone.now(): return mark_safe( '<span style="color:orange">expiro hace %s</span>' % timesince(obj.expiration_date)) return mark_safe('<span style="color:green">expira en %s</span>' % timeuntil(obj.expiration_date))
def get_cfp_tiles(self, _now): result = [] max_deadline = self.request.event.cfp.max_deadline if max_deadline and _now < max_deadline: result.append( {"large": timeuntil(max_deadline), "small": _("until the CfP ends")} ) if self.request.event.cfp.is_open: result.append( {"url": self.request.event.cfp.urls.public, "large": _("Go to CfP")} ) return result
def naturaldate(value): today = timezone.now().date() if value == today: return 'today' else: difference = value - today if difference.days < 7: template = ungettext_lazy('%d day', '%d days') result = avoid_wrapping(template % difference.days) else: result = defaultfilters.timeuntil(value) return pgettext('naturaltime', result)
def get_ending_point(self): #pdb.set_trace() if self.endingtype == self.choices.ending_type.TIME_LIMIT: return timeuntil(self.endingdate) elif self.endingtype == self.choices.ending_type.WORD_LIMIT: return self.wordlimit elif self.endingtype == self.choices.ending_type.INACTIVITY: d = self.writ.get_last_addition_date() dt = timedelta(seconds=self.inactive_time) return d + dt elif self.endingtype == self.choices.ending_type.INACTIVE_ROUND: return 'all players consecutively pass their turn' else: return self.endingtype
def get_ending_point (self): #pdb.set_trace() if self.endingtype == self.choices.ending_type.TIME_LIMIT: return timeuntil(self.endingdate) elif self.endingtype == self.choices.ending_type.WORD_LIMIT: return self.wordlimit elif self.endingtype == self.choices.ending_type.INACTIVITY: d = self.writ.get_last_addition_date() dt = timedelta(seconds=self.inactive_time) return d + dt elif self.endingtype == self.choices.ending_type.INACTIVE_ROUND: return 'all players consecutively pass their turn' else: return self.endingtype
def string_for(cls, value): if not isinstance(value, date): # datetime is a subclass of date return value now = datetime.now(utc if is_aware(value) else None) if value < now: delta = now - value if delta.days != 0: # The only change being done is here and in the else clausule: # It splits the string returned by timesince filter and gets only first element # to shorten the string so e.g '1 days, 5 hours' becomes '1 days' return cls.time_strings['past-day'] % { 'delta': defaultfilters.timesince( value, now, time_strings=cls.past_substrings).split(',')[0], } elif delta.seconds == 0: return cls.time_strings['now'] elif delta.seconds < 60: return cls.time_strings['past-second'] % { 'count': delta.seconds } elif delta.seconds // 60 < 60: count = delta.seconds // 60 return cls.time_strings['past-minute'] % {'count': count} else: count = delta.seconds // 60 // 60 return cls.time_strings['past-hour'] % {'count': count} else: delta = value - now if delta.days != 0: return cls.time_strings['future-day'] % { 'delta': defaultfilters.timeuntil( value, now, time_strings=cls.future_substrings).split(',')[0], } elif delta.seconds == 0: return cls.time_strings['now'] elif delta.seconds < 60: return cls.time_strings['future-second'] % { 'count': delta.seconds } elif delta.seconds // 60 < 60: count = delta.seconds // 60 return cls.time_strings['future-minute'] % {'count': count} else: count = delta.seconds // 60 // 60 return cls.time_strings['future-hour'] % {'count': count}
def string_for(cls, value): if not isinstance(value, date): # datetime is a subclass of date return value now = datetime.now(utc if is_aware(value) else None) if value < now: delta = now - value if delta.days != 0: return cls.time_strings['past-day'] % { 'delta': defaultfilters.timesince( value, now, time_strings=cls.past_substrings), } elif delta.seconds == 0: return cls.time_strings['now'] elif delta.seconds < 60: return cls.time_strings['past-templates'] % { 'count': delta.seconds } elif delta.seconds // 60 < 60: count = delta.seconds // 60 return cls.time_strings['past-minute'] % {'count': count} else: count = delta.seconds // 60 // 60 return cls.time_strings['past-hour'] % {'count': count} else: delta = value - now if delta.days != 0: return cls.time_strings['future-day'] % { 'delta': defaultfilters.timeuntil( value, now, time_strings=cls.future_substrings), } elif delta.seconds == 0: return cls.time_strings['now'] elif delta.seconds < 60: return cls.time_strings['future-templates'] % { 'count': delta.seconds } elif delta.seconds // 60 < 60: count = delta.seconds // 60 return cls.time_strings['future-minute'] % {'count': count} else: count = delta.seconds // 60 // 60 return cls.time_strings['future-hour'] % {'count': count}
def string_for(cls, value): if not isinstance(value, date): # datetime is a subclass of date return value now = datetime.now(utc if is_aware(value) else None) if value < now: delta = now - value if delta.days != 0: return cls.time_strings["past-day"] % { "delta": defaultfilters.timesince( value, now, time_strings=cls.past_substrings), } elif delta.seconds == 0: return cls.time_strings["now"] elif delta.seconds < 60: return cls.time_strings["past-second"] % { "count": delta.seconds } elif delta.seconds // 60 < 60: count = delta.seconds // 60 return cls.time_strings["past-minute"] % {"count": count} else: count = delta.seconds // 60 // 60 return cls.time_strings["past-hour"] % {"count": count} else: delta = value - now if delta.days != 0: return cls.time_strings["future-day"] % { "delta": defaultfilters.timeuntil( value, now, time_strings=cls.future_substrings), } elif delta.seconds == 0: return cls.time_strings["now"] elif delta.seconds < 60: return cls.time_strings["future-second"] % { "count": delta.seconds } elif delta.seconds // 60 < 60: count = delta.seconds // 60 return cls.time_strings["future-minute"] % {"count": count} else: count = delta.seconds // 60 // 60 return cls.time_strings["future-hour"] % {"count": count}
def to_dict(self): return { "title":self.title, "time_in_min":self.time_in_min, "serving_size":self.serving_size, "description":self.description, "need_by":timeuntil(self.need_by), "kind": self.kind, "slug":self.slug, "course": [str(course) for course in self.course.all()], "cuisine": [str(cuisine) for cuisine in self.cuisine.all()], "restrictions": [str(restrictions) for restrictions in self.restrictions.all()], "primary_ingredients": [str(primary_ingredients) for primary_ingredients in self.primary_ingredients.all()], "chef": self.chef_attatched.to_dict() if self.chef_attatched else None, "chef_profile_url": reverse('miniprofile', args=[self.chef_attatched.id]) if self.chef_attatched else "#", "inpot": int(self.in_pot), "percent": self.percent, "pledges":[pledge.to_dict() for pledge in self.pledges.all()[:10]], }
def timestr(value): # def distance_of_time(delta): # if delta < 1: # return 'less than a minute' # if delta == 1: # return '1 minute' # if delta < 50: # return '%s minutes' % delta # if delta < 90: # return 'about one hour' # if delta < 1080: # return '%s hours' % int(math.ceil(delta / 60)) # if delta < 1440: # return 'one day' # if delta < 2880: # return 'about one day' # days = int(math.ceil(delta / 1440)) # if days < 31: # return '%s days' % days # return 'about %s months' % int(math.ceil(days / 30)) if type(value) == type(""): value = datetime.datetime( *time.strptime(value, "%Y-%m-%d %H:%M:%S")[:6]) try: r = value - datetime.datetime.now() except TypeError: value = datetime.datetime( *time.strptime(value, "%Y-%m-%d %H:%M:%S")[:6]) r = value - datetime.datetime.now() delta = (r.days * 3600 * 24) + r.seconds # distance = distance_of_time(abs(int(delta / 60))) if delta > 0: return "in %s" % timeuntil(value) else: return "%s ago" % timesince(value)
def naturaltime(value): """ For date and time values shows how many seconds, minutes or hours ago compared to current timestamp returns representing string. """ if not isinstance(value, date): # datetime is a subclass of date return value now = datetime.now(utc if is_aware(value) else None) if value < now: delta = now - value if delta.days != 0: return pgettext("naturaltime", "%(delta)s ago") % {"delta": defaultfilters.timesince(value, now)} elif delta.seconds == 0: return _("now") elif delta.seconds < 60: return ungettext("a second ago", "%(count)s seconds ago", delta.seconds) % {"count": delta.seconds} elif delta.seconds // 60 < 60: count = delta.seconds // 60 return ungettext("a minute ago", "%(count)s minutes ago", count) % {"count": count} else: count = delta.seconds // 60 // 60 return ungettext("an hour ago", "%(count)s hours ago", count) % {"count": count} else: delta = value - now if delta.days != 0: return pgettext("naturaltime", "%(delta)s from now") % {"delta": defaultfilters.timeuntil(value, now)} elif delta.seconds == 0: return _("now") elif delta.seconds < 60: return ungettext("a second from now", "%(count)s seconds from now", delta.seconds) % { "count": delta.seconds } elif delta.seconds // 60 < 60: count = delta.seconds // 60 return ungettext("a minute from now", "%(count)s minutes from now", count) % {"count": count} else: count = delta.seconds // 60 // 60 return ungettext("an hour from now", "%(count)s hours from now", count) % {"count": count}
def timestr(value): # def distance_of_time(delta): # if delta < 1: # return 'less than a minute' # if delta == 1: # return '1 minute' # if delta < 50: # return '%s minutes' % delta # if delta < 90: # return 'about one hour' # if delta < 1080: # return '%s hours' % int(math.ceil(delta / 60)) # if delta < 1440: # return 'one day' # if delta < 2880: # return 'about one day' # days = int(math.ceil(delta / 1440)) # if days < 31: # return '%s days' % days # return 'about %s months' % int(math.ceil(days / 30)) if type(value) == type(""): value = datetime.datetime(*time.strptime(value, "%Y-%m-%d %H:%M:%S")[:6]) try: r = value - datetime.datetime.now() except TypeError: value = datetime.datetime(*time.strptime(value, "%Y-%m-%d %H:%M:%S")[:6]) r = value - datetime.datetime.now() delta = (r.days * 3600 * 24) + r.seconds # distance = distance_of_time(abs(int(delta / 60))) if delta > 0: return "in %s" % timeuntil(value) else: return "%s ago" % timesince(value)
def string_for(cls, value): if not isinstance(value, date): # datetime is a subclass of date return value now = datetime.now(utc if is_aware(value) else None) if value < now: delta = now - value if delta.days != 0: return cls.time_strings['past-day'] % { 'delta': defaultfilters.timesince(value, now, time_strings=cls.past_substrings), } elif delta.seconds == 0: return cls.time_strings['now'] elif delta.seconds < 60: return cls.time_strings['past-second'] % {'count': delta.seconds} elif delta.seconds // 60 < 60: count = delta.seconds // 60 return cls.time_strings['past-minute'] % {'count': count} else: count = delta.seconds // 60 // 60 return cls.time_strings['past-hour'] % {'count': count} else: delta = value - now if delta.days != 0: return cls.time_strings['future-day'] % { 'delta': defaultfilters.timeuntil(value, now, time_strings=cls.future_substrings), } elif delta.seconds == 0: return cls.time_strings['now'] elif delta.seconds < 60: return cls.time_strings['future-second'] % {'count': delta.seconds} elif delta.seconds // 60 < 60: count = delta.seconds // 60 return cls.time_strings['future-minute'] % {'count': count} else: count = delta.seconds // 60 // 60 return cls.time_strings['future-hour'] % {'count': count}
def naturaltime(value): """ For date and time values show how many seconds, minutes, or hours ago compared to current timestamp return representing string. """ if not isinstance(value, date): # datetime is a subclass of date return value now = datetime.now(utc if is_aware(value) else None) if value < now: delta = now - value if delta.days != 0: # Translators: delta will contain a string like '2 months' or '1 month, 2 weeks' return _('%(delta)s ago') % { 'delta': defaultfilters.timesince( value, now, time_strings={ # Translators: 'naturaltime-past' strings will be included in # '%(delta)s ago' 'year': npgettext_lazy('naturaltime-past', '%d year', '%d years'), 'month': npgettext_lazy('naturaltime-past', '%d month', '%d months'), 'week': npgettext_lazy('naturaltime-past', '%d week', '%d weeks'), 'day': npgettext_lazy('naturaltime-past', '%d day', '%d days'), 'hour': npgettext_lazy('naturaltime-past', '%d hour', '%d hours'), 'minute': npgettext_lazy('naturaltime-past', '%d minute', '%d minutes') }) } elif delta.seconds == 0: return _('now') elif delta.seconds < 60: return ngettext( # Translators: please keep a non-breaking space (U+00A0) # between count and time unit. 'a second ago', '%(count)s seconds ago', delta.seconds) % { 'count': delta.seconds } elif delta.seconds // 60 < 60: count = delta.seconds // 60 return ngettext( # Translators: please keep a non-breaking space (U+00A0) # between count and time unit. 'a minute ago', '%(count)s minutes ago', count) % { 'count': count } else: count = delta.seconds // 60 // 60 return ngettext( # Translators: please keep a non-breaking space (U+00A0) # between count and time unit. 'an hour ago', '%(count)s hours ago', count) % { 'count': count } else: delta = value - now if delta.days != 0: # Translators: delta will contain a string like '2 months' or '1 month, 2 weeks' return _('%(delta)s from now') % { 'delta': defaultfilters.timeuntil( value, now, time_strings={ # Translators: 'naturaltime-future' strings will be included in # '%(delta)s from now' 'year': npgettext_lazy('naturaltime-future', '%d year', '%d years'), 'month': npgettext_lazy('naturaltime-future', '%d month', '%d months'), 'week': npgettext_lazy('naturaltime-future', '%d week', '%d weeks'), 'day': npgettext_lazy('naturaltime-future', '%d day', '%d days'), 'hour': npgettext_lazy('naturaltime-future', '%d hour', '%d hours'), 'minute': npgettext_lazy('naturaltime-future', '%d minute', '%d minutes') }) } elif delta.seconds == 0: return _('now') elif delta.seconds < 60: return ngettext( # Translators: please keep a non-breaking space (U+00A0) # between count and time unit. 'a second from now', '%(count)s seconds from now', delta.seconds) % { 'count': delta.seconds } elif delta.seconds // 60 < 60: count = delta.seconds // 60 return ngettext( # Translators: please keep a non-breaking space (U+00A0) # between count and time unit. 'a minute from now', '%(count)s minutes from now', count) % { 'count': count } else: count = delta.seconds // 60 // 60 return ngettext( # Translators: please keep a non-breaking space (U+00A0) # between count and time unit. 'an hour from now', '%(count)s hours from now', count) % { 'count': count }
def remaining(self, obj): remaining = int(obj.expire - time.time()) if remaining > 0: return timeuntil(datetime.datetime.fromtimestamp(obj.expire)) else: return "expired {0} ago".format(timesince(datetime.datetime.fromtimestamp(obj.expire)))
def naturaltime(value): """ For date and time values show how many seconds, minutes, or hours ago compared to current timestamp return representing string. """ if not isinstance(value, date): # datetime is a subclass of date return value now = datetime.now(utc if is_aware(value) else None) if value < now: delta = now - value if delta.days != 0: # Translators: delta will contain a string like '2 months' or '1 month, 2 weeks' return _('%(delta)s ago') % {'delta': defaultfilters.timesince(value, now, time_strings={ # Translators: 'naturaltime-past' strings will be included in # '%(delta)s ago' 'year': npgettext_lazy('naturaltime-past', '%d year', '%d years'), 'month': npgettext_lazy('naturaltime-past', '%d month', '%d months'), 'week': npgettext_lazy('naturaltime-past', '%d week', '%d weeks'), 'day': npgettext_lazy('naturaltime-past', '%d day', '%d days'), 'hour': npgettext_lazy('naturaltime-past', '%d hour', '%d hours'), 'minute': npgettext_lazy('naturaltime-past', '%d minute', '%d minutes') })} elif delta.seconds == 0: return _('now') elif delta.seconds < 60: return ngettext( # Translators: please keep a non-breaking space (U+00A0) # between count and time unit. 'a second ago', '%(count)s seconds ago', delta.seconds ) % {'count': delta.seconds} elif delta.seconds // 60 < 60: count = delta.seconds // 60 return ngettext( # Translators: please keep a non-breaking space (U+00A0) # between count and time unit. 'a minute ago', '%(count)s minutes ago', count ) % {'count': count} else: count = delta.seconds // 60 // 60 return ngettext( # Translators: please keep a non-breaking space (U+00A0) # between count and time unit. 'an hour ago', '%(count)s hours ago', count ) % {'count': count} else: delta = value - now if delta.days != 0: # Translators: delta will contain a string like '2 months' or '1 month, 2 weeks' return _('%(delta)s from now') % {'delta': defaultfilters.timeuntil(value, now, time_strings={ # Translators: 'naturaltime-future' strings will be included in # '%(delta)s from now' 'year': npgettext_lazy('naturaltime-future', '%d year', '%d years'), 'month': npgettext_lazy('naturaltime-future', '%d month', '%d months'), 'week': npgettext_lazy('naturaltime-future', '%d week', '%d weeks'), 'day': npgettext_lazy('naturaltime-future', '%d day', '%d days'), 'hour': npgettext_lazy('naturaltime-future', '%d hour', '%d hours'), 'minute': npgettext_lazy('naturaltime-future', '%d minute', '%d minutes') })} elif delta.seconds == 0: return _('now') elif delta.seconds < 60: return ngettext( # Translators: please keep a non-breaking space (U+00A0) # between count and time unit. 'a second from now', '%(count)s seconds from now', delta.seconds ) % {'count': delta.seconds} elif delta.seconds // 60 < 60: count = delta.seconds // 60 return ngettext( # Translators: please keep a non-breaking space (U+00A0) # between count and time unit. 'a minute from now', '%(count)s minutes from now', count ) % {'count': count} else: count = delta.seconds // 60 // 60 return ngettext( # Translators: please keep a non-breaking space (U+00A0) # between count and time unit. 'an hour from now', '%(count)s hours from now', count ) % {'count': count}
def timeuntil(time): return defaultfilters.timeuntil(time)
def naturaltime(value): """ For date and time values shows how many seconds, minutes or hours ago compared to current timestamp returns representing string. """ try: value = datetime.datetime(value.year, value.month, value.day, value.hour, value.minute, value.second) except AttributeError: return value except ValueError: return value if getattr(value, 'tzinfo', None): now = datetime.datetime.now(LocalTimezone(value)) else: now = datetime.datetime.now() now = now - timedelta(0, 0, now.microsecond) if value < now: delta = now - value if delta.days != 0: return pgettext( 'naturaltime', '%(delta)s ago' ) % {'delta': defaultfilters.timesince(value)} elif delta.seconds == 0: return _(u'now') elif delta.seconds < 60: return ungettext( u'a second ago', u'%(count)s seconds ago', delta.seconds ) % {'count': delta.seconds} elif delta.seconds // 60 < 60: count = delta.seconds // 60 return ungettext( u'a minute ago', u'%(count)s minutes ago', count ) % {'count': count} else: count = delta.seconds // 60 // 60 return ungettext( u'an hour ago', u'%(count)s hours ago', count ) % {'count': count} else: delta = value - now if delta.days != 0: return pgettext( 'naturaltime', '%(delta)s from now' ) % {'delta': defaultfilters.timeuntil(value)} elif delta.seconds == 0: return _(u'now') elif delta.seconds < 60: return ungettext( u'a second from now', u'%(count)s seconds from now', delta.seconds ) % {'count': delta.seconds} elif delta.seconds // 60 < 60: count = delta.seconds // 60 return ungettext( u'a minute from now', u'%(count)s minutes from now', count ) % {'count': count} else: count = delta.seconds // 60 // 60 return ungettext( u'an hour from now', u'%(count)s hours from now', count ) % {'count': count}
def zeropush_new_job(job): # Get sellers matching seller title sellers = Seller.objects.filter(titles=job.title).exclude(user=job.buyer).all() # Get sellers within distance suitable_sellers = [] for seller in sellers: kwargs = { 'lat1': float(job.geoposition.latitude), 'long1': float(job.geoposition.longitude), 'lat2': float(seller.geoposition.latitude), 'long2': float(seller.geoposition.longitude), } if distance_in_kilometers(**kwargs) <= seller.distance: suitable_sellers.append(seller) # Get suitable user devices if len(suitable_sellers) > 0: devices = [] for seller in suitable_sellers: devices += seller.user.pushdevice_set.all() # Notify devices if len(devices) > 0: if job.start_asap: alert = _(u"{title} ASAP").format(title=job.title.title) else: alert = _(u"{title} in {start_on}").format(title=job.title.title, start_on=timeuntil(job.start_on)) print alert zeropush.notify_devices(devices, alert=alert)
def simpletime(value): if not isinstance(value, date): # datetime is a subclass of date return value now = datetime.now(utc if is_aware(value) else None) if value < now: delta = now - value if delta.days != 0: if delta.days < 31: return ungettext( # Translators: \\u00a0 is non-breaking space 'yesterday', '%(count)s days ago', delta.days ) % {'count': delta.days} else: if value.year == now.year: return value.strftime("%d %B"); else: return value.strftime("%B %Y"); elif delta.seconds == 0: return _('now') elif delta.seconds < 60: return ungettext( # Translators: \\u00a0 is non-breaking space 'a second ago', '%(count)s seconds ago', delta.seconds ) % {'count': delta.seconds} elif delta.seconds // 60 < 60: count = delta.seconds // 60 return ungettext( # Translators: \\u00a0 is non-breaking space 'a minute ago', '%(count)s minutes ago', count ) % {'count': count} else: count = delta.seconds // 60 // 60 return ungettext( # Translators: \\u00a0 is non-breaking space 'an hour ago', '%(count)s hours ago', count ) % {'count': count} else: delta = value - now if delta.days != 0: return pgettext( 'naturaltime', '%(delta)s from now' ) % {'delta': defaultfilters.timeuntil(value, now)} elif delta.seconds == 0: return _('now') elif delta.seconds < 60: return ungettext( # Translators: \\u00a0 is non-breaking space 'a second from now', '%(count)s seconds from now', delta.seconds ) % {'count': delta.seconds} elif delta.seconds // 60 < 60: count = delta.seconds // 60 return ungettext( # Translators: \\u00a0 is non-breaking space 'a minute from now', '%(count)s minutes from now', count ) % {'count': count} else: count = delta.seconds // 60 // 60 return ungettext( # Translators: \\u00a0 is non-breaking space 'an hour from now', '%(count)s hours from now', count ) % {'count': count}
def naturaltime(value): """ For date and time values shows how many seconds, minutes or hours ago compared to current timestamp returns representing string. """ try: value = datetime(value.year, value.month, value.day, value.hour, value.minute, value.second) except AttributeError: return value except ValueError: return value if getattr(value, 'tzinfo', None): now = datetime.now(LocalTimezone(value)) else: now = datetime.now() now = now - timedelta(0, 0, now.microsecond) if value < now: delta = now - value if delta.days != 0: return pgettext('naturaltime', '%(delta)s ago') % { 'delta': defaultfilters.timesince(value) } elif delta.seconds == 0: return _(u'now') elif delta.seconds < 60: return ungettext(u'a second ago', u'%(count)s seconds ago', delta.seconds) % { 'count': delta.seconds } elif delta.seconds / 60 < 60: count = delta.seconds / 60 return ungettext(u'a minute ago', u'%(count)s minutes ago', count) % { 'count': count } else: count = delta.seconds / 60 / 60 return ungettext(u'an hour ago', u'%(count)s hours ago', count) % { 'count': count } else: delta = value - now if delta.days != 0: return pgettext('naturaltime', '%(delta)s from now') % { 'delta': defaultfilters.timeuntil(value) } elif delta.seconds == 0: return _(u'now') elif delta.seconds < 60: return ungettext(u'a second from now', u'%(count)s seconds from now', delta.seconds) % { 'count': delta.seconds } elif delta.seconds / 60 < 60: count = delta.seconds / 60 return ungettext(u'a minute from now', u'%(count)s minutes from now', count) % { 'count': count } else: count = delta.seconds / 60 / 60 return ungettext(u'an hour from now', u'%(count)s hours from now', count) % { 'count': count }
def getTimeuntilEnd(self): return timeuntil(datetime.combine(date.today(), self.end_time))
def get_publication_date(self, obj): if obj.publication_date and obj.publication_date <= datetime.now(): return mark_safe('<span style="color:green">publicado hace %s</span>' % timesince(obj.publication_date)) return mark_safe('<span style="color:blue">se publica en %s</span>' % timeuntil(obj.publication_date))
def timeuntil(self): return timeuntil(datetime.now() - self.age)
def naturaltime(value): """ For date and time values show how many seconds, minutes, or hours ago compared to current timestamp return representing string. """ if not isinstance(value, date): # datetime is a subclass of date return value now = datetime.now(utc if is_aware(value) else None) if value < now: delta = now - value if delta.days != 0: return pgettext('naturaltime', '%(delta)s ago') % { 'delta': defaultfilters.timesince( value, now, context='naturaltime-past') } elif delta.seconds == 0: return _('now') elif delta.seconds < 60: return ngettext( # Translators: please keep a non-breaking space (U+00A0) # between count and time unit. 'a second ago', '%(count)s seconds ago', delta.seconds) % { 'count': delta.seconds } elif delta.seconds // 60 < 60: count = delta.seconds // 60 return ngettext( # Translators: please keep a non-breaking space (U+00A0) # between count and time unit. 'a minute ago', '%(count)s minutes ago', count) % { 'count': count } else: count = delta.seconds // 60 // 60 return ngettext( # Translators: please keep a non-breaking space (U+00A0) # between count and time unit. 'an hour ago', '%(count)s hours ago', count) % { 'count': count } else: delta = value - now if delta.days != 0: return pgettext('naturaltime', '%(delta)s from now') % { 'delta': defaultfilters.timeuntil( value, now, context='naturaltime-future') } elif delta.seconds == 0: return _('now') elif delta.seconds < 60: return ngettext( # Translators: please keep a non-breaking space (U+00A0) # between count and time unit. 'a second from now', '%(count)s seconds from now', delta.seconds) % { 'count': delta.seconds } elif delta.seconds // 60 < 60: count = delta.seconds // 60 return ngettext( # Translators: please keep a non-breaking space (U+00A0) # between count and time unit. 'a minute from now', '%(count)s minutes from now', count) % { 'count': count } else: count = delta.seconds // 60 // 60 return ngettext( # Translators: please keep a non-breaking space (U+00A0) # between count and time unit. 'an hour from now', '%(count)s hours from now', count) % { 'count': count }
def naturaltime(value): """ For date and time values shows how many seconds, minutes or hours ago compared to current timestamp returns representing string. """ if not isinstance(value, date): # datetime is a subclass of date return value # These two lines below are only available in django 1.4 # from django.utils.timezone import is_aware, utc # now = datetime.now(utc if is_aware(value) else None) now = datetime.now() if value < now: delta = now - value if delta.days > 30: return pgettext( 'more than 1 month ago', 'on %(date)s at %(time)s' ) % { 'date': value.strftime("%d/%m/%Y"), 'time': value.strftime("%H:%M") } elif delta.days != 0: return pgettext( 'naturaltime', '%(delta)s ago' ) % {'delta': defaultfilters.timesince(value)} elif delta.seconds == 0: return _('now') elif delta.seconds < 60: return ungettext( 'a second ago', '%(count)s seconds ago', delta.seconds ) % {'count': delta.seconds} elif delta.seconds // 60 < 60: count = delta.seconds // 60 return ungettext( 'a minute ago', '%(count)s minutes ago', count ) % {'count': count} else: count = delta.seconds // 60 // 60 return ungettext( 'an hour ago', '%(count)s hours ago', count ) % {'count': count} else: delta = value - now if delta.days != 0: return pgettext( 'naturaltime', '%(delta)s from now' ) % {'delta': defaultfilters.timeuntil(value)} elif delta.seconds == 0: return _('now') elif delta.seconds < 60: return ungettext( 'a second from now', '%(count)s seconds from now', delta.seconds ) % {'count': delta.seconds} elif delta.seconds // 60 < 60: count = delta.seconds // 60 return ungettext( 'a minute from now', '%(count)s minutes from now', count ) % {'count': count} else: count = delta.seconds // 60 // 60 return ungettext( 'an hour from now', '%(count)s hours from now', count ) % {'count': count}
def timeuntil(context, value, arg=None): return filters.timeuntil(value, arg)
# -*- encoding: utf-8 -*-