| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent cc92d51 commit 854a2d1
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -137,22 +137,25 @@ def get_object_type_by_name( | |||
| 137 | 137 | ||
| 138 | 138 | ||
| 139 | 139 | def utctz_to_altz(utctz: str) -> int: | |
| 140 | - """we convert utctz to the timezone in seconds, it is the format time.altzone | ||
| 141 | - returns. Git stores it as UTC timezone which has the opposite sign as well, | ||
| 142 | - which explains the -1 * ( that was made explicit here ) | ||
| 143 | - | ||
| 144 | - :param utctz: git utc timezone string, i.e. +0200""" | ||
| 145 | - return -1 * int(float(utctz) / 100 * 3600) | ||
| 146 | - | ||
| 147 | - | ||
| 148 | - def altz_to_utctz_str(altz: float) -> str: | ||
| 149 | - """As above, but inverses the operation, returning a string that can be used | ||
| 150 | - in commit objects""" | ||
| 151 | - utci = -1 * int((float(altz) / 3600) * 100) | ||
| 152 | - utcs = str(abs(utci)) | ||
| 153 | - utcs = "0" * (4 - len(utcs)) + utcs | ||
| 154 | - prefix = (utci < 0 and "-") or "+" | ||
| 155 | - return prefix + utcs | ||
| 140 | + """Convert a git timezone offset into a timezone offset west of | ||
| 141 | + UTC in seconds (compatible with time.altzone). | ||
| 142 | + | ||
| 143 | + :param utctz: git utc timezone string, i.e. +0200 | ||
| 144 | + """ | ||
| 145 | + int_utctz = int(utctz) | ||
| 146 | + seconds = ((abs(int_utctz) // 100) * 3600 + (abs(int_utctz) % 100) * 60) | ||
| 147 | + return seconds if int_utctz < 0 else -seconds | ||
| 148 | + | ||
| 149 | + | ||
| 150 | + def altz_to_utctz_str(altz: int) -> str: | ||
| 151 | + """Convert a timezone offset west of UTC in seconds into a git timezone offset string | ||
| 152 | + | ||
| 153 | + :param altz: timezone offset in seconds west of UTC | ||
| 154 | + """ | ||
| 155 | + hours = abs(altz) // 3600 | ||
| 156 | + minutes = (abs(altz) % 3600) // 60 | ||
| 157 | + sign = "-" if altz >= 60 else "+" | ||
| 158 | + return "{}{:02}{:02}".format(sign, hours, minutes) | ||
| 156 | 159 | ||
| 157 | 160 | ||
| 158 | 161 | def verify_utctz(offset: str) -> str: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -333,6 +333,27 @@ def test_iterable_list(self, case): | |||
| 333 | 333 | self.assertRaises(IndexError, ilist.__delitem__, 0) | |
| 334 | 334 | self.assertRaises(IndexError, ilist.__delitem__, "something") | |
| 335 | 335 | ||
| 336 | + def test_utctz_to_altz(self): | ||
| 337 | + self.assertEqual(utctz_to_altz("+0000"), 0) | ||
| 338 | + self.assertEqual(utctz_to_altz("+1400"), -(14 * 3600)) | ||
| 339 | + self.assertEqual(utctz_to_altz("-1200"), 12 * 3600) | ||
| 340 | + self.assertEqual(utctz_to_altz("+0001"), -60) | ||
| 341 | + self.assertEqual(utctz_to_altz("+0530"), -(5 * 3600 + 1800)) | ||
| 342 | + self.assertEqual(utctz_to_altz("-0930"), 9 * 3600 + 1800) | ||
| 343 | + | ||
| 344 | + def test_altz_to_utctz_str(self): | ||
| 345 | + self.assertEqual(altz_to_utctz_str(0), "+0000") | ||
| 346 | + self.assertEqual(altz_to_utctz_str(-(14 * 3600)), "+1400") | ||
| 347 | + self.assertEqual(altz_to_utctz_str(12 * 3600), "-1200") | ||
| 348 | + self.assertEqual(altz_to_utctz_str(-60), "+0001") | ||
| 349 | + self.assertEqual(altz_to_utctz_str(-(5 * 3600 + 1800)), "+0530") | ||
| 350 | + self.assertEqual(altz_to_utctz_str(9 * 3600 + 1800), "-0930") | ||
| 351 | + | ||
| 352 | + self.assertEqual(altz_to_utctz_str(1), "+0000") | ||
| 353 | + self.assertEqual(altz_to_utctz_str(59), "+0000") | ||
| 354 | + self.assertEqual(altz_to_utctz_str(-1), "+0000") | ||
| 355 | + self.assertEqual(altz_to_utctz_str(-59), "+0000") | ||
| 356 | + | ||
| 336 | 357 | def test_from_timestamp(self): | |
| 337 | 358 | # Correct offset: UTC+2, should return datetime + tzoffset(+2) | |
| 338 | 359 | altz = utctz_to_altz("+0200") | |
| Back | FazBrowse Home | New Git URL |
0 commit comments