本文整理汇总了Python中babel.dates.format_timedelta方法的典型用法代码示例。如果您正苦于以下问题:Python dates.format_timedelta方法的具体用法?Python dates.format_timedelta怎么用?Python dates.format_timedelta使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类babel.dates
的用法示例。
在下文中一共展示了dates.format_timedelta方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: format_datetime
# 需要导入模块: from babel import dates [as 别名]
# 或者: from babel.dates import format_timedelta [as 别名]
def format_datetime(d):
from dateutil.tz import tzutc
from babel import dates
d = d.replace(microsecond=0)
if not USING_PYTHON2:
# Switch from UTC to local TZ
d = d.astimezone(tz=None)
return dates.format_timedelta(d - datetime.now(tzutc()), add_direction=True)
示例2: format_cell
# 需要导入模块: from babel import dates [as 别名]
# 或者: from babel.dates import format_timedelta [as 别名]
def format_cell(cell):
if isinstance(cell, datetime):
cell = format_datetime(cell)
if isinstance(cell, timedelta):
from babel import dates
cell = dates.format_timedelta(-cell, add_direction=True)
if isinstance(cell, (list, dict)):
cell = json.dumps(cell, default=lambda x: str(x))
return cell
示例3: deltatimeformat
# 需要导入模块: from babel import dates [as 别名]
# 或者: from babel.dates import format_timedelta [as 别名]
def deltatimeformat(t):
return dates.format_timedelta(t - datetime.now(), add_direction=True)
示例4: format_timedelta
# 需要导入模块: from babel import dates [as 别名]
# 或者: from babel.dates import format_timedelta [as 别名]
def format_timedelta(self, datetime_or_timedelta, granularity='second',
threshold=.85):
"""Formats the elapsed time from the given date to now or the given
timedelta. This currently requires an unreleased development version
of Babel.
:param datetime_or_timedelta:
A ``timedelta`` object representing the time difference to format,
or a ``datetime`` object in UTC.
:param granularity:
Determines the smallest unit that should be displayed, the value
can be one of "year", "month", "week", "day", "hour", "minute" or
"second".
:param threshold:
Factor that determines at which point the presentation switches to
the next higher unit.
:returns:
A string with the elapsed time.
"""
if isinstance(datetime_or_timedelta, datetime.datetime):
datetime_or_timedelta = datetime.datetime.utcnow() - \
datetime_or_timedelta
return dates.format_timedelta(datetime_or_timedelta, granularity,
threshold=threshold,
locale=self.locale)
示例5: uptime
# 需要导入模块: from babel import dates [as 别名]
# 或者: from babel.dates import format_timedelta [as 别名]
def uptime(caller):
delta = datetime.datetime.utcnow() - globals.server.started
caller.connection.notify(caller.connection._("This server has been running for %s.")%(format_timedelta(delta, locale=caller.connection.player.get_locale())))
if globals.rebooting:
caller.connection.notify(caller.connection._("This server is going to reboot soon."))
示例6: cmd_ro
# 需要导入模块: from babel import dates [as 别名]
# 或者: from babel.dates import format_timedelta [as 别名]
def cmd_ro(message: types.Message, chat: Chat):
duration = await parse_timedelta_from_message(message)
if not duration:
return
try: # Apply restriction
await message.chat.restrict(
message.reply_to_message.from_user.id, can_send_messages=False, until_date=duration
)
logger.info(
"User {user} restricted by {admin} for {duration}",
user=message.reply_to_message.from_user.id,
admin=message.from_user.id,
duration=duration,
)
except exceptions.BadRequest as e:
logger.error("Failed to restrict chat member: {error!r}", error=e)
return False
await message.reply_to_message.answer(
_("<b>Read-only</b> activated for user {user}. Duration: {duration}").format(
user=message.reply_to_message.from_user.get_mention(),
duration=format_timedelta(
duration, locale=chat.language, granularity="seconds", format="short"
),
)
)
return True
示例7: cmd_ban
# 需要导入模块: from babel import dates [as 别名]
# 或者: from babel.dates import format_timedelta [as 别名]
def cmd_ban(message: types.Message, chat: Chat):
duration = await parse_timedelta_from_message(message)
if not duration:
return
try: # Apply restriction
await message.chat.kick(message.reply_to_message.from_user.id, until_date=duration)
logger.info(
"User {user} kicked by {admin} for {duration}",
user=message.reply_to_message.from_user.id,
admin=message.from_user.id,
duration=duration,
)
except exceptions.BadRequest as e:
logger.error("Failed to kick chat member: {error!r}", error=e)
return False
await message.reply_to_message.answer(
_("User {user} <b>banned</b> for {duration}").format(
user=message.reply_to_message.from_user.get_mention(),
duration=format_timedelta(
duration, locale=chat.language, granularity="seconds", format="short"
),
)
)
return True
示例8: uptime
# 需要导入模块: from babel import dates [as 别名]
# 或者: from babel.dates import format_timedelta [as 别名]
def uptime(message):
"""
Processes uptime commands in the messages
:param message: message to process
:return:
"""
logger = logging.getLogger(__name__)
msgdetail = stampy.stampy.getmsgdetail(message)
chat_id = msgdetail["chat_id"]
message_id = msgdetail["message_id"]
datelast = stampy.stampy.utize(dateutil.parser.parse(stampy.plugin.config.config(key='uptime', gid=0)))
datelastfor = datelast.strftime('%Y/%m/%d %H:%M:%S')
datelastts = time.mktime(datelast.timetuple())
date = stampy.stampy.utize(datetime.datetime.now())
datefor = date.strftime('%Y/%m/%d %H:%M:%S')
dateforts = time.mktime(date.timetuple())
elapsed = dateforts - datelastts
text = _("Bot was started at: %s\n") % datelastfor
text += _("Now it is: %s\n") % datefor
text += _("Elapsed time: %s (seconds)\n") % elapsed
text += _("Elapsed time: %s \n") % format_timedelta(datetime.timedelta(seconds=elapsed), locale=stampy.stampy.language)
logger.debug(msg=_L("Returning %s") % text)
stampy.stampy.sendmessage(chat_id=chat_id, text=text,
reply_to_message_id=message_id,
disable_web_page_preview=True,
parse_mode='markdown')
return