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

Side by Side 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: 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 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
11
10 12
11 def GetUTCNow(): # pragma: no cover. 13 def GetUTCNow(): # pragma: no cover.
12 """Returns the datetime.utcnow. This is to mock for testing.""" 14 """Returns the datetime.utcnow. This is to mock for testing."""
13 return datetime.utcnow() 15 return datetime.utcnow()
14 16
15 17
18 def GetUTCNowWithTimezone(): # pragma: no cover.
19 """Returns datetime.now but in utc timezone. This is to mock for testing."""
20 return datetime.now(pytz.utc)
21
22
16 def GetUTCNowTimestamp(): # pragma: no cover. 23 def GetUTCNowTimestamp(): # pragma: no cover.
17 """Returns the timestamp for datetime.utcnow. This is to mock for testing.""" 24 """Returns the timestamp for datetime.utcnow. This is to mock for testing."""
18 return calendar.timegm(GetUTCNow().timetuple()) 25 return calendar.timegm(GetUTCNow().timetuple())
19 26
20 27
21 def RemoveMicrosecondsFromDelta(delta): 28 def RemoveMicrosecondsFromDelta(delta):
22 """Returns a timedelta object without microseconds based on delta.""" 29 """Returns a timedelta object without microseconds based on delta."""
23 return delta - timedelta(microseconds=delta.microseconds) 30 return delta - timedelta(microseconds=delta.microseconds)
24 31
25 32
26 def FormatTimedelta(delta): 33 def FormatTimedelta(delta):
27 if not delta: 34 if not delta:
28 return None 35 return None
29 hours, remainder = divmod(delta.seconds, 3600) 36 hours, remainder = divmod(delta.seconds, 3600)
30 minutes, seconds = divmod(remainder, 60) 37 minutes, seconds = divmod(remainder, 60)
31 return '%02d:%02d:%02d' % (hours, minutes, seconds) 38 return '%02d:%02d:%02d' % (hours, minutes, seconds)
32 39
33 40
34 def FormatDatetime(date): 41 def FormatDatetime(date):
35 if not date: 42 if not date:
36 return None 43 return None
37 else: 44 else:
38 return date.strftime('%Y-%m-%d %H:%M:%S UTC') 45 return date.strftime('%Y-%m-%d %H:%M:%S UTC')
46
47
48 def GetDatetimeInTimezone(timezone_name, date_time=None):
49 """Returns the datetime.datetime of the given one in the specified timezone.
50
51 Args:
52 timezone_name (str): The name of any timezone supported by pytz.
53 date_time (datetime.datetime): The optional datetime to be converted into
54 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
55
56 Returns:
57 A datetime.datetime of the given one in the specified timezone.
58 """
59 date_time = date_time or GetUTCNowWithTimezone()
60 return date_time.astimezone(pytz.timezone(timezone_name))
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698