本文整理汇总了Python中babel.dates.get_timezone函数的典型用法代码示例。如果您正苦于以下问题:Python get_timezone函数的具体用法?Python get_timezone怎么用?Python get_timezone使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_timezone函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: localize_timestamp
def localize_timestamp(timestamp, user):
try:
user_timezone = dates.get_timezone(user.timezone)
except LookupError:
user_timezone = dates.get_timezone('Etc/UTC')
try:
user_locale = Locale(user.locale)
except core.UnknownLocaleError:
user_locale = Locale('en')
# Do our best to find a valid locale
try:
user_locale.date_formats
except IOError: # An IOError will be raised if locale's casing is incorrect, e.g. de_de vs. de_DE
# Attempt to fix the locale, e.g. de_de -> de_DE
try:
user_locale = Locale(fix_locale(user.locale))
user_locale.date_formats
except (core.UnknownLocaleError, IOError):
user_locale = Locale('en')
formatted_date = dates.format_date(timestamp, format='full', locale=user_locale)
formatted_time = dates.format_time(timestamp, format='short', tzinfo=user_timezone, locale=user_locale)
return u'{time} on {date}'.format(time=formatted_time, date=formatted_date)
示例2: localize_timestamp
def localize_timestamp(timestamp, user):
try:
user_timezone = dates.get_timezone(user.timezone)
except LookupError:
user_timezone = dates.get_timezone('Etc/UTC')
try:
user_locale = Locale(user.locale)
except core.UnknownLocaleError:
user_locale = 'en'
formatted_date = dates.format_date(timestamp, format='full', locale=user_locale)
formatted_time = dates.format_time(timestamp, format='short', tzinfo=user_timezone, locale=user_locale)
return u'{time} on {date}'.format(time=formatted_time, date=formatted_date)
示例3: value_from_raw
def value_from_raw(self, raw):
if raw.value is None:
return raw.missing_value('Missing datetime')
try:
chunks = raw.value.split(' ')
date_info = [int(bit) for bit in chunks[0].split('-')]
time_info = [int(bit) for bit in chunks[1].split(':')]
datetime_info = date_info + time_info
result = datetime(*datetime_info)
if len(chunks) > 2:
try:
tz = get_timezone(chunks[-1])
except LookupError:
if len(chunks[-1]) > 5:
chunks[-1] = chunks[-1][-5:]
delta = int(chunks[-1][1:3]) * 60 + int(chunks[-1][3:])
if chunks[-1][0] == '-':
delta *= -1
tz = FixedOffset(delta)
return tz.localize(result)
return result
except Exception:
return raw.bad_value('Bad date format')
示例4: server_to_utc
def server_to_utc(dt):
"""Converts the given datetime in the server's TZ to UTC.
The given datetime **MUST** be naive but already contain the correct time in the server's TZ.
"""
server_tz = get_timezone(config.DEFAULT_TIMEZONE)
return server_tz.localize(dt).astimezone(pytz.utc)
示例5: test_format
def test_format():
l10n = L10n()
d = date(2007, 4, 1)
dformat = "EEE, MMM d, ''yy"
expected = u"Sun, Apr 1, '07"
assert l10n.format(d, dformat, locale='en') == expected
dt = datetime(2007, 4, 1, 15, 30)
dformat = "yyyyy.MMMM.dd GGG hh:mm a"
expected = u'02007.April.01 AD 03:30 PM'
assert l10n.format(dt, dformat, locale='en') == expected
dt = datetime(2007, 4, 1, 15, 30, tzinfo=UTC)
eastern = get_timezone('US/Eastern')
dformat = 'H:mm Z'
expected = u'11:30 -0400'
assert l10n.format(dt, dformat, tzinfo=eastern, locale='en_US') == expected
t = time(15, 30)
tformat = "hh 'o''clock' a"
expected = u"03 o'clock PM"
assert l10n.format(t, tformat, locale='en') == expected
delta = timedelta(days=6)
assert l10n.format(delta, locale='en_US') == u'1 week'
assert l10n.format(delta, granularity='month', locale='en_US') == u'1 month'
assert l10n.format(1099, locale='en_US') == u'1,099'
assert l10n.format(1.2345, locale='en_US') == u'1.234'
assert l10n.format('test', locale='en') == 'test'
assert l10n.format(None, locale='en_US') == ''
示例6: event_to_utc
def event_to_utc(dt, event):
"""Converts a datetime of an event to UTC.
:param dt: A naive :class:`datetime.datetime` object in UTC
:param event: An :class:`.Conference` object from which to take the timezone
"""
timezone = DisplayTZ(conf=event).getDisplayTZ()
return get_timezone(timezone).localize(dt).astimezone(pytz.utc)
示例7: scheduled_dt
def scheduled_dt(self):
if self.schedule_type.data == 'absolute':
dt = datetime.combine(self.absolute_date.data, self.absolute_time.data)
return get_timezone(self.timezone).localize(dt).astimezone(pytz.utc)
elif self.schedule_type.data == 'relative':
return self.event.getStartDate() - self.relative_delta.data
elif self.schedule_type.data == 'now':
return now_utc()
示例8: test_format_datetime_tzinfo
def test_format_datetime_tzinfo():
l10n = L10n()
dt = datetime(2007, 4, 1, 15, 30, tzinfo=UTC)
eastern = get_timezone('US/Eastern')
dformat = 'H:mm Z'
expected = u'11:30 -0400'
assert l10n.format_datetime(dt, dformat, tzinfo=eastern, locale='en_US') == expected
示例9: getdatelocalized
def getdatelocalized(value):
if value:
from datetime import datetime
from babel.dates import format_datetime, get_timezone
return format_datetime(value, tzinfo=get_timezone(TIMEZONE), locale=LOCALE, format="d 'de' MMMM 'em' HH':'mm")
else:
return u"-"
示例10: format_datetime
def format_datetime(value, format='medium'):
print(value)
value = parse(str(value))
if format == 'full':
format="EEEE, d. MMMM y 'at' HH:mm"
elif format == 'medium':
format="EE dd.MM.y HH:mm"
return dates.format_datetime(value, format, tzinfo=dates.get_timezone('Asia/Vladivostok'))
示例11: __init__
def __init__(self,
date_format='E MMMM d YYYY, h:mm a',
timezone=None,
):
self.date_format = date_format
if timezone is not None:
self.tzinfo = get_timezone(timezone)
else:
self.tzinfo = LOCALTZ
示例12: normalize_timezone
def normalize_timezone(tzinfo):
if not tzinfo:
return
if isinstance(tzinfo, datetime.tzinfo):
return tzinfo
try:
return get_timezone(tzinfo)
except LookupError:
return
示例13: datetime_formatter
def datetime_formatter(request, value, format='medium',
tzname=None, locale_name=None):
"""DateTime formatter
Short::
>> dt = datetime(2011, 2, 6, 10, 35, 45, 80, pytz.UTC)
>> request.fmt.datetime(dt, 'short')
'02/06/11 04:35 AM'
Medium::
>> request.fmt.datetime(dt, 'medium')
'Feb 06, 2011 04:35 AM'
Long::
>> request.fmt.datetime(dt, 'long')
'February 06, 2011 04:35 AM -0600'
Full::
>> request.fmt.datetime(dt, 'full')
'Sunday, February 06, 2011 04:35:45 AM CST'
"""
if not isinstance(value, datetime):
return value
tzinfo = None
if tzname:
tzinfo = get_timezone(tzname)
if not tzinfo:
settings = request.registry.settings
tzinfo = get_timezone(settings['ptah.timezone'])
if not locale_name:
locale_name = request.locale_name
return text_type(format_datetime(value, format, tzinfo, locale_name))
示例14: _get_defaults
def _get_defaults(self):
reminder = self.reminder
if reminder.is_relative:
defaults_kwargs = {"schedule_type": "relative", "relative_delta": reminder.event_start_delta}
else:
# Use the user's preferred event timezone
tz = get_timezone(DisplayTZ(conf=self.event).getDisplayTZ())
dt = reminder.scheduled_dt.astimezone(tz)
defaults_kwargs = {"schedule_type": "absolute", "absolute_date": dt.date(), "absolute_time": dt.time()}
return FormDefaults(reminder, **defaults_kwargs)
示例15: get_cert_date
def get_cert_date(force_date, locale, timezone):
"""
Get pertinent date for display on cert
"""
locale = locale or settings.DEFAULT_LOCALE
timezone = timezone or settings.TIMEZONE
if force_date:
date = datetime.strptime(force_date, '%Y-%m-%d')
else:
from_zone = get_timezone(settings.TIMEZONE)
to_zone = get_timezone(timezone)
date = get_today()
date = date.replace(tzinfo=from_zone)
date = date.astimezone(to_zone)
date = date.date()
date = format_date(date, 'long', locale=locale)
date = unicode(date)
return date