|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# This simple set of python functions makes it easy to do simple math with |
| 4 | +# times formatted as <hr>h<min>m<sec>s. This makes it easier to analyze |
| 5 | +# timing data that is spit out in that form. |
| 6 | +# |
| 7 | + |
| 8 | +import sys |
| 9 | +import os |
| 10 | + |
| 11 | +def hms2s(hms): |
| 12 | + #print "mmss =", mmss |
| 13 | + hms_len = len(hms) |
| 14 | + h_idx = hms.find("h") |
| 15 | + m_idx = hms.find("m") |
| 16 | + s_idx = hms.find("s") |
| 17 | + # hours |
| 18 | + hours = 0.0 |
| 19 | + if h_idx > 0: |
| 20 | + h_start = 0 |
| 21 | + hours = float(hms[0:h_idx]) |
| 22 | + # ToDo: Handle 'm' and 's' |
| 23 | + # minutes |
| 24 | + minutes = 0.0 |
| 25 | + if m_idx > 0: |
| 26 | + m_start = 0 |
| 27 | + if h_idx > 0: |
| 28 | + m_start = h_idx + 1 |
| 29 | + minutes = float(hms[m_start:m_idx]) |
| 30 | + # seconds |
| 31 | + seconds = 0.0 |
| 32 | + if s_idx > 0: |
| 33 | + s_start = 0 |
| 34 | + if m_idx > 0: |
| 35 | + s_start = m_idx + 1 |
| 36 | + elif h_idx > 0: |
| 37 | + s_start = h_idx + 1 |
| 38 | + seconds = float(hms[s_start:s_idx]) |
| 39 | + return hours*3600 + minutes*60 + seconds |
| 40 | + |
| 41 | +def s2hms(seconds): |
| 42 | + s_digits = 5 |
| 43 | + #print "" |
| 44 | + #print "seconds =", seconds |
| 45 | + hours = int(seconds) / 3600 |
| 46 | + #print "hours =", hours |
| 47 | + seconds_h_remainder = round(seconds - hours*3600, s_digits) |
| 48 | + #print "seconds_h_remainder =", seconds_h_remainder |
| 49 | + minutes = int(seconds_h_remainder) / 60 |
| 50 | + #print "mintues =", minutes |
| 51 | + seconds_reminder = round(seconds_h_remainder - minutes*60, s_digits) |
| 52 | + #print "seconds_reminder = ", seconds_reminder |
| 53 | + h_str = "" |
| 54 | + if hours != 0: |
| 55 | + h_str = str(hours)+"h" |
| 56 | + m_str = "" |
| 57 | + if minutes != 0: |
| 58 | + m_str = str(minutes)+"m" |
| 59 | + s_str = "" |
| 60 | + if seconds_reminder != 0.0: |
| 61 | + s_str = str(seconds_reminder)+"s" |
| 62 | + return h_str + m_str + s_str |
| 63 | + |
| 64 | +def sub_hms(hms1, hms2): |
| 65 | + num1 = hms2s(hms1) |
| 66 | + num2 = hms2s(hms2) |
| 67 | + return s2hms(num1 - num2) |
| 68 | + |
| 69 | +def div_hms(hms_num, hms_denom): |
| 70 | + num_num = hms2s(hms_num) |
| 71 | + num_denom = hms2s(hms_denom) |
| 72 | + return num_num/num_denom |
| 73 | + |
| 74 | + |
| 75 | +# |
| 76 | +# Unit test suite |
| 77 | +# |
| 78 | + |
| 79 | +if __name__ == '__main__': |
| 80 | + |
| 81 | + import unittest |
| 82 | + |
| 83 | + class test_hms2s(unittest.TestCase): |
| 84 | + |
| 85 | + def setUp(self): |
| 86 | + None |
| 87 | + |
| 88 | + def test_s1(self): |
| 89 | + self.assertEqual(hms2s("2s"), 2.0) |
| 90 | + |
| 91 | + def test_s2(self): |
| 92 | + self.assertEqual(hms2s("2.53s"), 2.53) |
| 93 | + |
| 94 | + def test_s3(self): |
| 95 | + self.assertEqual(hms2s("0m4.5s"), 4.5) |
| 96 | + |
| 97 | + def test_m1(self): |
| 98 | + self.assertEqual(hms2s("1m2.4s"), 62.4) |
| 99 | + |
| 100 | + def test_m2(self): |
| 101 | + self.assertEqual(hms2s("3m10.531s"), 190.531) |
| 102 | + |
| 103 | + def test_h1(self): |
| 104 | + self.assertEqual(hms2s("2h"), 7200.0) |
| 105 | + |
| 106 | + def test_h1(self): |
| 107 | + self.assertEqual(hms2s("2.5h"), 9000.0) |
| 108 | + |
| 109 | + def test_h2(self): |
| 110 | + self.assertEqual(hms2s("1h2m3s"), 3723.0) |
| 111 | + |
| 112 | + def test_h3(self): |
| 113 | + self.assertEqual(hms2s("1h3s"), 3603.0) |
| 114 | + |
| 115 | + class test_s2hms(unittest.TestCase): |
| 116 | + |
| 117 | + def setUp(self): |
| 118 | + None |
| 119 | + |
| 120 | + def test_s1(self): |
| 121 | + self.assertEqual(s2hms(2.0), "2.0s") |
| 122 | + |
| 123 | + def test_s2(self): |
| 124 | + self.assertEqual(s2hms(3.456), "3.456s") |
| 125 | + |
| 126 | + def test_s3(self): |
| 127 | + self.assertEqual(s2hms(60.0), "1m") |
| 128 | + |
| 129 | + def test_m1(self): |
| 130 | + self.assertEqual(s2hms(75.346), "1m15.346s") |
| 131 | + |
| 132 | + def test_m2(self): |
| 133 | + self.assertEqual(s2hms(121.25), "2m1.25s") |
| 134 | + |
| 135 | + def test_m3(self): |
| 136 | + self.assertEqual(s2hms(60.0), "1m") |
| 137 | + |
| 138 | + def test_h1(self): |
| 139 | + self.assertEqual(s2hms(3600.0), "1h") |
| 140 | + |
| 141 | + def test_h2(self): |
| 142 | + self.assertEqual(s2hms(3600.001), "1h0.001s") |
| 143 | + |
| 144 | + def test_h3(self): |
| 145 | + self.assertEqual(s2hms(3660.0), "1h1m") |
| 146 | + |
| 147 | + def test_h4(self): |
| 148 | + self.assertEqual(s2hms(7140.0), "1h59m") |
| 149 | + |
| 150 | + def test_h5(self): |
| 151 | + self.assertEqual(s2hms(7141.0), "1h59m1.0s") |
| 152 | + |
| 153 | + def test_h5(self): |
| 154 | + self.assertEqual(s2hms(2*3600+3*60+7.82), "2h3m7.82s") |
| 155 | + |
| 156 | + class test_sub_hms(unittest.TestCase): |
| 157 | + |
| 158 | + def setUp(self): |
| 159 | + None |
| 160 | + |
| 161 | + def test_1(self): |
| 162 | + self.assertEqual(sub_hms("2s", "1s"), "1.0s") |
| 163 | + |
| 164 | + def test_2(self): |
| 165 | + self.assertEqual(sub_hms("1m5.23s", "45s"), "20.23s") |
| 166 | + |
| 167 | + class test_div_hms(unittest.TestCase): |
| 168 | + |
| 169 | + def setUp(self): |
| 170 | + None |
| 171 | + |
| 172 | + def test_1(self): |
| 173 | + self.assertEqual(div_hms("2s", "1s"), 2.0) |
| 174 | + |
| 175 | + def test_2(self): |
| 176 | + self.assertEqual(div_hms("1s", "2s"), 0.5) |
| 177 | + |
| 178 | + def test_3(self): |
| 179 | + self.assertEqual(div_hms("1m50s", "55s"), 2.0) |
| 180 | + |
| 181 | + def test_4(self): |
| 182 | + self.assertEqual(div_hms("55s", "1m50s"), 0.5) |
| 183 | + |
| 184 | + unittest.main() |
0 commit comments