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

Source Code for Module Products.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 or (at your 
  8  # option) any later version as published by 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 LocalDateTimeFromMilli(milliseconds):
37 """ 38 @param milliseconds:: UTC timestamp in milliseconds 39 """ 40 return LocalDateTime(milliseconds / 1000)
41 42
43 -def isoDateTime(gmtSecondsSince1970 = None):
44 value = _maybenow(gmtSecondsSince1970) 45 return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(value))
46
47 -def isoDateTimeFromMilli(milliseconds):
48 """ 49 @param milliseconds:: UTC timestamp in milliseconds 50 """ 51 return isoDateTime(milliseconds / 1000)
52
53 -def LocalDateTimeSecsResolution(gmtSecondsSince1970 = None):
54 value = _maybenow(gmtSecondsSince1970) 55 return time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(value))
56
57 -def USDate(gmtSecondsSince1970 = None):
58 value = _maybenow(gmtSecondsSince1970) 59 return time.strftime("%m/%d/%Y", time.localtime(value))
60
61 -def ParseUSDate(mdy):
62 return time.mktime(time.strptime(mdy, "%m/%d/%Y"))
63
64 -def YYYYMMDDHHMMS(gmtSecondsSince1970 = None):
65 value = _maybenow(gmtSecondsSince1970) 66 return time.strftime("%Y%m%d%H%M%S", time.localtime(value))
67
68 -def HHMMSS(gmtSecondsSince1970 = None):
69 value = _maybenow(gmtSecondsSince1970) 70 return time.strftime("%H:%M:%S", time.localtime(value))
71
72 -def SaveMessage():
73 return "Saved at time: " + HHMMSS()
74
75 -def Duration(seconds):
76 result = ':%02d' % (seconds % 60) 77 seconds /= 60 78 if seconds: 79 result = '%02d%s' % (seconds % 60, result) 80 seconds /= 60 81 if seconds: 82 result = '%02d:%s' % (seconds % 24, result) 83 seconds /= 24 84 if seconds: 85 result = '%d days %s' % (seconds, result) 86 return result
87 88
89 -def getBeginningOfDay(gmtSecondsSince1970=None):
90 value = _maybenow(gmtSecondsSince1970) 91 return time.mktime(time.localtime(value)[:3] + (0,0,0,0,0,-1))
92 93
94 -def getEndOfDay(gmtSecondsSince1970=None):
95 value = _maybenow(gmtSecondsSince1970) 96 return time.mktime(time.localtime(value)[:3] + (23,59,59,0,0,-1))
97
98 -def isoToTimestamp(value):
99 """ 100 converts a iso time string that does not contain a timezone, ie. 101 YYYY-MM-DD HH:MM:SS, to a timestamp in seconds since 1970; uses the system 102 timezone 103 """ 104 timeStr = value.replace('T', ' ') 105 timeTuple = time.strptime(timeStr, '%Y-%m-%d %H:%M:%S') 106 timestamp = time.mktime(timeTuple) 107 return timestamp
108