Statistics
| Branch: | Revision:

nepatest_popbabel / mylib / mytime.py @ 60ba786f

History | View | Annotate | Download (2.01 KB)

1
class MyTime:
2
        string = "0:0:0.0"
3
        
4
        @classmethod
5
        def vecToString(cls, vec):
6
                mus=vec[-1]
7
                highORlow="high"
8
                if (mus<0.5):
9
                        highORlow="low"
10
                return str(vec[0])+":"+str(vec[1])+":"+str(vec[2])+"-"+highORlow
11
        
12
        def __init__(self, timestring="0:0:0.0"):
13
                self.string=timestring
14
                spl=self.string.split(':')
15
                self.mh=int(spl[-3])
16
                self.mm=int(spl[-2])
17
                self.ms=int(spl[-1].split('.')[0])
18
                self.mus=int(spl[-1].split('.')[1]) * (10**-6)
19
                self.time=self.mh *60 * 60 + self.mm * 60 + self.ms + self.mus
20

    
21
        def asHMSUVector(self):
22
                if (self.mus<0.5):
23
                        retmus=0
24
                else:
25
                        retmus=5
26
                #print "\t\tmus="+str(self.mus)+", retmus="+str(retmus)
27
                return (self.mh,self.mm,self.ms,retmus)
28

    
29
        def asHMSVector(self):
30
                return (self.mh,self.mm,self.ms)
31

    
32
        def toString(self):
33
                return self.string
34

    
35
        def toFloat(self):
36
                return self.time
37

    
38
        #se self e' piu' tardi ritorna 1, -1 altrimenti
39
        #si assume che i log siano stati fatti nella stessa ora!
40
        def vecCompare(self,v2):
41
                if (len(v2)>3):
42
                        h1,m1,s1,hl1=self.asHMSUVector()
43
                        h2,m2,s2,hl2=v2
44
                else:
45
                        h1,m1,s1=self.asHMSVector()
46
                        h2,m2,s2=v2
47

    
48
                if (h2 != h1):
49
                        if (h2>h1):
50
                                return -1
51
                        else:
52
                                return 1
53
                if (m2 != m1):
54
                        if (m2>m1):
55
                                return -1
56
                        else:
57
                                return 1
58
                if (s2 != s1):
59
                        if (s2>s1):
60
                                return -1
61
                        else:
62
                                return 1
63
                if (len(v2)<4):
64
                        if(s2==s1):
65
                                return 0
66
                else:
67
                        if (hl2 != hl1):
68
                                if (hl2>hl1):
69
                                        return -1
70
                                else:
71
                                        return 1
72
                return 0
73

    
74
        def __cmp__(self,obj):
75
                return self.vecCompare(obj.asHMSUVector())
76

    
77
        @classmethod
78
        def priorHMSUVOne(cls,vec):
79
                h1,m1,s1,hl1=vec
80
                if hl1<5:
81
                        if s1>0:
82
                                return (h1,m1,s1-1,hl1+5)
83
                        else:
84
                                if m1>0:
85
                                        return (h1,m1-1,59,hl1+5)
86
                                else:
87
                                        raise Exception("Non trovo tempo prima, sfiga nera")
88
                else:
89
                        return (h1,m1,s1,hl1-5)
90

    
91
        @classmethod
92
        def nextHMSUVOne(cls,vec):
93
                h1,m1,s1,hl1=vec
94
                if hl1<5:
95
                        return (h1,m1,s1,hl1+5)
96
                else:
97
                        if s1<59:
98
                                return (h1,m1,s1+1,hl1-5)
99
                        else:
100
                                if m1<59:
101
                                        return (h1,m1+1,0,hl1-5)
102
                                else:
103
                                        raise Exception("Non trovo tempo dopo, sfiga nera")