| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6ef3775 commit 4a19917
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -135,6 +135,7 @@ def parse_date(string_date): | |||
| 135 | 135 | """ | |
| 136 | 136 | Parse the given date as one of the following | |
| 137 | 137 | ||
| 138 | + * aware datetime instance | ||
| 138 | 139 | * Git internal format: timestamp offset | |
| 139 | 140 | * RFC 2822: Thu, 07 Apr 2005 22:13:13 +0200. | |
| 140 | 141 | * ISO 8601 2005-04-07T22:13:13 | |
@@ -144,6 +145,10 @@ def parse_date(string_date): | |||
| 144 | 145 | :raise ValueError: If the format could not be understood | |
| 145 | 146 | :note: Date can also be YYYY.MM.DD, MM/DD/YYYY and DD.MM.YYYY. | |
| 146 | 147 | """ | |
| 148 | + if isinstance(string_date, datetime) and string_date.tzinfo: | ||
| 149 | + offset = -int(string_date.utcoffset().total_seconds()) | ||
| 150 | + return int(string_date.astimezone(utc).timestamp()), offset | ||
| 151 | + | ||
| 147 | 152 | # git time | |
| 148 | 153 | try: | |
| 149 | 154 | if string_date.count(' ') == 1 and string_date.rfind(':') == -1: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -174,6 +174,10 @@ def test_user_id(self): | |||
| 174 | 174 | self.assertIn('@', get_user_id()) | |
| 175 | 175 | ||
| 176 | 176 | def test_parse_date(self): | |
| 177 | + # parse_date(from_timestamp()) must return the tuple unchanged | ||
| 178 | + for timestamp, offset in (1522827734, -7200), (1522827734, 0), (1522827734, +3600): | ||
| 179 | + self.assertEqual(parse_date(from_timestamp(timestamp, offset)), (timestamp, offset)) | ||
| 180 | + | ||
| 177 | 181 | # test all supported formats | |
| 178 | 182 | def assert_rval(rval, veri_time, offset=0): | |
| 179 | 183 | self.assertEqual(len(rval), 2) | |
@@ -200,6 +204,7 @@ def assert_rval(rval, veri_time, offset=0): | |||
| 200 | 204 | # END for each date type | |
| 201 | 205 | ||
| 202 | 206 | # and failure | |
| 207 | + self.assertRaises(ValueError, parse_date, datetime.now()) # non-aware datetime | ||
| 203 | 208 | self.assertRaises(ValueError, parse_date, 'invalid format') | |
| 204 | 209 | self.assertRaises(ValueError, parse_date, '123456789 -02000') | |
| 205 | 210 | self.assertRaises(ValueError, parse_date, ' 123456789 -0200') | |
| Back | FazBrowse Home | New Git URL |
0 commit comments