Giter VIP home page Giter VIP logo

readabledelta's Introduction

Installation

pip install readabledelta

Usage examples

The readabledelta is just a more human-friendly printable version of timedelta. The public interface is equivalent to timedelta, only printing behaviour has been modified.

It's easy to confuse hours/minutes with minutes/seconds in the default formatting of timedeltas

>>> lunchtime = datetime(year=2015, month=5, day=27, hour=12)
>>> right_now = datetime(year=2015, month=5, day=27, hour=13, minute=5)
>>> 'Lunch was {} ago'.format(delta)
'Lunch was 1:05:00 ago'
>>> 'Lunch was {} ago'.format(readabledelta(delta))
'Lunch was 1 hour and 5 minutes ago'

For negative timedeltas, the default representation is more machine-friendly than human-friendly: "an hour and five minutes" back is easier for people to understand than the weird but technically-correct "negative one day plus 22 hours and 55 minutes"

>>> '{}'.format(lunchtime - right_now)
'-1 day, 22:55:00'
>>> '{}'.format(readabledelta(lunchtime - right_now))
'-1 hour and 5 minutes'

A readabledelta is a timedelta, compares as you'd expect, and is simple enough not to misbehave.

>>> issubclass(readabledelta, timedelta)
True
>>> isinstance(readabledelta(), timedelta)
True
>>> readabledelta() == timedelta()
True

This means you can safely add or subtract them to datetime instances, there will be no unpleasant surprises with arithmetic.

readabledelta's People

Contributors

wimglenn avatar

Stargazers

Alexander Ilin avatar

Watchers

James Cloos avatar  avatar

readabledelta's Issues

Add custom options to __format__ with significant digits

To round off a number to n significant figures-

Discard all digits to the right of nth significant digit.
If this discarded number is

  • less than half a unit in nth place, leave the nth digit unchanged.
  • greater than half a unit in the nth place, increase the nth digit by unity.
  • exactly half a unit in the nth place, increase the nth digit by unity if its odd, otherwise leave it unchanged. (???)

Then add e.g. '{rd:3}' handling in format spec.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.