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

Side by Side Diff: appengine/findit/lib/time_util.py

Issue 2538373003: [Culprit-Finder] Merge lib/ to libs/. (Closed)
Patch Set: . Created 4 years 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
(Empty)
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
3 # found in the LICENSE file.
4
5 import calendar
6 from datetime import datetime
7 from datetime import time
8 from datetime import timedelta
9
10 import pytz
11
12
13 def GetUTCNow(): # pragma: no cover.
14 """Returns the datetime.utcnow. This is to mock for testing."""
15 return datetime.utcnow()
16
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
23 def GetUTCNowTimestamp(): # pragma: no cover.
24 """Returns the timestamp for datetime.utcnow. This is to mock for testing."""
25 return calendar.timegm(GetUTCNow().timetuple())
26
27
28 def RemoveMicrosecondsFromDelta(delta):
29 """Returns a timedelta object without microseconds based on delta."""
30 return delta - timedelta(microseconds=delta.microseconds)
31
32
33 def FormatTimedelta(delta):
34 """Returns a string representing the given time delta."""
35 if not delta:
36 return None
37 hours, remainder = divmod(delta.total_seconds(), 3600)
38 minutes, seconds = divmod(remainder, 60)
39 return '%02d:%02d:%02d' % (hours, minutes, seconds)
40
41
42 def FormatDatetime(date):
43 """Returns a string representing the given UTC datetime."""
44 if not date:
45 return None
46 else:
47 return date.strftime('%Y-%m-%d %H:%M:%S UTC')
48
49
50 def FormatDuration(datetime_start, datetime_end):
51 """Returns a string representing the given time duration or None."""
52 if not datetime_start or not datetime_end:
53 return None
54 return FormatTimedelta(datetime_end - datetime_start)
55
56
57 def GetDatetimeInTimezone(timezone_name, date_time):
58 """Returns the datetime.datetime of the given one in the specified timezone.
59
60 Args:
61 timezone_name (str): The name of any timezone supported by pytz.
62 date_time (datetime.datetime): The optional datetime to be converted into
63 the new timezone.
64
65 Returns:
66 A datetime.datetime of the given one in the specified timezone.
67 """
68 return date_time.astimezone(pytz.timezone(timezone_name))
69
70
71 class TimeZoneInfo(object):
72 """Gets time zone info from string like: +0800.
73
74 The string is HHMM offset relative to UTC timezone."""
75
76 def __init__(self, offset_str):
77 self._utcoffset = self.GetOffsetFromStr(offset_str)
78
79 def GetOffsetFromStr(self, offset_str):
80 offset = int(offset_str[-4:-2]) * 60 + int(offset_str[-2:])
81 if offset_str[0] == '-':
82 offset = -offset
83 return timedelta(minutes=offset)
84
85 def LocalToUTC(self, naive_time):
86 """Localizes naive datetime and converts it to utc naive datetime.
87
88 Args:
89 naive_time(datetime): naive time in local time zone, for example '+0800'.
90
91 Return:
92 A naive datetime object in utc time zone.
93 For example:
94 For TimeZoneInfo('+0800'), and naive local time is
95 datetime(2016, 9, 1, 10, 0, 0), the returned result should be
96 datetime(2016, 9, 1, 2, 0, 0) in utc time.
97 """
98 return naive_time - self._utcoffset
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698