Package ZenUtils :: Module Time
[hide private]
[frames] | no frames]

Source Code for Module ZenUtils.Time

 1  ########################################################################### 
 2  # 
 3  # This program is part of Zenoss Core, an open source monitoring platform. 
 4  # Copyright (C) 2007, Zenoss Inc. 
 5  # 
 6  # This program is free software; you can redistribute it and/or modify it 
 7  # under the terms of the GNU General Public License version 2 as published by 
 8  # the Free Software Foundation. 
 9  # 
10  # For complete information please visit: http://www.zenoss.com/oss/ 
11  # 
12  ########################################################################### 
13   
14  """Time 
15   
16  Utilities for consistent manipulation of Dates and Time.  All simple 
17  code should migrate here, and without dependencies on anything other 
18  than standard python libraries. 
19   
20  $Id:$""" 
21   
22  __version__ = "$$"[11:-2] 
23   
24  import time 
25   
26 -def _maybenow(gmtSecondsSince1970):
27 if gmtSecondsSince1970 is None: 28 return time.time() 29 return int(gmtSecondsSince1970)
30
31 -def LocalDateTime(gmtSecondsSince1970 = None):
32 value = _maybenow(gmtSecondsSince1970) 33 secs = value % 60 34 return time.strftime("%Y/%m/%d %H:%M:%%06.3f", time.localtime(value)) % secs
35
36 -def LocalDateTimeSecsResolution(gmtSecondsSince1970 = None):
37 value = _maybenow(gmtSecondsSince1970) 38 return time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(value))
39
40 -def USDate(gmtSecondsSince1970 = None):
41 value = _maybenow(gmtSecondsSince1970) 42 return time.strftime("%m/%d/%Y", time.localtime(value))
43
44 -def ParseUSDate(mdy):
45 return time.mktime(time.strptime(mdy, "%m/%d/%Y"))
46
47 -def YYYYMMDDHHMMS(gmtSecondsSince1970 = None):
48 value = _maybenow(gmtSecondsSince1970) 49 return time.strftime("%Y%m%d%H%M%S", time.localtime(value))
50
51 -def HHMMSS(gmtSecondsSince1970 = None):
52 value = _maybenow(gmtSecondsSince1970) 53 return time.strftime("%H:%M:%S", time.localtime(value))
54
55 -def SaveMessage():
56 return "Saved at time: " + HHMMSS()
57
58 -def Duration(seconds):
59 result = ':%02d' % (seconds % 60) 60 seconds /= 60 61 if seconds: 62 result = '%02d%s' % (seconds % 60, result) 63 seconds /= 60 64 if seconds: 65 result = '%02d:%s' % (seconds % 24, result) 66 seconds /= 24 67 if seconds: 68 result = '%d days %s' % (seconds, result) 69 return result
70