getReadableTimeBetween()   D
last analyzed

Complexity

Conditions 12

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
c 0
b 0
f 0
dl 0
loc 40
rs 4.8

How to fix   Complexity   

Complexity

Complex classes like getReadableTimeBetween() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
# Copyright (c) 2017 CorpNewt
2
# 
3
# This software is released under the MIT License.
4
# https://opensource.org/licenses/MIT
5
6
def getReadableTimeBetween(first, last):
7
    # A helper function to make a readable string between two times
8
    timeBetween = int(last - first)
9
    weeks = int(timeBetween / 604800)
10
    days = int((timeBetween - (weeks * 604800)) / 86400)
11
    hours = int((timeBetween - (days * 86400 + weeks * 604800)) / 3600)
12
    minutes = int((timeBetween - (hours * 3600 + days * 86400 + weeks * 604800)) / 60)
13
    seconds = int(timeBetween - (minutes * 60 + hours * 3600 + days * 86400 + weeks * 604800))
14
    msg = ""
15
16
    if weeks > 0:
17
        if weeks == 1:
18
            msg = '{}{} week, '.format(msg, str(weeks))
19
        else:
20
            msg = '{}{} weeks, '.format(msg, str(weeks))
21
    if days > 0:
22
        if days == 1:
23
            msg = '{}{} day, '.format(msg, str(days))
24
        else:
25
            msg = '{}{} days, '.format(msg, str(days))
26
    if hours > 0:
27
        if hours == 1:
28
            msg = '{}{} hour, '.format(msg, str(hours))
29
        else:
30
            msg = '{}{} hours, '.format(msg, str(hours))
31
    if minutes > 0:
32
        if minutes == 1:
33
            msg = '{}{} minute, '.format(msg, str(minutes))
34
        else:
35
            msg = '{}{} minutes, '.format(msg, str(minutes))
36
    if seconds > 0:
37
        if seconds == 1:
38
            msg = '{}{} second, '.format(msg, str(seconds))
39
        else:
40
            msg = '{}{} seconds, '.format(msg, str(seconds))
41
42
    if not msg:
43
        return "0 seconds"
44
    else:
45
        return msg[:-2]