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

Side by Side Diff: appengine/findit/lib/time_util.py

Issue 2480593002: [Predator] Move time_util from common/ to lib/, split code review related part to code_review_util (Closed)
Patch Set: Rebase and fix nits. Created 4 years, 1 month 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
OLDNEW
1 # Copyright 2016 The Chromium Authors. All rights reserved. 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 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import calendar 5 import calendar
6 from datetime import datetime 6 from datetime import datetime
7 from datetime import time 7 from datetime import time
8 from datetime import timedelta 8 from datetime import timedelta
9 9
10 import pytz 10 import pytz
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 59
60 Args: 60 Args:
61 timezone_name (str): The name of any timezone supported by pytz. 61 timezone_name (str): The name of any timezone supported by pytz.
62 date_time (datetime.datetime): The optional datetime to be converted into 62 date_time (datetime.datetime): The optional datetime to be converted into
63 the new timezone. 63 the new timezone.
64 64
65 Returns: 65 Returns:
66 A datetime.datetime of the given one in the specified timezone. 66 A datetime.datetime of the given one in the specified timezone.
67 """ 67 """
68 return date_time.astimezone(pytz.timezone(timezone_name)) 68 return date_time.astimezone(pytz.timezone(timezone_name))
69
70
71 class TimeZoneInfo(object):
72 """Gets time zone info from string like: +0800.
73
74 The string is HHMM offset relative to UTC timezone."""
75
76 def __init__(self, offset_str):
77 self._utcoffset = self.GetOffsetFromStr(offset_str)
78
79 def GetOffsetFromStr(self, offset_str):
80 offset = int(offset_str[-4:-2]) * 60 + int(offset_str[-2:])
81 if offset_str[0] == '-':
82 offset = -offset
83 return timedelta(minutes=offset)
84
85 def LocalToUTC(self, naive_time):
86 """Localizes naive datetime and converts it to utc naive datetime.
87
88 Args:
89 naive_time(datetime): naive time in local time zone, for example '+0800'.
90
91 Return:
92 A naive datetime object in utc time zone.
93 For example:
94 For TimeZoneInfo('+0800'), and naive local time is
95 datetime(2016, 9, 1, 10, 0, 0), the returned result should be
96 datetime(2016, 9, 1, 2, 0, 0) in utc time.
97 """
98 return naive_time - self._utcoffset
OLDNEW
« no previous file with comments | « appengine/findit/lib/test/time_util_test.py ('k') | appengine/findit/model/base_suspected_cl.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698