ROS Code
 All Classes Namespaces Files Functions Variables
time.py
Go to the documentation of this file.
1 """
2 
3 All commands based around time functions
4 
5 """
6 import datetime
7 import time
8 import calendar
9 
10 
11 def dayofweek(day, month, year, formatresult=True):
12  """
13 
14  Get the day of the week for a specific day
15 
16  day:
17  The day to include in the search
18 
19  month:
20  The month to include in the search
21 
22  year:
23  The year to include in the search
24 
25  formatresult:
26  Whether or not to format the result.
27  A formatted date would look like: "Monday".
28  A non formatted date would look like: 1.
29  Default is True.
30 
31  """
32  if formatresult is False:
33  return calendar.weekday(year, month, day) + 1
34  else:
35  days = {
36  0: 'Monday',
37  1: "Tuesday",
38  2: "Wednesday",
39  3: "Thursday",
40  4: "Friday",
41  5: "Saturday",
42  6: "Sunday"
43  }
44  return days[calendar.weekday(year, month, day)]
45 
46 
47 def leapyear(year):
48  """
49 
50  Check if a year in particular is a leap year
51 
52  year:
53  The year to check for
54 
55  """
56  return bool(calendar.isleap(year))
57 
58 
59 def closeyear(year):
60  """
61 
62  Find how many years away was the closest leap year to a specific year
63 
64  """
65  return int(year % 4)
66 
67 
68 def converttime(time, currentformat, newformat):
69  """
70 
71  Convert a specific time format to another type
72 
73  time:
74  The time to convert
75 
76  currentformat:
77  The current format of the time.
78  Can be seconds, milliseconds, minutes, hours, days, weeks, fortnights, years, decades, centuaries or millenniums.
79 
80  newformat:
81  The new format of the time.
82  Can be seconds, milliseconds, minutes, hours, days, weeks, fortnights, years, decades, centuaries or millenniums.
83 
84  """
85  currentformat = currentformat.lower()
86  newformat = newformat.lower()
87  if currentformat == newformat:
88  return time
89  if currentformat == 'seconds':
90  if newformat == 'milliseconds':
91  return time * 1000
92  elif newformat == 'minutes':
93  return time / 60
94  elif newformat == 'hours':
95  return time / 60 / 60
96  elif newformat == 'days':
97  return time / 60 / 60 / 24
98  elif newformat == 'weeks':
99  return time / 60 / 60 / 24 / 7
100  elif newformat == 'fortnights':
101  return time / 60 / 60 / 24 / 14
102  elif newformat == 'years':
103  return time / 60 / 60 / 24 / 365
104  elif newformat == 'decades':
105  return time / 60 / 60 / 24 / 365 / 10
106  elif newformat == 'centuaries':
107  return time / 60 / 60 / 24 / 365 / 100
108  elif newformat == 'millenniums':
109  return time / 60 / 60 / 24 / 365 / 1000
110  elif currentformat == 'minutes':
111  if newformat == 'milliseconds':
112  return time * 60 * 1000
113  elif newformat == 'seconds':
114  return time * 60
115  elif newformat == 'hours':
116  return time / 60
117  elif newformat == 'days':
118  return time / 60 / 24
119  elif newformat == 'weeks':
120  return time / 60 / 24 / 7
121  elif newformat == 'fortnights':
122  return time / 60 / 24 / 14
123  elif newformat == 'years':
124  return time / 60 / 24 / 365
125  elif newformat == 'decades':
126  return time / 60 / 24 / 365 / 10
127  elif newformat == 'centuaries':
128  return time / 60 / 24 / 365 / 100
129  elif newformat == 'millenniums':
130  return time / 60 / 24 / 365 / 1000
131  elif currentformat == 'hours':
132  if newformat == 'milliseconds':
133  return time * 60 * 60 * 1000
134  elif newformat == 'seconds':
135  return time * 60 * 60
136  elif newformat == 'minutes':
137  return time / 60
138  elif newformat == 'days':
139  return time / 24
140  elif newformat == 'weeks':
141  return time / 24 / 7
142  elif newformat == 'fortnights':
143  return time / 24 / 14
144  elif newformat == 'years':
145  return time / 24 / 7 / 365
146  elif newformat == 'decades':
147  return time / 24 / 365 / 10
148  elif newformat == 'centuaries':
149  return time / 24 / 365 / 100
150  elif newformat == 'millenniums':
151  return time / 24 / 365 / 1000
152  elif currentformat == 'days':
153  if newformat == 'milliseconds':
154  return time * 24 * 60 * 60 * 1000
155  elif newformat == 'seconds':
156  return time * 24 * 60 * 60
157  elif newformat == 'minutes':
158  return time * 24 * 60
159  elif newformat == 'hours':
160  return time * 24
161  elif newformat == 'weeks':
162  return time / 24 / 7
163  elif newformat == 'fortnights':
164  return time / 60 / 24 / 14
165  elif newformat == 'years':
166  return time / 60 / 24 / 7 / 365
167  elif newformat == 'decades':
168  return time / 7 / 365 / 10
169  elif newformat == 'centuaries':
170  return time / 7 / 365 / 100
171  elif newformat == 'millenniums':
172  return time / 7 / 365 / 1000
173  elif currentformat == 'weeks':
174  if newformat == 'milliseconds':
175  return time * 7 * 24 * 60 * 60 * 1000
176  elif newformat == 'seconds':
177  return time * 7 * 24 * 60 * 60
178  elif newformat == 'minutes':
179  return time * 7 * 24 * 60
180  elif newformat == 'hours':
181  return time * 7 * 24
182  elif newformat == 'fortnights':
183  return time * 7 / 14
184  elif newformat == 'years':
185  return time * 7 / 365
186  elif newformat == 'decades':
187  return time * 7 / 365 / 10
188  elif newformat == 'centuaries':
189  return time * 7 / 365 / 100
190  elif newformat == 'millenniums':
191  return time * 7 / 365 / 1000
192  elif currentformat == 'fortnights':
193  if newformat == 'milliseconds':
194  return time * 14 * 24 * 60 * 60 * 1000
195  elif newformat == 'seconds':
196  return time * 14 * 24 * 60 * 60
197  elif newformat == 'minutes':
198  return time * 14 * 24 * 60
199  elif newformat == 'hours':
200  return time * 14 * 24
201  elif newformat == 'weeks':
202  return time * 14
203  elif newformat == 'years':
204  return time * 14 / 365
205  elif newformat == 'decades':
206  return time * 14 / 365 / 10
207  elif newformat == 'centuaries':
208  return time * 14 / 365 / 100
209  elif newformat == 'millenniums':
210  return time * 14 / 365 / 1000
211  elif currentformat == 'years':
212  if newformat == 'milliseconds':
213  return time * 365 * 24 * 60 * 60 * 1000
214  elif newformat == 'seconds':
215  return time * 365 * 24 * 60 * 60
216  elif newformat == 'minutes':
217  return time * 365 * 24 * 60
218  elif newformat == 'hours':
219  return time * 365 * 24
220  elif newformat == 'days':
221  return time * 365
222  elif newformat == 'weeks':
223  return time * 365 / 7
224  elif newformat == 'fortnights':
225  return time * 365 / 14
226  elif newformat == 'decades':
227  return time / 10
228  elif newformat == 'centuaries':
229  return time / 100
230  elif newformat == 'millenniums':
231  return time / 1000
232  elif currentformat == 'decades':
233  if newformat == 'milliseconds':
234  return time * 10 * 365 * 24 * 60 * 60 * 1000
235  elif newformat == 'seconds':
236  return time * 10 * 365 * 24 * 60 * 60
237  elif newformat == 'minutes':
238  return time * 10 * 365 * 24 * 60
239  elif newformat == 'hours':
240  return time * 10 * 365 * 24
241  elif newformat == 'days':
242  return time * 10 * 365
243  elif newformat == 'weeks':
244  return time * 10 * 365 / 7
245  elif newformat == 'fortnights':
246  return time * 10 * 365 / 14
247  elif newformat == 'years':
248  return time * 10
249  elif newformat == 'centuaries':
250  return time * 10 / 100
251  elif newformat == 'millenniums':
252  return time * 10 / 1000
253  elif currentformat == 'centuaries':
254  if newformat == 'milliseconds':
255  return time * 100 * 365 * 24 * 60 * 60 * 1000
256  elif newformat == 'seconds':
257  return time * 100 * 365 * 24 * 60 * 60
258  elif newformat == 'minutes':
259  return time * 100 * 365 * 24 * 60
260  elif newformat == 'hours':
261  return time * 100 * 365 * 24
262  elif newformat == 'days':
263  return time * 100 * 365
264  elif newformat == 'weeks':
265  return time * 100 * 365 / 7
266  elif newformat == 'fortnights':
267  return time * 100 * 365 / 14
268  elif newformat == 'years':
269  return time * 100
270  elif newformat == 'decades':
271  return time * 100 / 10
272  elif newformat == 'millenniums':
273  return time * 100 / 1000
274  elif currentformat == 'millenniums':
275  if newformat == 'milliseconds':
276  return time * 1000 * 365 * 24 * 60 * 60 * 1000
277  elif newformat == 'seconds':
278  return time * 1000 * 365 * 24 * 60 * 60
279  elif newformat == 'minutes':
280  return time * 1000 * 365 * 24 * 60
281  elif newformat == 'hours':
282  return time * 1000 * 365 * 24
283  elif newformat == 'days':
284  return time * 1000 * 365
285  elif newformat == 'weeks':
286  return time * 1000 * 365 / 7
287  elif newformat == 'fortnights':
288  return time * 1000 * 365 / 14
289  elif newformat == 'years':
290  return time * 1000
291  elif newformat == 'decades':
292  return time * 1000 / 10
293  elif newformat == 'centuaries':
294  return time * 1000 / 100
295 
296 
297 def minyear():
298  """
299 
300  Get the minimum year allowed by the current OS.
301 
302  """
303  return datetime.MINYEAR
304 
305 
306 def maxyear():
307  """
308 
309  Get the maxiumum year allowed by the current OS.
310 
311  """
312  return datetime.MAXYEAR
313 
314 
315 def timezone():
316  """
317 
318  Get the current timezone code.
319 
320  """
321  return time.timezone
322 
323 
324 def timesince():
325  """
326 
327  Get the amount of time since 00:00 on 1 January 1970, the raw date before formatting by computers.
328 
329  """
330  return time.time()
331 
332 
333 def getdatetime(timedateformat='complete'):
334  """
335 
336  Get the current date or time in a specific format
337 
338  timedateformat:
339  The type of date to query for.
340  Can be day, month, year, hour, minute, second, millisecond, yearmonthday, daymonthyear, hourminutesecond, secondminutehour, complete, datetime or timedate.
341  Default is "complete".
342 
343  """
344  timedateformat = timedateformat.lower()
345  if timedateformat == 'day':
346  return ((str(datetime.datetime.now())).split(' ')[0]).split('-')[2]
347  elif timedateformat == 'month':
348  return ((str(datetime.datetime.now())).split(' ')[0]).split('-')[1]
349  elif timedateformat == 'year':
350  return ((str(datetime.datetime.now())).split(' ')[0]).split('-')[0]
351  elif timedateformat == 'hour':
352  return (((str(datetime.datetime.now())).split(' ')[1]).split('.')[0]
353  ).split(':')[0]
354  elif timedateformat == 'minute':
355  return (((str(datetime.datetime.now())).split(' ')[1]).split('.')[0]
356  ).split(':')[1]
357  elif timedateformat == 'second':
358  return (((str(datetime.datetime.now())).split(' ')[1]).split('.')[0]
359  ).split(':')[2]
360  elif timedateformat == 'millisecond':
361  return (str(datetime.datetime.now())).split('.')[1]
362  elif timedateformat == 'yearmonthday':
363  return (str(datetime.datetime.now())).split(' ')[0]
364  elif timedateformat == 'daymonthyear':
365  return ((str(datetime.datetime.now())).split(' ')[0]).split(
366  '-')[2] + '-' + ((str(
367  datetime.datetime.now())).split(' ')[0]).split('-')[1] + '-' + (
368  (str(datetime.datetime.now())).split(' ')[0]).split('-')[0]
369  elif timedateformat == 'hourminutesecond':
370  return ((str(datetime.datetime.now())).split(' ')[1]).split('.')[0]
371  elif timedateformat == 'secondminutehour':
372  return (((str(datetime.datetime.now())).split(' ')[1]).split('.')[0]
373  ).split(':')[2] + ':' + (((str(datetime.datetime.now())).split(
374  ' ')[1]).split('.')[0]).split(':')[1] + ':' + (
375  ((str(datetime.datetime.now())).split(' ')[1]
376  ).split('.')[0]).split(':')[0]
377  elif timedateformat == 'complete':
378  return str(datetime.datetime.now())
379  elif timedateformat == 'datetime':
380  return (str(datetime.datetime.now())).split('.')[0]
381  elif timedateformat == 'timedate':
382  return ((str(
383  datetime.datetime.now())).split('.')[0]).split(' ')[1] + ' ' + (
384  (str(datetime.datetime.now())).split('.')[0]).split(' ')[0]
385 
386 
387 def timeit(command, round=True):
388  """
389 
390  Time how long a command takes to execute
391 
392  command:
393  The command to time.
394 
395  round:
396  Whether or not to round the number to an integer. Default is True.
397 
398  """
399  t1 = time.clock()
400  exec(command)
401  t2 = time.clock()
402  if round == True:
403  return int(t2 - t1)
404  return t2 - t1
def minyear
Definition: time.py:297
def closeyear
Definition: time.py:59
def timeit
Definition: time.py:387
def converttime
Definition: time.py:68
def leapyear
Definition: time.py:47
def dayofweek
Definition: time.py:11
def timezone
Definition: time.py:315
def getdatetime
Definition: time.py:333
def maxyear
Definition: time.py:306
def timesince
Definition: time.py:324