blob: 41cf3acf983d58408f26b2f8e9bd61cba01f4a82 [file] [log] [blame]
[email protected]81937562016-02-03 08:00:531# 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
7import datetime
8
9
10def 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
28def 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
34class 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)