本文整理汇总了Python中babel.dates.get_timezone函数的典型用法代码示例。如果您正苦于以下问题:Python get_timezone函数的具体用法?Python get_timezone怎么用?Python get_timezone使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_timezone函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
开发者ID:CenterForOpenScience,项目名称:osf.io,代码行数:26,代码来源:emails.py
示例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)
开发者ID:XTech2K,项目名称:osf.io,代码行数:15,代码来源:emails.py
示例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')
开发者ID:sunliwen,项目名称:lektor,代码行数:25,代码来源:primitives.py
示例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)
开发者ID:indico,项目名称:indico,代码行数:7,代码来源:date_time.py
示例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') == ''
开发者ID:jpscaletti,项目名称:allspeak,代码行数:35,代码来源:test_l10n.py
示例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)
开发者ID:fph,项目名称:indico,代码行数:8,代码来源:date_time.py
示例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()
开发者ID:k3njiy,项目名称:indico,代码行数:8,代码来源:forms.py
示例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
开发者ID:jpscaletti,项目名称:allspeak,代码行数:8,代码来源:test_l10n.py
示例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"-"
开发者ID:dedeco,项目名称:cnddh-human-rights,代码行数:8,代码来源:filters.py
示例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'))
开发者ID:tpsh,项目名称:Server,代码行数:9,代码来源:app.py
示例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
开发者ID:baldwint,项目名称:clans,代码行数:9,代码来源:fmt.py
示例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
开发者ID:jpscaletti,项目名称:allspeak,代码行数:9,代码来源:utils.py
示例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))
开发者ID:rainerwahnsinn,项目名称:ptah,代码行数:44,代码来源:formatter.py
示例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)
开发者ID:k3njiy,项目名称:indico,代码行数:10,代码来源:controllers.py
示例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
开发者ID:stvstnfrd,项目名称:edx-certificates,代码行数:19,代码来源:gen_cert.py
示例16: time_formatter
def time_formatter(request, value, format='medium',
tzname=None, locale_name=None):
"""Time formatters
"""
if not isinstance(value, datetime) and not isinstance(value, time):
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_time(value, format, tzinfo, locale_name))
开发者ID:rainerwahnsinn,项目名称:ptah,代码行数:19,代码来源:formatter.py
示例17: test_datetime_timezone_est
def test_datetime_timezone_est(env, pad):
field = make_field(env, 'datetime')
with Context(pad=pad):
# Known timezone name, EST
rv = field.deserialize_value('2016-04-30 01:02:03 EST', pad=pad)
assert isinstance(rv, datetime.datetime)
assert rv.year == 2016
assert rv.month == 4
assert rv.day == 30
assert rv.hour == 1
assert rv.minute == 2
assert rv.second == 3
assert rv.tzinfo is get_timezone('EST')
开发者ID:azazel75,项目名称:lektor,代码行数:13,代码来源:test_types.py
示例18: timezones_choices
def timezones_choices():
"""Timezones values and their labels for current locale.
:return: an iterable of `(code, label)`, code being a timezone code and label
the timezone name in current locale.
"""
utcnow = pytz.utc.localize(datetime.utcnow())
locale = _get_locale()
for tz in sorted(pytz.common_timezones):
tz = get_timezone(tz)
now = tz.normalize(utcnow.astimezone(tz))
label = "({}) {}".format(get_timezone_gmt(now, locale=locale), tz.zone)
yield (tz, label) # get_timezone_name(tz, locale=locale))
开发者ID:abilian,项目名称:abilian-core,代码行数:13,代码来源:i18n.py
示例19: format_time
def format_time(t, format='short', locale=None, timezone=None, server_tz=False):
"""
Basically a wrapper around Babel's own format_time
"""
if not locale:
locale = currentLocale()
if not timezone and t.tzinfo:
timezone = DisplayTZ().getDisplayTZ()
elif server_tz:
timezone = HelperMaKaCInfo.getMaKaCInfoInstance().getTimezone()
if timezone:
timezone = get_timezone(timezone)
return _format_time(t, format=format, locale=locale, tzinfo=timezone).encode('utf-8')
开发者ID:sofian86,项目名称:indico-gh-test,代码行数:14,代码来源:date_time.py
示例20: test_datetime_timezone_location
def test_datetime_timezone_location(env, pad):
field = make_field(env, 'datetime')
with Context(pad=pad):
# Known location name, Asia/Seoul
rv = field.deserialize_value('2016-04-30 01:02:03 Asia/Seoul', pad=pad)
assert isinstance(rv, datetime.datetime)
assert rv.year == 2016
assert rv.month == 4
assert rv.day == 30
assert rv.hour == 1
assert rv.minute == 2
assert rv.second == 3
tzinfos = get_timezone('Asia/Seoul')._tzinfos # pylint: disable=no-member
assert rv.tzinfo in itervalues(tzinfos)
开发者ID:azazel75,项目名称:lektor,代码行数:14,代码来源:test_types.py
注:本文中的babel.dates.get_timezone函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论