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

Unified Diff: appengine/findit/common/time_util.py

Issue 2376573004: [Findit] For automatic analyses of flaky tests, run the Swarming tasks off PST peak hours. (Closed)
Patch Set: Fix nit. Created 4 years, 2 months 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/common/test/time_util_test.py ('k') | appengine/findit/handlers/flake/check_flake.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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.
+
+ 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))
« no previous file with comments | « appengine/findit/common/test/time_util_test.py ('k') | appengine/findit/handlers/flake/check_flake.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698