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

Unified 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 side-by-side diff with in-line comments
Download patch
« 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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: appengine/findit/lib/time_util.py
diff --git a/appengine/findit/common/time_util.py b/appengine/findit/lib/time_util.py
similarity index 69%
rename from appengine/findit/common/time_util.py
rename to appengine/findit/lib/time_util.py
index 4ef6e8b8a41a167b34bb2a3d5fb090fadf27bf2a..243a3ec126f5c3d3c5b3def18424f1d11eb7ee32 100644
--- a/appengine/findit/common/time_util.py
+++ b/appengine/findit/lib/time_util.py
@@ -66,3 +66,33 @@ 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):
+ """Gets time zone info from string like: +0800.
+
+ The string is HHMM offset relative to UTC timezone."""
+
+ def __init__(self, offset_str):
+ 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)
+
+ 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, 2, 0, 0) in utc time.
+ """
+ return naive_time - self._utcoffset
« 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