def filesizeformat(value): prefixes = [ 'digital-kilobyte', 'digital-megabyte', 'digital-gigabyte', 'digital-terabyte', ] locale = get_locale() base = 1024 # # we are using the long length because the short length has no # plural variant and it reads like a bug instead of something # on purpose # if value < base: return units.format_unit(value, "byte", locale=locale, length="long") else: i = min(int(math.log(value, base)), len(prefixes)) - 1 prefix = prefixes[i] bytes = float(value) / base ** (i + 1) return units.format_unit(bytes, prefix, locale=locale, length="short")
def format_runtime(runtime): retVal = "" if runtime.days: retVal = format_unit( runtime.days, 'duration-day', length="long", locale=get_locale()) + ', ' mins, seconds = divmod(runtime.seconds, 60) hours, minutes = divmod(mins, 60) # ToDo: locale.number_symbols._data['timeSeparator'] -> localize time separator ? if hours: retVal += '{:d}:{:02d}:{:02d}s'.format(hours, minutes, seconds) elif minutes: retVal += '{:2d}:{:02d}s'.format(minutes, seconds) else: retVal += '{:2d}s'.format(seconds) return retVal
def format_unit(unit, value, length='short'): return units.format_unit(value=value, measurement_unit=unit, length=length, locale=flask_babel.get_locale())
print(Locale('el', 'GR').scripts['Copt']) # Calendar locale = Locale('it') month_names = locale.days['format']['wide'].items() print(list(month_names)) # Date, time d = date(2010, 3, 10) print(format_date(d, format='short', locale='it')) print(format_date(d, format='full', locale='it')) print(format_date(d, "EEEE, d.M.yyyy", locale='de')) dt = datetime.now() print(format_datetime(dt, "yyyy.MMMM.dd GGG hh:mm a", locale='en')) # Numbers/ Units print(format_decimal(123.45123, locale='en_US')) print(format_decimal(123.45123, locale='de')) print(format_unit(12, 'length-meter', locale='en_GB')) print(format_unit(12, 'length-meter', locale='en_US')) print(parse_decimal('2.029,98', locale='de'))