|
16 | 16 | import os |
17 | 17 |
|
18 | 18 | __all__ = ('get_object_type_by_name', 'get_user_id', 'parse_date', 'parse_actor_and_date', |
19 | | - 'ProcessStreamAdapter', 'Traversable') |
| 19 | + 'ProcessStreamAdapter', 'Traversable', 'altz_to_utctz_str', 'utctz_to_altz', |
| 20 | + 'verify_utctz') |
20 | 21 |
|
21 | 22 | def get_object_type_by_name(object_type_name): |
22 | 23 | """ |
@@ -57,14 +58,24 @@ def get_user_id(): |
57 | 58 | return "%s@%s" % (username, platform.node()) |
58 | 59 |
|
59 | 60 |
|
60 | | -def _utc_tz_to_altz(utctz): |
| 61 | +def utctz_to_altz(utctz): |
61 | 62 | """we convert utctz to the timezone in seconds, it is the format time.altzone |
62 | 63 | returns. Git stores it as UTC timezon which has the opposite sign as well, |
63 | 64 | which explains the -1 * ( that was made explicit here ) |
64 | 65 | :param utctz: git utc timezone string, i.e. +0200""" |
65 | 66 | return -1 * int(float(utctz)/100*3600) |
| 67 | + |
| 68 | +def altz_to_utctz_str(altz): |
| 69 | + """As above, but inverses the operation, returning a string that can be used |
| 70 | + in commit objects""" |
| 71 | + utci = -1 * int((altz / 3600)*100) |
| 72 | + utcs = str(abs(utci)) |
| 73 | + utcs = "0"*(4-len(utcs)) + utcs |
| 74 | + prefix = (utci < 0 and '-') or '+' |
| 75 | + return prefix + utcs |
| 76 | + |
66 | 77 |
|
67 | | -def _verify_utctz(offset): |
| 78 | +def verify_utctz(offset): |
68 | 79 | """:raise ValueError: if offset is incorrect |
69 | 80 | :return: offset""" |
70 | 81 | fmt_exc = ValueError("Invalid timezone offset format: %s" % offset) |
@@ -97,11 +108,11 @@ def parse_date(string_date): |
97 | 108 | if string_date.count(' ') == 1 and string_date.rfind(':') == -1: |
98 | 109 | timestamp, offset = string_date.split() |
99 | 110 | timestamp = int(timestamp) |
100 | | - return timestamp, _utc_tz_to_altz(_verify_utctz(offset)) |
| 111 | + return timestamp, utctz_to_altz(verify_utctz(offset)) |
101 | 112 | else: |
102 | 113 | offset = "+0000" # local time by default |
103 | 114 | if string_date[-5] in '-+': |
104 | | - offset = _verify_utctz(string_date[-5:]) |
| 115 | + offset = verify_utctz(string_date[-5:]) |
105 | 116 | string_date = string_date[:-6] # skip space as well |
106 | 117 | # END split timezone info |
107 | 118 |
|
@@ -139,7 +150,7 @@ def parse_date(string_date): |
139 | 150 | fstruct = time.struct_time((dtstruct.tm_year, dtstruct.tm_mon, dtstruct.tm_mday, |
140 | 151 | tstruct.tm_hour, tstruct.tm_min, tstruct.tm_sec, |
141 | 152 | dtstruct.tm_wday, dtstruct.tm_yday, tstruct.tm_isdst)) |
142 | | - return int(time.mktime(fstruct)), _utc_tz_to_altz(offset) |
| 153 | + return int(time.mktime(fstruct)), utctz_to_altz(offset) |
143 | 154 | except ValueError: |
144 | 155 | continue |
145 | 156 | # END exception handling |
@@ -167,7 +178,7 @@ def parse_actor_and_date(line): |
167 | 178 | """ |
168 | 179 | m = _re_actor_epoch.search(line) |
169 | 180 | actor, epoch, offset = m.groups() |
170 | | - return (Actor._from_string(actor), int(epoch), _utc_tz_to_altz(offset)) |
| 181 | + return (Actor._from_string(actor), int(epoch), utctz_to_altz(offset)) |
171 | 182 |
|
172 | 183 |
|
173 | 184 |
|
|
0 commit comments