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