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

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: 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
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 66%
rename from appengine/findit/common/time_util.py
rename to appengine/findit/lib/time_util.py
index 4ef6e8b8a41a167b34bb2a3d5fb090fadf27bf2a..7aace1bc3eb81f41e134254f415f86af41c4fedb 100644
--- a/appengine/findit/common/time_util.py
+++ b/appengine/findit/lib/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/03 22:14:05 Can we make this a simple function like the other
wrengr 2016/11/03 22:17:58 Why?
stgao 2016/11/03 23:04:52 The current version of it looks simpler to me: htt
Sharu Jiang 2016/11/04 01:02:34 Actually I prefer this one, because it's more expa
wrengr 2016/11/04 20:24:26 That's the direction I was thinking, whence my que
+ """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.
stgao 2016/11/03 22:14:05 Is this true? Shouldn't it be datetime(2016, 9, 1,
Sharu Jiang 2016/11/04 01:02:34 Oops, updated.
+ """
+ return naive_time - self.utcoffset

Powered by Google App Engine
This is Rietveld 408576698