0

I've got a list of elapsed times in minutes and I'm trying to reformat them as strings of weeks, days, hours and minutes.

So, for example, 7,480 minutes = 3 weeks, 4 hours and 40 minutes.

I'd like the format to be like so: '3w4h40m'

Using divmod I've written a function to break the minutes down into weeks, days, hours and minutes and return the string:

def formattime(time):
    tl = [divmod(divmod(divmod(time,60)[0],8)[0],5)[0],divmod(divmod(divmod(time,60)[0],8)[0],5)[1],divmod(divmod(time,60)[0],8)[1],divmod(time,60)[1]]

    timestring = str(tl[0])+'w'+str(tl[1])+'d'+str(tl[2])+'h'+str(tl[3])+'m'

    return timestring

However, I don't want it to return a figure for any of weeks, days, hours or minutes if the number is zero:

>>> formattime(7480)
'3w0d4h40m'

Is there a pythonic and straightforward way of returning '3w4h40m'?

Charon
  • 2,344
  • 6
  • 25
  • 44
  • 2
    I wouldn't call your function as it stands now, Pythonic.. – Martijn Pieters Mar 10 '16 at 20:08
  • @MartijnPieters - yes, I know, it's very ugly and unpythonic - I was hoping to find a more elegant alternative. – Charon Mar 10 '16 at 20:09
  • @vaultah - I don't follow, could you elaborate? – Charon Mar 10 '16 at 20:10
  • Why not just break it out into a half dozen statements or so? You don't win prizes by cramming code into one line or one big list comprehension. "pythonic" is more about clarity than it is about using cool python features. – Bryan Oakley Mar 10 '16 at 20:10
  • @BryanOakley - I know, it's unnecessary - but that notwithstanding, do you know of a way for me to achieve my aim? – Charon Mar 10 '16 at 20:11
  • Not sure how 7480 breaks down to over 3 weeks. It is barely 6 days. – Martijn Pieters Mar 10 '16 at 20:14
  • @MartijnPieters - Sorry, I should have said - I'm counting days in 8 hour blocks and weeks as 5 days - working days and working weeks, in other words. Your function has given me the ability to do that, so thank you so much, I'll accept your answer. – Charon Mar 10 '16 at 20:15
  • @Charon: yes, by splitting the code into a half dozen statements or so... – Bryan Oakley Mar 10 '16 at 20:16
  • 2
    @Charon: see, another reason to keep not try to cram everything onto one line! That would have been far more easily deduced if the code had been more readable. – Martijn Pieters Mar 10 '16 at 20:18
  • @MartijnPieters - yes, that was silly of me.... sorry! – Charon Mar 10 '16 at 20:19

1 Answers1

2

You could filter out the components at 0 if you had a list of them:

def formattime(time):
    # format to workweeks, workdays, hours and minutes
    # a workweek is 5 days, a workday is 8 hours
    hours, minutes = divmod(time, 60)
    days, hours = divmod(hours, 8)  # work days
    weeks, days = divmod(days, 5)   # work weeks
    components = [str(v) + l for l, v in zip('wdhm', (weeks, days, hours, minutes)) if v]
    return ''.join(components) or '0m'

Demo:

>>> formattime(7480)
'3w4h40m'
>>> formattime(0)
'0m'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343