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

Side by Side Diff: third_party/APScheduler/build/lib.linux-x86_64-2.6/apscheduler/util.py

Issue 6673078: Initial checkin of media test matrix class, which will be used for media perf test later. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Put APScheduler in third_party directory for Media Performance test. Created 9 years, 9 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 """
2 This module contains several handy functions primarily meant for internal use.
3 """
4
5 from datetime import date, datetime, timedelta
6 from time import mktime
7
8 __all__ = ('asint', 'asbool', 'convert_to_datetime', 'timedelta_seconds',
9 'time_difference', 'datetime_ceil')
10
11
12 def asint(text):
13 """
14 Safely converts a string to an integer, returning None if the string
15 is None.
16
17 :type text: str
18 :rtype: int
19 """
20 if text is not None:
21 return int(text)
22
23
24 def asbool(obj):
25 """
26 Interprets an object as a boolean value.
27
28 :rtype: bool
29 """
30 if isinstance(obj, str):
31 obj = obj.strip().lower()
32 if obj in ('true', 'yes', 'on', 'y', 't', '1'):
33 return True
34 if obj in ('false', 'no', 'off', 'n', 'f', '0'):
35 return False
36 raise ValueError('Unable to interpret value "%s" as boolean' % obj)
37 return bool(obj)
38
39
40 def convert_to_datetime(dateval):
41 """
42 Converts a date object to a datetime object.
43 If an actual datetime object is passed, it is returned unmodified.
44
45 :type dateval: date
46 :rtype: datetime
47 """
48 if isinstance(dateval, datetime):
49 return dateval
50 elif isinstance(dateval, date):
51 return datetime.fromordinal(dateval.toordinal())
52 raise TypeError('Expected date, got %s instead' % type(dateval))
53
54
55 def timedelta_seconds(delta):
56 """
57 Converts the given timedelta to seconds.
58
59 :type delta: timedelta
60 :rtype: float
61 """
62 return delta.days * 24 * 60 * 60 + delta.seconds + \
63 delta.microseconds / 1000000.0
64
65
66 def time_difference(date1, date2):
67 """
68 Returns the time difference in seconds between the given two
69 datetime objects. The difference is calculated as: date1 - date2.
70
71 :param date1: the later datetime
72 :type date1: datetime
73 :param date2: the earlier datetime
74 :type date2: datetime
75 :rtype: float
76 """
77 later = mktime(date1.timetuple())
78 earlier = mktime(date2.timetuple())
79 return int(later - earlier)
80
81
82 def datetime_ceil(dateval):
83 """
84 Rounds the given datetime object upwards.
85
86 :type dateval: datetime
87 """
88 if dateval.microsecond > 0:
89 return dateval + timedelta(seconds=1,
90 microseconds=-dateval.microsecond)
91 return dateval
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698