Esempio n. 1
0
    def render_distance_of_time_in_words(dt_from, dt_to=None):
        if not dt_from:
            return

        if dt_from.tzinfo is None:
            dt_from = convert_timezone(dt_from)

        if dt_to is None:
            return {'text': distance_of_time_in_words(dt_from, convert_timezone(datetime.now())) + ' ago', 'data_numeric': dt_from.strftime('%s')}
        else:
            if dt_to.tzinfo is None:
                dt_to = convert_timezone(dt_to)
            return {'text': distance_of_time_in_words(dt_from, dt_to),
                    'data_numeric': -timedelta_to_seconds(dt_to - dt_from)}
Esempio n. 2
0
def ago(start_time, show_date_after=7):
    """
    Return time since starting time as a rounded, human readable string.
    E.g., "3 hours ago"
    """

    if start_time is None:
        return 'unknown'
    granularities = [
        'century', 'decade', 'year', 'month', 'day', 'hour', 'minute'
    ]
    end_time = datetime.utcnow()
    if show_date_after is not None and end_time - start_time > timedelta(
            days=show_date_after):
        return start_time.strftime('%Y-%m-%d')

    while True:
        granularity = granularities.pop()
        ago = date.distance_of_time_in_words(start_time,
                                             end_time,
                                             granularity,
                                             round=True)
        rounded_to_one_granularity = 'and' not in ago
        if rounded_to_one_granularity:
            break
    return ago + ' ago'
Esempio n. 3
0
    def age(self, end=None, tz=None, granularity='hour', general=False):
        """
        Return the distance of time in words from `self.datetime` to `end`.

            >>> start = datetime(1984, 11, 02)
            >>> now = datetime(2009, 5, 22, 12, 11, 10)
            >>> DateTimeDisplay(start).age(now)
            '2 decades, 4 years, 6 months, 20 days and 12 hours'
            >>> DateTimeDisplay(start).age(now, general=True)
            '2 decades'

        """
        start = self.datetime
        if not end:
            end = datetime.utcnow()
        else:
            if isinstance(end, DateTimeDisplay):
                end = end.datetime
        if tz:
            zone = timezone(tz)
            end = end.replace(tzinfo=utc)
            end = zone.normalize(end.astimezone(zone))
            start = self.astimezone(tz)

        age = distance_of_time_in_words(start, end, granularity=granularity)

        if general:
            if not age.startswith('less than'):
                age = age.split('and')[0].split(',')[0]

        return age
Esempio n. 4
0
def time_ago( x ):
    """
    Convert a datetime to a string.
    """
    delta = timedelta(weeks=1)

    # If the date is more than one week ago, then display the actual date instead of in words
    if (datetime.utcnow() - x) > delta:  # Greater than a week difference
        return x.strftime("%b %d, %Y")
    else:
        date_array = date.distance_of_time_in_words( x, datetime.utcnow() ).replace(",", "").split(" ")
        return "~%s %s ago" % (date_array[0], date_array[1])
Esempio n. 5
0
def time_ago(x):
    """
    Convert a datetime to a string.
    """
    delta = timedelta(weeks=1)

    # If the date is more than one week ago, then display the actual date instead of in words
    if (datetime.utcnow() - x) > delta:  # Greater than a week difference
        return x.strftime("%b %d, %Y")
    else:
        return date.distance_of_time_in_words(x, datetime.utcnow()).replace(
            "about", "~") + " ago"
Esempio n. 6
0
    now = datetime.now()
    return now.strftime('%Y')


def strftime(x, human=False):
    if x:
        if human:
            return x.strftime('%c').decode('utf-8')
        else:
            return x.strftime(config.D_T_FMT)
    else:
        return u''


strftimedelta = (lambda delta, granularity='minute': distance_of_time_in_words(
    datetime.now(),
    datetime.now() + delta, granularity))

#----------------------------------------------------------------------


class MyHtmlFormatter(HtmlFormatter):
    '''Create lines that have unique name tags to allow highlighting

    Each line has an anchor named <lineanchors>-<linenumber>
    '''
    def _wrap_lineanchors(self, inner):
        s = self.lineanchors
        i = 0
        for t, line in inner:
            if t:
Esempio n. 7
0
 def render_volretention(self, request):
     # volretention is integer of seconds
     return {'text': distance_of_time_in_words(self.volretention)}