示例#1
0
def _format_html_time_tag(datetime, what_to_display):
    now = time_utcnow()

    date = None
    if datetime.date() == now.date():
        date = "Today"
    elif datetime.date() == (now - timedelta(days=1)).date():
        date = "Yesterday"

    if what_to_display == "date-only":
        exact = babel_format_date(datetime, format="dd MMM yyyy", locale=_get_user_locale())
        content = date if date else exact
    elif what_to_display == "date-and-time":
        exact = babel_format_datetime(
                    datetime, format="dd.MM.yyyy HH:mm:ss", tzinfo=get_timezone(), locale=_get_user_locale()
                )

        if date:
            content = date + ", " + \
                babel_format_datetime(
                    datetime, format="HH:mm:ss", tzinfo=get_timezone(), locale=_get_user_locale()
                )
        else:
            content = exact
    else:
        raise ValueError("what_to_display argument invalid")

    isoformat = datetime.isoformat()

    return Markup('<time datetime="{isoformat}" data-toggle="tooltip" data-placement="top" title="{exact}">{content}</time>'.format(
            isoformat=isoformat, exact=exact, content=content
        ))
示例#2
0
def format_datetime(t=None, format='%x %X', tzinfo=None, locale=None):
    """Format the `datetime` object `t` into an `unicode` string

    If `t` is None, the current time will be used.
    
    The formatting will be done using the given `format`, which consist
    of conventional `strftime` keys. In addition the format can be 'iso8601'
    to specify the international date format (compliant with RFC 3339).

    `tzinfo` will default to the local timezone if left to `None`.
    """
    if locale == 'iso8601':
        format = _ISO8601_FORMATS['datetime'].get(format, format)
        return _format_datetime_without_babel(t, format, tzinfo)
    if babel and locale:
        if format == '%x':
            return babel_format_date(t, 'medium', locale)
        if format == '%X':
            t = to_datetime(t, tzinfo)
            return babel_format_time(t, 'medium', None, locale)
        if format in ('%x %X', None):
            format = 'medium'
        if format in _BABEL_FORMATS['datetime']:
            t = to_datetime(t, tzinfo)
            return babel_format_datetime(t, format, None, locale)
    format = _BABEL_FORMATS['datetime'].get(format, format)
    return _format_datetime_without_babel(t, format, tzinfo)
示例#3
0
def format_datetime(t=None, format='%x %X', tzinfo=None, locale=None):
    """Format the `datetime` object `t` into an `unicode` string

    If `t` is None, the current time will be used.
    
    The formatting will be done using the given `format`, which consist
    of conventional `strftime` keys. In addition the format can be 'iso8601'
    to specify the international date format (compliant with RFC 3339).

    `tzinfo` will default to the local timezone if left to `None`.
    """
    if locale == 'iso8601':
        format = _ISO8601_FORMATS['datetime'].get(format, format)
        return _format_datetime_without_babel(t, format, tzinfo)
    if babel and locale:
        if format == '%x':
            return babel_format_date(t, 'medium', locale)
        if format == '%X':
            t = to_datetime(t, tzinfo)
            return babel_format_time(t, 'medium', None, locale)
        if format in ('%x %X', None):
            format = 'medium'
        if format in _BABEL_FORMATS['datetime']:
            t = to_datetime(t, tzinfo)
            return babel_format_datetime(t, format, None, locale)
    format = _BABEL_FORMATS['datetime'].get(format, format)
    return _format_datetime_without_babel(t, format, tzinfo)
示例#4
0
def get_time_format_jquery_ui(locale):
    """Get the time format for the jQuery UI timepicker addon."""
    if locale == 'iso8601':
        return 'HH:mm:ssZ'
    if babel and locale:
        values = {
            'h': 'h',
            'hh': 'hh',
            'H': 'H',
            'HH': 'HH',
            'm': 'm',
            'mm': 'mm',
            's': 's',
            'ss': 'ss'
        }
        f = get_time_format('medium', locale=locale).format
        if '%(a)s' in f:
            t = datetime(1999, 10, 29, 23, 59, 58, tzinfo=utc)
            ampm = babel_format_datetime(t, 'a', None, locale)
            values['a'] = 'TT' if ampm[0].isupper() else 'tt'
        return f % values

    t = datetime(1999, 10, 29, 23, 59, 58, tzinfo=utc)
    tmpl = format_time(t, tzinfo=utc)
    ampm = format_time(t, '%p', tzinfo=utc)
    if ampm:
        tmpl = tmpl.replace(ampm, 'TT' if ampm[0].isupper() else 'tt', 1)
    return tmpl.replace('23', 'HH', 1).replace('11', 'hh', 1) \
               .replace('59', 'mm', 1).replace('58', 'ss', 1)
示例#5
0
def _format_datetime(t, format, tzinfo, locale, hint):
    t = to_datetime(t, tzinfo or localtz)

    if format == 'iso8601':
        return _format_datetime_iso8601(t, 'long', hint)
    if format in ('iso8601date', 'iso8601time'):
        return _format_datetime_iso8601(t, 'long', format[7:])
    if locale == 'iso8601':
        if format is None:
            format = 'long'
        elif format in _STRFTIME_HINTS:
            hint = _STRFTIME_HINTS[format]
            format = 'long'
        if format in ('short', 'medium', 'long', 'full'):
            return _format_datetime_iso8601(t, format, hint)
        return _format_datetime_without_babel(t, format)

    if babel and locale:
        if format is None:
            format = 'medium'
        elif format in _STRFTIME_HINTS:
            hint = _STRFTIME_HINTS[format]
            format = 'medium'
        if format in ('short', 'medium', 'long', 'full'):
            if hint == 'datetime':
                return babel_format_datetime(t, format, None, locale)
            if hint == 'date':
                return babel_format_date(t, format, locale)
            if hint == 'time':
                return babel_format_time(t, format, None, locale)

    format = _BABEL_FORMATS[hint].get(format, format)
    return _format_datetime_without_babel(t, format)
示例#6
0
文件: datefmt.py 项目: pkdevbox/trac
def _format_datetime(t, format, tzinfo, locale, hint):
    t = to_datetime(t, tzinfo or localtz)

    if format == 'iso8601':
        return _format_datetime_iso8601(t, 'long', hint)
    if format in ('iso8601date', 'iso8601time'):
        return _format_datetime_iso8601(t, 'long', format[7:])
    if locale == 'iso8601':
        if format is None:
            format = 'long'
        elif format in _STRFTIME_HINTS:
            hint = _STRFTIME_HINTS[format]
            format = 'long'
        if format in ('short', 'medium', 'long', 'full'):
            return _format_datetime_iso8601(t, format, hint)
        return _format_datetime_without_babel(t, format)

    if babel and locale:
        if format is None:
            format = 'medium'
        elif format in _STRFTIME_HINTS:
            hint = _STRFTIME_HINTS[format]
            format = 'medium'
        if format in ('short', 'medium', 'long', 'full'):
            if hint == 'datetime':
                return babel_format_datetime(t, format, None, locale)
            if hint == 'date':
                return babel_format_date(t, format, locale)
            if hint == 'time':
                return babel_format_time(t, format, None, locale)

    format = _BABEL_FORMATS[hint].get(format, format)
    return _format_datetime_without_babel(t, format)
示例#7
0
文件: app.py 项目: ibuttimer/fyyur
def format_datetime(date_value, date_format='medium'):
    date = dateutil.parser.parse(date_value)
    if date_format == 'full':
        date_format = "EEEE MMMM, d, y 'at' h:mma"
    elif date_format == 'medium':
        date_format = "EE MM, dd, y h:mma"
    return babel_format_datetime(date, format=date_format, locale=LOCALE)
示例#8
0
def format_datetime(dt, locale_name=None, format="medium"):
    """Returns a prettyfied version of a datetime. If a locale_name is
    provided the datetime will be localized. Without a locale the
    datetime will be formatted into the form YYYY-MM-DD hh:ss"""
    if locale_name:
        locale = Locale(locale_name)
        return babel_format_datetime(dt, locale=locale, format=format)
    return dt.strftime("%Y-%m-%d %H:%M")
示例#9
0
def format_datetime(dt, locale_name=None, format="medium"):
    """Returns a prettyfied version of a datetime. If a locale_name is
    provided the datetime will be localized. Without a locale the
    datetime will be formatted into the form YYYY-MM-DD hh:ss"""
    if locale_name:
        locale = Locale(locale_name)
        return babel_format_datetime(dt, locale=locale, format=format)
    return dt.strftime("%Y-%m-%d %H:%M")
示例#10
0
def format_datetime(value, format='medium'):
    if format == 'full':
        format = "EEEE, d. MMMM y 'at' HH:mm"
    elif format == 'medium':
        format = "EE dd.MM.y HH:mm"
    elif format == 'pretty':
        now = datetime.now()
        delta = now - value
        return pretty.date(now - delta)
    return babel_format_datetime(value, format)
def local_to_utc_datetime(dt):
    """Converts a naive local datetime object to a naive UTC datetime object."""

    from datetime import datetime
    import pytz
    from babel.dates import format_datetime as babel_format_datetime
    from flask.ext.babel import get_timezone as flask_babel_get_timezone

    local = flask_babel_get_timezone()

    return datetime.strptime(babel_format_datetime(local.localize(dt), 'YYYY-MM-dd HH:mm:ss', tzinfo=pytz.UTC), '%Y-%m-%d %H:%M:%S')
示例#12
0
 def format_datetime(self, datetime_obj, date_only, format,
                     localized) -> str:
     if date_only:
         return babel_format_date(datetime_obj,
                                  format=format,
                                  locale=self.locale)
     else:
         if localized:
             tzinfo = get_localzone()
         else:
             tzinfo = None
         return babel_format_datetime(datetime_obj,
                                      format=format,
                                      tzinfo=tzinfo,
                                      locale=self.locale)
示例#13
0
def _format_html_time_tag(datetime, what_to_display):
    if what_to_display == "date-only":
        content = babel_format_date(datetime,
                                    format="dd MMM yyyy",
                                    locale=_get_user_locale())
    elif what_to_display == "date-and-time":
        content = babel_format_datetime(datetime,
                                        format="dd.MM.yyyy HH:mm:ss",
                                        tzinfo=get_timezone("Europe/Moscow"),
                                        locale=_get_user_locale())
    else:
        raise ValueError("what_to_display argument invalid")

    isoformat = datetime.isoformat()

    return Markup('<time datetime="{}">{}</time>'.format(isoformat, content))
示例#14
0
def _format_html_time_tag(datetime, what_to_display):
    if what_to_display == "date-only":
        content = babel_format_date(datetime, locale=_get_user_locale())
    elif what_to_display == "date-and-time":
        content = babel_format_datetime(datetime,
                                        tzinfo=UTC,
                                        locale=_get_user_locale())
        # While this could be done with a format string, that would
        # hinder internationalization and honestly why bother.
        content += " UTC"
    else:
        raise ValueError("what_to_display argument invalid")

    isoformat = datetime.isoformat()

    return Markup(
        '<time datetime="{}" data-what_to_display="{}">{}</time>'.format(
            isoformat, what_to_display, content))
示例#15
0
文件: helpers.py 项目: eirnym/flaskbb
def _format_html_time_tag(datetime, what_to_display):
    if what_to_display == "date-only":
        content = babel_format_date(datetime, locale=_get_user_locale())
    elif what_to_display == "date-and-time":
        content = babel_format_datetime(
            datetime, tzinfo=UTC, locale=_get_user_locale()
        )
        # While this could be done with a format string, that would
        # hinder internationalization and honestly why bother.
        content += " UTC"
    else:
        raise ValueError("what_to_display argument invalid")

    isoformat = datetime.isoformat()

    return Markup(
        '<time datetime="{}" data-what_to_display="{}">{}</time>'.format(
            isoformat, what_to_display, content
        )
    )
示例#16
0
文件: datefmt.py 项目: pkdevbox/trac
def get_time_format_jquery_ui(locale):
    """Get the time format for the jQuery UI timepicker addon."""
    if locale == 'iso8601':
        return 'HH:mm:ssz'  # XXX timepicker doesn't support 'ISO_8601'
    if babel and locale:
        values = {'h': 'h', 'hh': 'hh', 'H': 'H', 'HH': 'HH',
                  'm': 'm', 'mm': 'mm', 's': 's', 'ss': 'ss'}
        f = get_time_format('medium', locale=locale).format
        if '%(a)s' in f:
            t = datetime(1999, 10, 29, 23, 59, 58, tzinfo=utc)
            ampm = babel_format_datetime(t, 'a', None, locale)
            values['a'] = 'TT' if ampm[0].isupper() else 'tt'
        return f % values

    t = datetime(1999, 10, 29, 23, 59, 58, tzinfo=utc)
    tmpl = format_time(t, tzinfo=utc)
    ampm = format_time(t, '%p', tzinfo=utc)
    if ampm:
        tmpl = tmpl.replace(ampm, 'TT' if ampm[0].isupper() else 'tt', 1)
    return tmpl.replace('23', 'HH', 1).replace('11', 'hh', 1) \
               .replace('59', 'mm', 1).replace('58', 'ss', 1)
示例#17
0
def format_datetime(dt):
    return babel_format_datetime(dt, format='long', locale='fr')[:-9]
示例#18
0
def format_datetime(date_time: datetime) -> str:
    return babel_format_datetime(date_time, format="long", locale="fr")[:-9]
示例#19
0
def format_datetime(value: date, format: str = "medium") -> str:
    return babel_format_datetime(value, format=format)
示例#20
0
文件: pdf.py 项目: nyuhuhuu/trachacks
 def _format_datetime(t, req):
     tz = getattr(req, 'tz', None)
     locale = getattr(req, 'locale', None)
     return babel_format_datetime(t, tzinfo=tz, locale=locale)