Chromium Code Reviews| Index: appengine/findit/common/time_util.py |
| diff --git a/appengine/findit/common/time_util.py b/appengine/findit/common/time_util.py |
| index 4ef6e8b8a41a167b34bb2a3d5fb090fadf27bf2a..7aace1bc3eb81f41e134254f415f86af41c4fedb 100644 |
| --- a/appengine/findit/common/time_util.py |
| +++ b/appengine/findit/common/time_util.py |
| @@ -66,3 +66,38 @@ def GetDatetimeInTimezone(timezone_name, date_time): |
| A datetime.datetime of the given one in the specified timezone. |
| """ |
| return date_time.astimezone(pytz.timezone(timezone_name)) |
| + |
| + |
| +class TimeZoneInfo(object): |
|
stgao
2016/11/02 02:04:20
We already have the code to handle this in https:/
Sharu Jiang
2016/11/03 20:59:48
Sure, I can refactor that code to use this TimeZon
|
| + """Gets time zone info from string like: +0800. |
| + |
| + The string is HHMM offset relative to UTC timezone.""" |
| + |
| + def __init__(self, offset_str): |
| + super(TimeZoneInfo, self).__init__() |
| + self._utcoffset = self.GetOffsetFromStr(offset_str) |
| + |
| + def GetOffsetFromStr(self, offset_str): |
| + offset = int(offset_str[-4:-2]) * 60 + int(offset_str[-2:]) |
| + if offset_str[0] == '-': |
| + offset = -offset |
| + return timedelta(minutes=offset) |
| + |
| + @property |
| + def utcoffset(self): |
| + return self._utcoffset |
| + |
| + def LocalToUTC(self, naive_time): |
| + """Localizes naive datetime and converts it to utc naive datetime. |
| + |
| + Args: |
| + naive_time(datetime): naive time in local time zone, for example '+0800'. |
| + |
| + Return: |
| + A naive datetime object in utc time zone. |
| + For example: |
| + For TimeZoneInfo('+0800'), and naive local time is |
| + datetime(2016, 9, 1, 10, 0, 0), the returned result should be |
| + datetime(2016, 9, 1, 18, 0, 0) in utc time. |
| + """ |
| + return naive_time - self.utcoffset |