コード例 #1
0
def format_datetime(dt,
                    format='medium',
                    locale=None,
                    timezone=None,
                    server_tz=False,
                    keep_tz=False):
    """
    Basically a wrapper around Babel's own format_datetime
    """
    inject_unicode = True
    if format == 'code':
        format = 'dd/MM/yyyy HH:mm'
        inject_unicode = False
    if not locale:
        locale = get_current_locale()
    if keep_tz:
        assert timezone is None
        timezone = dt.tzinfo
    elif not timezone and dt.tzinfo:
        timezone = session.tzinfo
    elif server_tz:
        timezone = config.DEFAULT_TIMEZONE

    rv = _format_datetime(dt, format=format, locale=locale, tzinfo=timezone)
    return inject_unicode_debug(
        rv, 2).encode('utf-8') if inject_unicode else rv.encode('utf-8')
コード例 #2
0
 def __init__(self, *args, **kwargs):
     locale = get_current_locale()
     self.day_number_options = self.WEEK_DAY_NUMBER_CHOICES
     self.week_day_options = [(n, locale.weekday(n, short=False))
                              for n in xrange(7)]
     self.week_day_options.append((-1, _('Any day')))
     self.day_number_missing = False
     self.week_day_missing = False
     super(fossirWeekDayRepetitionField, self).__init__(*args, **kwargs)
コード例 #3
0
def format_timedelta(td, format='short', threshold=0.85, locale=None):
    """
    Basically a wrapper around Babel's own format_timedelta
    """
    if not locale:
        locale = get_current_locale()

    rv = _format_timedelta(td,
                           format=format,
                           locale=locale,
                           threshold=threshold)
    return inject_unicode_debug(rv, 2).encode('utf-8')
コード例 #4
0
def format_date(d, format='medium', locale=None, timezone=None):
    """
    Basically a wrapper around Babel's own format_date
    """
    inject_unicode = True
    if format == 'code':
        format = 'dd/MM/yyyy'
        inject_unicode = False
    if not locale:
        locale = get_current_locale()
    if timezone and isinstance(d, datetime) and d.tzinfo:
        d = d.astimezone(
            pytz.timezone(timezone) if isinstance(timezone, basestring
                                                  ) else timezone)

    rv = _format_date(d, format=format, locale=locale)
    return inject_unicode_debug(
        rv, 2).encode('utf-8') if inject_unicode else rv.encode('utf-8')
コード例 #5
0
def format_human_date(dt, format='medium', locale=None):
    """
    Return the date in a human-like format for yesterday, today and tomorrow.
    Format the date otherwise.
    """
    today = now_utc().date()
    oneday = timedelta(days=1)

    if not locale:
        locale = get_current_locale()

    if dt == today - oneday:
        return _("yesterday")
    elif dt == today:
        return _("today")
    elif dt == today + oneday:
        return _("tomorrow")
    else:
        return format_date(dt, format, locale=locale)
コード例 #6
0
def _format_pretty_datetime(dt, locale, tzinfo, formats):
    locale = get_current_locale() if not locale else parse_locale(locale)

    if tzinfo:
        if dt.tzinfo:
            dt = dt.astimezone(tzinfo)
        else:
            dt = tzinfo.localize(dt).astimezone(tzinfo)

    today = (now_utc(False).astimezone(tzinfo)
             if tzinfo else now_utc(False)).replace(hour=0, minute=0)
    diff = (dt - today).total_seconds() / 86400.0
    mapping = [(-6, 'other'), (-1, 'last_week'), (0, 'last_day'),
               (1, 'same_day'), (2, 'next_day'), (7, 'next_week'),
               (None, 'other')]

    fmt = next(formats[key] for delta, key in mapping
               if delta is None or diff < delta)
    fmt = fmt.format(date_fmt=locale.date_formats['medium'],
                     time_fmt=locale.time_formats['short'])
    return _format_datetime(dt, fmt, tzinfo, locale)
コード例 #7
0
def format_time(t,
                format='short',
                locale=None,
                timezone=None,
                server_tz=False):
    """
    Basically a wrapper around Babel's own format_time
    """
    inject_unicode = True
    if format == 'code':
        format = 'HH:mm'
        inject_unicode = False
    if not locale:
        locale = get_current_locale()
    if not timezone and t.tzinfo:
        timezone = session.tzinfo
    elif server_tz:
        timezone = config.DEFAULT_TIMEZONE
    if isinstance(timezone, basestring):
        timezone = get_timezone(timezone)
    rv = _format_time(t, format=format, locale=locale, tzinfo=timezone)
    return inject_unicode_debug(
        rv, 2).encode('utf-8') if inject_unicode else rv.encode('utf-8')
コード例 #8
0
def format_number(number, locale=None):
    if not locale:
        locale = get_current_locale()
    rv = _format_number(number, locale=locale)
    return inject_unicode_debug(rv, 2).encode('utf-8')