Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(317)

Side by Side Diff: git_dates.py

Issue 1559943003: Added git hyper-blame, a tool that skips unwanted commits in git blame. (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: Respond to review. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « git_common.py ('k') | git_hyper_blame.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # Copyright 2016 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 """Utility module for dealing with Git timestamps."""
6
7 import datetime
8
9
10 def timestamp_offset_to_datetime(timestamp, offset):
11 """Converts a timestamp + offset into a datetime.datetime.
12
13 Useful for dealing with the output of porcelain commands, which provide times
14 as timestamp and offset strings.
15
16 Args:
17 timestamp: An int UTC timestamp, or a string containing decimal digits.
18 offset: A str timezone offset. e.g., '-0800'.
19
20 Returns:
21 A tz-aware datetime.datetime for this timestamp.
22 """
23 timestamp = int(timestamp)
24 tz = FixedOffsetTZ.from_offset_string(offset)
25 return datetime.datetime.fromtimestamp(timestamp, tz)
26
27
28 def datetime_string(dt):
29 """Converts a tz-aware datetime.datetime into a string in git format."""
30 return dt.strftime('%Y-%m-%d %H:%M:%S %z')
31
32
33 # Adapted from: https://docs.python.org/2/library/datetime.html#tzinfo-objects
34 class FixedOffsetTZ(datetime.tzinfo):
35 def __init__(self, offset, name):
36 datetime.tzinfo.__init__(self)
37 self.__offset = offset
38 self.__name = name
39
40 def __repr__(self): # pragma: no cover
41 return '{}({!r}, {!r})'.format(type(self).__name__, self.__offset,
42 self.__name)
43
44 @classmethod
45 def from_offset_string(cls, offset):
46 try:
47 hours = int(offset[:-2])
48 minutes = int(offset[-2:])
49 except ValueError:
50 return cls(datetime.timedelta(0), 'UTC')
51
52 delta = datetime.timedelta(hours=hours, minutes=minutes)
53 return cls(delta, offset)
54
55 def utcoffset(self, dt):
56 return self.__offset
57
58 def tzname(self, dt):
59 return self.__name
60
61 def dst(self, dt):
62 return datetime.timedelta(0)
OLDNEW
« no previous file with comments | « git_common.py ('k') | git_hyper_blame.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698