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')
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')
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')
def gettext_unicode(*args, **kwargs): from fossir.util.string import inject_unicode_debug func_name = kwargs.pop('func_name', 'ugettext') plugin_name = kwargs.pop('plugin_name', None) force_unicode = kwargs.pop('force_unicode', False) if not isinstance(args[0], unicode): args = [(text.decode('utf-8') if isinstance(text, str) else text) for text in args] using_unicode = force_unicode else: using_unicode = True translations = get_translation_domain(plugin_name).get_translations() res = getattr(translations, func_name)(*args, **kwargs) res = inject_unicode_debug(res) if not using_unicode: res = res.encode('utf-8') return res
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')
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')