Trees | Indices | Help |
---|
|
1 # -*- Mode: Python; test-case-name: flumotion.test.test_common_format -*- 2 # vi:si:et:sw=4:sts=4:ts=4 3 # 4 # Flumotion - a streaming media server 5 # Copyright (C) 2004,2005,2006,2007,2008 Fluendo, S.L. (www.fluendo.com). 6 # All rights reserved. 7 8 # This file may be distributed and/or modified under the terms of 9 # the GNU General Public License version 2 as published by 10 # the Free Software Foundation. 11 # This file is distributed without any warranty; without even the implied 12 # warranty of merchantability or fitness for a particular purpose. 13 # See "LICENSE.GPL" in the source distribution for more information. 14 15 # Licensees having purchased or holding a valid Flumotion Advanced 16 # Streaming Server license may use this file in accordance with the 17 # Flumotion Advanced Streaming Server Commercial License Agreement. 18 # See "LICENSE.Flumotion" in the source distribution for more information. 19 20 # Headers in this file shall remain intact. 21 22 """formatting functions for storage, time, etc 23 """ 24 25 import gettext 26 import locale 27 import sys 28 import time 29 30 from flumotion.common.i18n import N_ 31 from flumotion.configure import configure 32 33 _ = gettext.gettext 34 __version__ = "$Rev: 7162 $" 35 3638 """ 39 Nicely formats a storage size using SI units. 40 See Wikipedia and other sources for rationale. 41 Prefixes are k, M, G, ... 42 Sizes are powers of 10. 43 Actual result should be suffixed with bit or byte, not b or B. 44 45 @param units: the unit size to format 46 @type units: int or float 47 @param precision: the number of floating point digits to use 48 @type precision: int 49 50 @rtype: string 51 @returns: value of units, formatted using SI scale and the given precision 52 """ 53 54 # XXX: We might end up calling float(), which breaks 55 # when using LC_NUMERIC when it is not C -- only in python 56 # 2.3 though, no prob in 2.4. See PEP 331 57 if sys.version_info < (2, 4): 58 locale.setlocale(locale.LC_NUMERIC, "C") 59 60 prefixes = ['E', 'P', 'T', 'G', 'M', 'k', ''] 61 62 value = float(units) 63 prefix = prefixes.pop() 64 while prefixes and value >= 1000: 65 prefix = prefixes.pop() 66 value /= 1000 67 68 format = "%%.%df %%s" % precision 69 return format % (value, prefix)70 7173 """ 74 Nicely format time in a human-readable format. 75 Will chunks weeks, days, hours and minutes. 76 77 @param seconds: the time in seconds to format. 78 @type seconds: int or float 79 @param fractional: how many digits to show for the fractional part. 80 @type fractional: int 81 82 @rtype: string 83 @returns: a nicely formatted time string. 84 """ 85 chunks = [] 86 87 week = 60 * 60 * 24 * 7 88 weeks = seconds / week 89 seconds %= week 90 91 day = 60 * 60 * 24 92 days = seconds / day 93 seconds %= day 94 95 hour = 60 * 60 96 hours = seconds / hour 97 seconds %= hour 98 99 minute = 60 100 minutes = seconds / minute 101 seconds %= minute 102 103 if weeks >= 1: 104 chunks.append(gettext.dngettext( 105 configure.PACKAGE, 106 N_('%d week'), N_('%d weeks'), weeks) % weeks) 107 if days >= 1: 108 chunks.append(gettext.dngettext( 109 configure.PACKAGE, 110 N_('%d day'), N_('%d days'), days) % days) 111 112 chunk = _('%02d:%02d') % (hours, minutes) 113 if fractional > 0: 114 chunk += ':%0*.*f' % (fractional + 3, fractional, seconds) 115 116 chunks.append(chunk) 117 return " ".join(chunks)118 119121 """ 122 Format a timestamp in a human-readable format. 123 124 @param timeOrTuple: the timestamp to format 125 @type timeOrTuple: something that time.strftime will accept 126 127 @rtype: string 128 @returns: a nicely formatted timestamp string. 129 """ 130 return time.strftime("%Y-%m-%d %H:%M %Z", timeOrTuple)131 132134 """A version of time.strftime that can handle unicode formats. 135 @param format: format to convert, see man strftime(3) 136 @param t: time tuple as returned by time.localtime() 137 """ 138 out = [] 139 percent = False 140 for c in format: 141 if percent: 142 out.append(time.strftime('%' + c, t)) 143 percent = False 144 elif c == '%': 145 percent = True 146 else: 147 out.append(c) 148 if percent: 149 out.append('%') 150 return ''.join(out)151
Trees | Indices | Help |
---|
Generated by Epydoc 3.0.1 on Fri Sep 24 12:50:30 2010 | http://epydoc.sourceforge.net |