Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use named parameters in get_timestamp() time formatting strings #2292

Merged
merged 1 commit into from
Aug 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions picard/util/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# Copyright (C) 2021 Gabriel Ferreira
# Copyright (C) 2021 Laurent Monin
# Copyright (C) 2021 Philipp Wolfer
# Copyright (C) 2021, 2023 Philipp Wolfer
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
Expand All @@ -20,12 +20,17 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

from collections import namedtuple


SECS_IN_DAY = 86400
SECS_IN_HOUR = 3600
SECS_IN_MINUTE = 60


Duration = namedtuple('Duration', 'days hours minutes seconds')


def euclidian_div(a, b):
return a // b, a % b

Expand All @@ -34,17 +39,17 @@ def seconds_to_dhms(seconds):
days, seconds = euclidian_div(seconds, SECS_IN_DAY)
hours, seconds = euclidian_div(seconds, SECS_IN_HOUR)
minutes, seconds = euclidian_div(seconds, SECS_IN_MINUTE)
return days, hours, minutes, seconds
return Duration(days=days, hours=hours, minutes=minutes, seconds=seconds)


def get_timestamp(seconds):
d, h, m, s = seconds_to_dhms(seconds)
if d > 0:
return _("%.2dd %.2dh") % (d, h)
if h > 0:
return _("%.2dh %.2dm") % (h, m)
if m > 0:
return _("%.2dm %.2ds") % (m, s)
if s > 0:
return _("%.2ds") % s
time = seconds_to_dhms(seconds)
if time.days > 0:
return _("%(days).2dd %(hours).2dh") % time._asdict()
if time.hours > 0:
return _("%(hours).2dh %(minutes).2dm") % time._asdict()
if time.minutes > 0:
return _("%(minutes).2dm %(seconds).2ds") % time._asdict()
if time.seconds > 0:
return _("%(seconds).2ds") % time._asdict()
return ''
Loading