Duration.py 2.97 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from collections import namedtuple

last_duration_data = 0
last_duration_signal = 0

def calculateDurations(distance_real, signal_speed_real, data_quantity, transfer_rate):
    Variables = namedtuple('Variables', 'duration_signal, duration_data')
    if distance_real == 0 or signal_speed_real == 0:
        duration_signal = 0
    else:
        duration_signal = distance_real / signal_speed_real
    if transfer_rate == 0 or data_quantity == 0:
        duration_data = 0
    else:
        duration_data = (data_quantity * 8 * 1024) / transfer_rate
    return Variables(duration_signal, duration_data)

def lastDurationData(duration_data, check_result):
#def lastDurationData(duration_data, check_result, tries):
    global last_duration_data
    
    if check_result == 1:
        last_duration_data = duration_data
    return last_duration_data
    
def lastDurationSignal(duration_signal, check_result):
#def lastDurationSignal(duration_signal, check_result, tries):
    global last_duration_signal
    
    if check_result == 1:
        last_duration_signal = duration_signal
    return last_duration_signal

def generateDurationText(duration):
    years = 0
    days = 0
    hours = 0
    minutes = 0
    seconds = 0
    duration_text = ""
    
    if duration < 60:
        duration_text = '%.2f' % duration + " sec"
#        print duration_text
        return duration_text
    
    if duration >= 31557600 :   # years
        years = duration // 31557600
        duration = duration - years * 31557600 
        if years == 1:
            duration_text = str(int(years)) + " year "
        else:
            duration_text = str(int(years)) + " years "
    if duration >= 86400:   # days
        days = duration // 86400
        duration = duration - days * 86400
        if days == 1:
            duration_text = duration_text + str(int(days)) + " day "
        else:
            duration_text = duration_text + str(int(days)) + " days "
    if duration >= 3600:    # hours
        hours = duration // 3600
        duration = duration - hours * 3600
        if years == 0 :
            if hours == 1:
                duration_text = duration_text + str(int(hours)) + " hour "
            else:
                duration_text = duration_text + str(int(hours)) + " hours "
    if duration >= 60:      # minutes
        minutes = duration // 60
        duration = duration - minutes * 60
        if years == 0 and days == 0 :           
            duration_text = duration_text + str(int(minutes)) + " min "              
    if duration > 0:      # seconds
        if years == 0 and days == 0 and hours == 0:
            seconds = duration
            duration_text = duration_text + str(int(seconds)) + " sec"

#    print duration_text
 
    return duration_text

#Test
#generateDurationText(500000000)
#generateDurationText(5000000)
#generateDurationText(50000)
#generateDurationText(500)
#generateDurationText(5)
#generateDurationText(0.05)