source: people/peter.buschman/backup_monitoring/datetime/_datetime.py@ 1044

Last change on this file since 1044 was 976, checked in by peter, on Dec 6, 2011 at 10:19:33 AM

Raw checkin of current NetBackup / TSM parsing code.

File size: 2.7 KB
RevLine 
[976]1###
2### Temporal Functions
3###
4
5import time
6import datetime
7import calendar
8import pytz
9
10from ..math import micropad
11
12__all__ = [
13 'timedelta_to_seconds',
14 'mindatetime',
15 'maxdatetime',
16 'hh_mm_ss',
17 'dd_hh_mm',
18 'dd_hh_mm_ss',
19 'timeslot',
20 'backup_day',
21 'iso',
22]
23
24##
25## TimeError
26##
27class TimeError(StandardError):
28 pass
29
30
31##
32## Return the duration of a timedelta object in seconds as a float.
33##
34def timedelta_to_seconds(timedelta):
35 return float('%s.%s' % ((timedelta.days * 86400 ) + (timedelta.seconds), micropad(timedelta.microseconds)))
36
37
38##
39## Minimum date + 1 to allow for timezones
40##
41def mindatetime(tz=pytz.timezone('UTC')):
42 return datetime.datetime(datetime.MINYEAR, 1, 2, tzinfo=tz)
43
44
45##
46## Maximum date + 1 to allow for timezones
47##
48def maxdatetime(tz=pytz.timezone('UTC')):
49 return datetime.datetime(datetime.MAXYEAR, 12, 30, 23, 59, 59, 999999, tzinfo=tz)
50
51
52##
53## Convert seconds to HH:MM:SS
54##
55def hh_mm_ss(seconds):
56 minutes, seconds = divmod(seconds, 60)
57 hours, minutes = divmod(minutes, 60)
58 return '%02d:%02d:%02d' % (hours, minutes, seconds)
59
60
61##
62## Convert seconds to DD:HH:MM
63##
64def dd_hh_mm(seconds):
65 minutes, seconds = divmod(seconds, 60)
66 hours, minutes = divmod(minutes, 60)
67 days, hours = divmod(hours, 24)
68 return '%02d:%02d:%02d' % (days, hours, minutes)
69
70
71##
72## Convert seconds to DD HH:MM:SS
73##
74def dd_hh_mm_ss(seconds):
75 minutes, seconds = divmod(seconds, 60)
76 hours, minutes = divmod(minutes, 60)
77 days, hours = divmod(hours, 24)
78 return '%02d %02d:%02d:%02d' % (days, hours, minutes, seconds)
79
80
81##
82## Round timestamp to timeslot according to step value
83##
84def timeslot(t, step=60):
85
86 if 86400 % step != 0:
87 raise TimeError, 'step %d is not evenly divisible into 86400 seconds (1 day)!' % (step)
88 elif type(t) is datetime.datetime:
89 seconds = (t.minute * 60) + t.second
90 seconds = seconds - seconds % step
91 minute, second = divmod(seconds, 60)
92 return t.replace(minute=minute, second=second, microsecond=0)
93 elif type(t) is int or type(t) is long:
94 return t - t % step
95 elif type(t) is float:
96 t = long(t)
97 return t - t % step
98 else:
99 return None
100
101
102##
103## Return backup day from datetime
104##
105def backup_day(t, offset=8):
106
107 if t.hour < offset:
108 t = t - datetime.timedelta(days=1)
109
110 return t.replace(hour=0, minute=0, second=0, microsecond=0)
111
112
113##
114## Return timestamp as ISO formatted string
115##
116def iso(t,nosep=False):
117
118 if nosep is True:
119 return t.strftime('%Y%m%d%H%M%S')
120 else:
121 return t.strftime('%Y-%m-%d %H:%M:%S')
122
123
Note: See TracBrowser for help on using the repository browser.