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

# Source Code for Module Products.ZenUtils.Time

      1  ##############################################################################
      2  #
      3  # Copyright (C) Zenoss, Inc. 2007, all rights reserved.
      4  #
      5  # This content is made available according to terms specified in
      6  # License.zenoss under the directory where your Zenoss product is installed.
      7  #
      8  ##############################################################################
      9
     10
     11  """Time
     12
     13  Utilities for consistent manipulation of Dates and Time.  All simple
     14  code should migrate here, and without dependencies on anything other
     15  than standard python libraries.
     16
     17  $Id:$"""
     18
     19  __version__ = "$$"[11:-2]
     20
     21  import time
     22
     23 -def _maybenow(gmtSecondsSince1970):
     24      if gmtSecondsSince1970 is None:
     25          return time.time()
     26      return int(gmtSecondsSince1970)
     27
     28 -def LocalDateTime(gmtSecondsSince1970 = None):
     29      value = _maybenow(gmtSecondsSince1970)
     30      secs = value % 60
     31      return time.strftime("%Y/%m/%d %H:%M:%%06.3f", time.localtime(value)) % secs
     32
     33 -def LocalDateTimeFromMilli(milliseconds):
     34      """
     35      @param milliseconds:: UTC timestamp in milliseconds
     36      """
     37      return LocalDateTime(milliseconds / 1000)
     38
     39
     40 -def isoDateTime(gmtSecondsSince1970 = None):
     41      value = _maybenow(gmtSecondsSince1970)
     42      return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(value))
     43
     44 -def isoDateTimeFromMilli(milliseconds):
     45      """
     46      @param milliseconds:: UTC timestamp in milliseconds
     47      """
     48      return isoDateTime(milliseconds / 1000)
     49
     50 -def LocalDateTimeSecsResolution(gmtSecondsSince1970 = None):
     51      value = _maybenow(gmtSecondsSince1970)
     52      return time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(value))
     53
     54 -def USDate(gmtSecondsSince1970 = None):
     55      value = _maybenow(gmtSecondsSince1970)
     56      return time.strftime("%m/%d/%Y", time.localtime(value))
     57
     58 -def ParseUSDate(mdy):
     59      return time.mktime(time.strptime(mdy, "%m/%d/%Y"))
     60
     61 -def YYYYMMDDHHMMS(gmtSecondsSince1970 = None):
     62      value = _maybenow(gmtSecondsSince1970)
     63      return time.strftime("%Y%m%d%H%M%S", time.localtime(value))
     64
     65 -def HHMMSS(gmtSecondsSince1970 = None):
     66      value = _maybenow(gmtSecondsSince1970)
     67      return time.strftime("%H:%M:%S", time.localtime(value))
     68
     69 -def SaveMessage():
     70      return "Saved at time: " + HHMMSS()
     71
     72 -def Duration(seconds):
     73      result = ':%02d' % (seconds % 60)
     74      seconds /= 60
     75      if seconds:
     76          result = '%02d%s' % (seconds % 60, result)
     77      seconds /= 60
     78      if seconds:
     79          result = '%02d:%s' % (seconds % 24, result)
     80      seconds /= 24
     81      if seconds:
     82          result = '%d days %s' % (seconds, result)
     83      return result
     84
     85
     86 -def getBeginningOfDay(gmtSecondsSince1970=None):
     87      value = _maybenow(gmtSecondsSince1970)
     88      return time.mktime(time.localtime(value)[:3] + (0,0,0,0,0,-1))
     89
     90
     91 -def getEndOfDay(gmtSecondsSince1970=None):
     92      value = _maybenow(gmtSecondsSince1970)
     93      return time.mktime(time.localtime(value)[:3] + (23,59,59,0,0,-1))
     94
     95 -def isoToTimestamp(value):
     96      """
     97      converts a iso time string that does not contain a timezone, ie.
     98      YYYY-MM-DD HH:MM:SS, to a timestamp in seconds since 1970; uses the system
     99      timezone
    100      """
    101      timeStr = value.replace('T', ' ')
    102      timeTuple = time.strptime(timeStr, '%Y-%m-%d %H:%M:%S')
    103      timestamp = time.mktime(timeTuple)
    104      return timestamp
    105