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 0d56e86b4eafdb9a0b457b12f7d4a11b478b77f1..407ed700f72e2dc11212c30d2be6190a907157bb 100644 |
| --- a/appengine/findit/common/time_util.py |
| +++ b/appengine/findit/common/time_util.py |
| @@ -7,12 +7,19 @@ from datetime import datetime |
| from datetime import time |
| from datetime import timedelta |
| +import pytz |
| + |
| def GetUTCNow(): # pragma: no cover. |
| """Returns the datetime.utcnow. This is to mock for testing.""" |
| return datetime.utcnow() |
| +def GetUTCNowWithTimezone(): # pragma: no cover. |
| + """Returns datetime.now but in utc timezone. This is to mock for testing.""" |
| + return datetime.now(pytz.utc) |
| + |
| + |
| def GetUTCNowTimestamp(): # pragma: no cover. |
| """Returns the timestamp for datetime.utcnow. This is to mock for testing.""" |
| return calendar.timegm(GetUTCNow().timetuple()) |
| @@ -36,3 +43,18 @@ def FormatDatetime(date): |
| return None |
| else: |
| return date.strftime('%Y-%m-%d %H:%M:%S UTC') |
| + |
| + |
| +def GetDatetimeInTimezone(timezone_name, date_time=None): |
| + """Returns the datetime.datetime of the given one in the specified timezone. |
| + |
| + Args: |
| + timezone_name (str): The name of any timezone supported by pytz. |
| + date_time (datetime.datetime): The optional datetime to be converted into |
| + the new timezone. If not given, default to UTC now. |
|
lijeffrey
2016/09/27 23:48:05
Is there a reason we don't just pass the default d
stgao
2016/10/01 05:55:25
No, we can't.
Default value is created and set onc
|
| + |
| + Returns: |
| + A datetime.datetime of the given one in the specified timezone. |
| + """ |
| + date_time = date_time or GetUTCNowWithTimezone() |
| + return date_time.astimezone(pytz.timezone(timezone_name)) |