35 lines
1.3 KiB
Python
35 lines
1.3 KiB
Python
# -*- coding: future_fstrings -*-
|
|
# mautrix-telegram - A Matrix-Telegram puppeting bridge
|
|
# Copyright (C) 2018 Tulir Asokan
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
def format_duration(seconds):
|
|
def pluralize(count, singular): return singular if count == 1 else singular + "s"
|
|
|
|
def include(count, word): return f"{count} {pluralize(count, word)}" if count > 0 else ""
|
|
|
|
minutes, seconds = divmod(seconds, 60)
|
|
hours, minutes = divmod(minutes, 60)
|
|
days, hours = divmod(hours, 24)
|
|
parts = [a for a in [
|
|
include(days, "day"),
|
|
include(hours, "hour"),
|
|
include(minutes, "minute"),
|
|
include(seconds, "second")] if a]
|
|
if len(parts) > 2:
|
|
return "{} and {}".format(", ".join(parts[:-1]), parts[-1])
|
|
return " and ".join(parts)
|