1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """Module for various timing purposes."""
24
25
26 from time import asctime
27
28
30 """Print the elapsed time value in day, hour, minute, and second format.
31
32 @param time_delta: The time value in seconds to format.
33 @type time_delta: float
34 @keyword return_str: A flag which if True will cause the elapsed time string to be returned rather than printed.
35 @type return_str: bool
36 @return: The elapsed time string, if asked for.
37 @rtype: None or str
38 """
39
40
41 sec = 0.0
42 min = 0
43 hour = 0
44 day = 0
45
46
47 min, sec = divmod(time_delta, 60)
48 if min > 0:
49 hour, min = divmod(min, 60)
50 else:
51 min = 0
52 if hour > 0:
53 day, hour = divmod(hour, 24)
54 else:
55 hour = 0
56
57
58 plural_min = 's'
59 plural_hour = 's'
60 plural_day = 's'
61 if min == 1.0:
62 plural_min = ''
63 if hour == 1.0:
64 plural_hour = ''
65 if day == 1.0:
66 plural_day = ''
67
68
69 if min == 0.0 and hour == 0.0 and day == 0.0:
70 string = "Elapsed time: %.3f seconds\n" % sec
71 elif hour == 0.0 and day == 0.0:
72 string = "Elapsed time: %i minute%s and %.3f seconds\n" % (min, plural_min, sec)
73 elif day == 0.0:
74 string = "Elapsed time: %i hour%s, %i minute%s and %.3f seconds\n" % (hour, plural_hour, min, plural_min, sec)
75 else:
76 string = "Elapsed time: %i day%s, %i hour%s, %i minute%s and %.3f seconds\n" % (day, plural_day, hour, plural_hour, min, plural_min, sec)
77
78
79 if not return_str:
80 print(string)
81
82
83 else:
84 return string
85
86
88 """Print the current date and time."""
89
90
91 print("%s\n" % asctime())
92