def day(t, now=None, format='%B %d'): ''' Date delta compared to ``t``. You can override ``now`` to specify what date to compare to. You can override the date format by supplying a ``format`` parameter. :param t: timestamp, :class:`datetime.date` or :class:`datetime.datetime` object :param now: default ``None``, optionally a :class:`datetime.datetime` object :param format: default ``'%B %d'`` ''' t1 = _to_date(t) t2 = _to_date(now or datetime.datetime.now()) diff = max(t1, t2) - min(t1, t2) if diff.days == 0: return _(u'today') elif diff.days == 1: if t1 < t2: return _(u'yesterday') else: return _(u'tomorrow') elif diff.days == 7: if t1 < t2: return _(u'last week') else: return _(u'next week') else: return t1.strftime(format)
def day(t, now=None, format='%B %d'): ''' Date delta compared to ``t``. You can override ``now`` to specify what date to compare to. You can override the date format by supplying a ``format`` parameter. :param t: timestamp, :class:`datetime.date` or :class:`datetime.datetime` object :param now: default ``None``, optionally a :class:`datetime.datetime` object :param format: default ``'%B %d'`` ''' t1 = _to_date(t) t2 = _to_date(now or datetime.datetime.now()) diff = t1 - t2 secs = diff.total_seconds() days = abs(diff.days) if days == 0: return _(u'today') elif days == 1: if secs < 0: return _(u'yesterday') else: return _(u'tomorrow') elif days == 7: if secs < 0: return _(u'last week') else: return _(u'next week') else: return t1.strftime(format)
def compress(t, sign=False, pad=u''): ''' Convert the input to compressed format, works with a :class:`datetime.timedelta` object or a number that represents the number of seconds you want to compress. If you supply a timestamp or a :class:`datetime.datetime` object, it will give the delta relative to the current time. You can enable showing a sign in front of the compressed format with the ``sign`` parameter, the default is not to show signs. Optionally, you can chose to pad the output. If you wish your values to be separated by spaces, set ``pad`` to ``' '``. :param t: seconds or :class:`datetime.timedelta` object :param sign: default ``False`` :param pad: default ``u''`` ''' if isinstance(t, datetime.timedelta): seconds = t.seconds + (t.days * 86400) elif isinstance(t, float) or isinstance(t, int) or isinstance(t, long): seconds = long(t) else: return compress(datetime.datetime.now() - _to_datetime(t), sign, pad) parts = [] if sign: parts.append(u'-' if t.days < 0 else u'+') weeks, seconds = divmod(seconds, TIME_WEEK) days, seconds = divmod(seconds, TIME_DAY) hours, seconds = divmod(seconds, TIME_HOUR) minutes, seconds = divmod(seconds, TIME_MINUTE) if weeks: parts.append(_(u'%dw') % (weeks,)) if days: parts.append(_(u'%dd') % (days,)) if hours: parts.append(_(u'%dh') % (hours,)) if minutes: parts.append(_(u'%dm') % (minutes,)) if seconds: parts.append(_(u'%ds') % (seconds,)) return pad.join(parts)
def compress(t, sign=False, pad=u''): ''' Convert the input to compressed format, works with a :class:`datetime.timedelta` object or a number that represents the number of seconds you want to compress. If you supply a timestamp or a :class:`datetime.datetime` object, it will give the delta relative to the current time. You can enable showing a sign in front of the compressed format with the ``sign`` parameter, the default is not to show signs. Optionally, you can chose to pad the output. If you wish your values to be separated by spaces, set ``pad`` to ``' '``. :param t: seconds or :class:`datetime.timedelta` object :param sign: default ``False`` :param pad: default ``u''`` ''' if isinstance(t, datetime.timedelta): seconds = t.seconds + (t.days * 86400) elif isinstance(t, float) or isinstance(t, int) or isinstance(t, long): seconds = long(t) else: return compress(datetime.datetime.now() - _to_datetime(t), sign, pad) parts = [] if sign: parts.append(u'-' if d.days < 0 else u'+') weeks, seconds = divmod(seconds, 604800) days, seconds = divmod(seconds, 86400) hours, seconds = divmod(seconds, 3600) minutes, seconds = divmod(seconds, 60) if weeks: parts.append(_(u'%dw') % (weeks, )) if days: parts.append(_(u'%dd') % (days, )) if hours: parts.append(_(u'%dh') % (hours, )) if minutes: parts.append(_(u'%dm') % (minutes, )) if seconds: parts.append(_(u'%ds') % (seconds, )) return pad.join(parts)
def duration(t, now=None, precision=1, pad=u', ', words=None, plain=False): ''' Time delta compared to ``t``. You can override ``now`` to specify what time to compare to. :param t: timestamp, :class:`datetime.date` or :class:`datetime.datetime` object :param now: default ``None``, optionally a :class:`datetime.datetime` object :param precision: default ``1``, number of fragments to return :param words: default ``None``, allow words like "yesterday", if set to ``None`` this will be enabled if ``precision`` is set to ``1`` ''' if words is None: words = precision == 1 t1 = _to_datetime(t) t2 = _to_datetime(now or datetime.datetime.now()) if not plain: if t1 < t2: format = _(u'%s ago') else: format = _(u'%s from now') else: format = _(u'%s') result, remains = delta(max(t1, t2), min(t1, t2), words=words) if result in (_(u'just now'), _(u'yesterday')): return result elif precision > 1 and remains: t3 = t2 - datetime.timedelta(seconds=remains) return pad.join([ result, duration(t3, t2, precision - 1, pad, words=False), ]) else: return format % (result, )
def bban(value, country=None, validate=False): ''' Printable Basic Bank Account Number (BBAN) for the given country code. The ``country`` must be a valid ISO 3166-2 country code. :param value: string, int or long :param country: string >>> bban('068-9999995-01', 'BE') u'068999999501' >>> bban('555', 'NL') u'555' >>> bban('555', 'NL', validate=True) Traceback (most recent call last): ... ValueError: Invalid BBAN, number does not match specification >>> bban('123', 'XY', validate=True) Traceback (most recent call last): ... ValueError: Invalid BBAN, country unknown ''' value = bban_compact(value) if validate: country = country.upper() try: rules = BBAN_RULES[country] except KeyError: raise ValueError(_('Invalid BBAN, country unknown')) regex = _bban_regex(rules['bban']) if not regex.match(value): raise ValueError( _('Invalid BBAN, number does not match specification') ) return value
def duration(t, now=None, precision=1, pad=', ', words=None, plain=False): ''' Time delta compared to ``t``. You can override ``now`` to specify what time to compare to. :param t: timestamp, :class:`datetime.date` or :class:`datetime.datetime` object :param now: default ``None``, optionally a :class:`datetime.datetime` object :param precision: default ``1``, number of fragments to return :param words: default ``None``, allow words like "yesterday", if set to ``None`` this will be enabled if ``precision`` is set to ``1`` ''' if words is None: words = precision == 1 t1 = _to_datetime(t) t2 = _to_datetime(now or datetime.datetime.now()) if not plain: if t1 < t2: format = _('%s ago') else: format = _('%s from now') else: format = _('%s') result, remains = delta(max(t1, t2), min(t1, t2), words=words) if result in (_('just now'), _('yesterday')): return result elif precision > 1 and remains: t3 = t2 - datetime.timedelta(seconds=remains) return pad.join([ result, duration(t3, t2, precision - 1, pad, words=False), ]) else: return format % (result,)
def iban(number, validate=False): ''' Printable International Bank Account Number (IBAN) as specified in ISO 13616. :param number: string >>> iban('BE43068999999501') u'BE43 0689 9999 9501' >>> iban('XY32012341234123', validate=True) Traceback (most recent call last): ... ValueError: Invalid IBAN, country unknown >>> iban('BE43068999999502', validate=True) Traceback (most recent call last): ... ValueError: Invalid IBAN, digits check failed ''' number = bban_compact(number) if validate: country = number[:2] if country not in BBAN_RULES: raise ValueError(_('Invalid IBAN, country unknown')) # Do the 10-mod-97 check digits = bban_base10(number) if long(digits) % 97 != 1: raise ValueError(_('Invalid IBAN, digits check failed')) # Check BBAN for country bban(number[4:], country, validate=True) groups = [number[x:x + 4] for x in xrange(0, len(number), 4)] return u' '.join(groups)
def throughput(sample, window=1, format='decimal'): ''' Return the throughput in (intelli)bytes per second. :param sample: number of samples sent :param window: default 1, sample window in seconds or :class:`datetime.timedelta` object :param format: default 'decimal', see :func:`natural.size.filesize` ''' if isinstance(window, datetime.timedelta): window = float(window.days * 86400 + window.seconds) elif isinstance(window, basestring): window = float(window) per_second = sample / float(window) return _('%s/s') % (filesize(per_second, format=format),)
def throughput(sample, window=1, format='decimal'): ''' Return the throughput in (intelli)bytes per second. :param sample: number of samples sent :param window: default 1, sample window in seconds or :class:`datetime.timedelta` object :param format: default 'decimal', see :func:`natural.size.filesize` ''' if isinstance(window, datetime.timedelta): window = float(window.days * 86400 + window.seconds) elif isinstance(window, basestring): window = float(window) per_second = sample / float(window) return _('%s/s') % (filesize(per_second, format=format), )
def duration(t, now=None, precision=1, pad=u', ', words=None, justnow=datetime.timedelta(seconds=10)): ''' Time delta compared to ``t``. You can override ``now`` to specify what time to compare to. :param t: timestamp, :class:`datetime.date` or :class:`datetime.datetime` object :param now: default ``None``, optionally a :class:`datetime.datetime` object :param precision: default ``1``, number of fragments to return :param words: default ``None``, allow words like "yesterday", if set to ``None`` this will be enabled if ``precision`` is set to ``1`` :param justnow: default ``datetime.timedelta(seconds=10)``, :class:`datetime.timedelta` object passed to :func:`delta` representing tolerance for considering argument ``t`` as meaning 'just now' ''' if words is None: words = precision == 1 t1 = _to_datetime(t) t2 = _to_datetime(now or datetime.datetime.now()) if t1 < t2: format = _(u'%s ago') else: format = _(u'%s from now') result, remains = delta(t1, t2, words=words, justnow=justnow) if result in ( _(u'just now'), _(u'yesterday'), _(u'tomorrow'), _(u'last week'), _(u'next week'), ): return result elif precision > 1 and remains: t3 = t2 - datetime.timedelta(seconds=remains) return pad.join([ result, duration(t2, t3, precision - 1, pad, words=False), ]) else: return format % (result,)
def delta(t1, t2, words=True, justnow=datetime.timedelta(seconds=10)): ''' Calculates the estimated delta between two time objects in human-readable format. Used internally by the :func:`day` and :func:`duration` functions. :param t1: timestamp, :class:`datetime.date` or :class:`datetime.datetime` object :param t2: timestamp, :class:`datetime.date` or :class:`datetime.datetime` object :param words: default ``True``, allow words like "yesterday", "tomorrow" and "just now" :param justnow: default ``datetime.timedelta(seconds=10)``, :class:`datetime.timedelta` object representing tolerance for considering a delta as meaning 'just now' ''' t1 = _to_datetime(t1) t2 = _to_datetime(t2) diff = t1 - t2 # The datetime module includes milliseconds with float precision. Floats # will give unexpected results here, so we round the value here total = math.ceil(diff.total_seconds()) total_abs = abs(total) if total_abs < TIME_DAY: if abs(diff) < justnow and words: return ( _(u'just now'), 0, ) elif total_abs < TIME_MINUTE: seconds = total_abs return ( _multi( _(u'%d second'), _(u'%d seconds'), seconds) % (seconds,), 0, ) elif total_abs < TIME_MINUTE * 2 and words: return ( _(u'a minute'), 0, ) elif total_abs < TIME_HOUR: minutes, seconds = divmod(total_abs, TIME_MINUTE) if total < 0: seconds *= -1 return ( _multi( _(u'%d minute'), _(u'%d minutes'), minutes) % (minutes,), seconds, ) elif total_abs < TIME_HOUR * 2 and words: return ( _(u'an hour'), 0, ) else: hours, seconds = divmod(total_abs, TIME_HOUR) if total < 0: seconds *= -1 return ( _multi( _(u'%d hour'), _(u'%d hours'), hours) % (hours,), seconds, ) elif total_abs < TIME_DAY * 2 and words: if total > 0: return (_(u'tomorrow'), 0) else: return (_(u'yesterday'), 0) elif total_abs < TIME_WEEK: days, seconds = divmod(total_abs, TIME_DAY) if total < 0: seconds *= -1 return ( _multi( _(u'%d day'), _(u'%d days'), days) % (days,), seconds, ) elif abs(diff.days) == TIME_WEEK and words: if total > 0: return (_(u'next week'), diff.seconds) else: return (_(u'last week'), diff.seconds) # FIXME # # The biggest reliable unit we can supply to the user is a week (for now?), # because we can not safely determine the amount of days in the covered # month/year span. else: weeks, seconds = divmod(total_abs, TIME_WEEK) if total < 0: seconds *= -1 return ( _multi( _(u'%d week'), _(u'%d weeks'), weeks) % (weeks,), seconds, )
def delta(t1, t2, words=True): ''' Calculates the estimated delta between two time objects in human-readable format. Used internally by the :func:`day` and :func:`duration` functions. :param t1: timestamp, :class:`datetime.date` or :class:`datetime.datetime` object :param t2: timestamp, :class:`datetime.date` or :class:`datetime.datetime` object ''' t1 = _to_datetime(t1) t2 = _to_datetime(t2) diff = t1 - t2 if diff.days == 0: if diff.seconds < 10 and words: return ( _('just now'), 0, ) elif diff.seconds < 60: return ( _multi( _('%d second'), _('%d seconds'), diff.seconds) % (diff.seconds,), 0, ) elif diff.seconds < 120 and words: return ( _('a minute'), 0, ) elif diff.seconds < 3600: minutes, seconds = divmod(diff.seconds, 60) return ( _multi( _('%d minute'), _('%d minutes'), minutes) % (minutes,), seconds, ) elif diff.seconds < 7200 and words: return ( _('an hour'), 0, ) elif diff.seconds < 86400: hours, seconds = divmod(diff.seconds, 3600) return ( _multi( _('%d hour'), _('%d hours'), hours) % (hours,), seconds, ) elif diff.days == 1 and words: return ( _('yesterday'), 0, ) elif diff.days < 7: return ( _multi( _('%d day'), _('%d days'), diff.days) % (diff.days,), diff.seconds, ) elif diff.days < 31: weeks, days = divmod(diff.days, 7) seconds = days * 86400 + diff.seconds return ( _multi( _('%d week'), _('%d weeks'), weeks) % (weeks,), seconds, ) elif diff.days < 365: months, days = divmod(diff.days, 30) seconds = days * 86400 + diff.seconds return ( _multi( _('%d month'), _('%d months'), months) % (months,), seconds, ) else: years, days = divmod(diff.days, 365) seconds = days * 86400 + diff.seconds return ( _multi( _('%d year'), _('%d years'), years) % (years,), seconds, )
def delta(t1, t2, words=True): ''' Calculates the estimated delta between two time objects in human-readable format. Used internally by the :func:`day` and :func:`duration` functions. :param t1: timestamp, :class:`datetime.date` or :class:`datetime.datetime` object :param t2: timestamp, :class:`datetime.date` or :class:`datetime.datetime` object ''' t1 = _to_datetime(t1) t2 = _to_datetime(t2) diff = t1 - t2 if diff.days == 0: if diff.seconds < 10 and words: return ( _(u'just now'), 0, ) elif diff.seconds < 60: return ( _multi(_(u'%d second'), _(u'%d seconds'), diff.seconds) % (diff.seconds, ), 0, ) elif diff.seconds < 120 and words: return ( _(u'a minute'), 0, ) elif diff.seconds < 3600: minutes, seconds = divmod(diff.seconds, 60) return ( _multi(_(u'%d minute'), _(u'%d minutes'), minutes) % (minutes, ), seconds, ) elif diff.seconds < 7200 and words: return ( _(u'an hour'), 0, ) elif diff.seconds < 86400: hours, seconds = divmod(diff.seconds, 3600) return ( _multi(_(u'%d hour'), _(u'%d hours'), hours) % (hours, ), seconds, ) elif diff.days == 1 and words: return ( _(u'yesterday'), 0, ) elif diff.days < 7: return ( _multi(_(u'%d day'), _(u'%d days'), diff.days) % (diff.days, ), diff.seconds, ) elif diff.days < 31: weeks, days = divmod(diff.days, 7) seconds = days * 86400 + diff.seconds return ( _multi(_(u'%d week'), _(u'%d weeks'), weeks) % (weeks, ), seconds, ) elif diff.days < 365: months, days = divmod(diff.days, 30) seconds = days * 86400 + diff.seconds return ( _multi(_(u'%d month'), _(u'%d months'), months) % (months, ), seconds, ) else: years, days = divmod(diff.days, 365) seconds = days * 86400 + diff.seconds return ( _multi(_(u'%d year'), _(u'%d years'), years) % (years, ), seconds, )