Skip to main content

ago: Human readable timedeltas

Project description

What are human readable timedeltas?

ago.py makes customizable human readable timedeltas, for example:

Testing past tense:

Russell commented 1 year, 127 days, 16 hours ago
You replied 1 year, 127 days ago

Testing future tense:

Program will shutdown in 2 days, 3 hours, 27 minutes
Job will run 2 days, 3 hours from now

How to install

There are a number of ways to install this package:

easy_install ago

pip install ago

or specify ago under the setup_requires list within your setuptools-compatible project’s setup.py file.

How to use

The ago module comes with two functions:

  1. human

  2. delta2dict

You really only need to worry about human.

Here are all the available arguments and defaults:

human(dt, precision=2, past_tense='{} ago', future_tense='in {}'):
dt

either a datetime or timedelta object to become human readable, required

precision

control how verbose the output should look, optional

past_tense

format string used when dt is a past datetime, optional

future_tense

format string used when dt is a future datetime, optional

Here is an example on how to use human:

from ago import human
from ago import delta2dict

from datetime import datetime
from datetime import timedelta

# pretend this was stored in database
db_date = datetime(
  year = 2010,
  month=5,
  day=4,
  hour=6,
  minute=54,
  second=33,
  microsecond=4000
 )

# to find out how long ago, use the human function
print 'Created ' + human( db_date )

# optionally pass a precision
print 'Created ' + human( db_date, 3 )
print 'Created ' + human( db_date, 6 )

We also support future dates and times:

PRESENT = datetime.now()
PAST = PRESENT - timedelta( 492, 58711, 45 ) # days, secs, ms
FUTURE = PRESENT + timedelta( 2, 12447, 963 ) # days, secs, ms

print human( FUTURE )

Example past_tense and future_tense keyword arguments:

output1 = human( PAST,
  past_tense = 'titanic sunk {0} ago',
  future_tense = 'titanic will sink in {0} from now'
)

output2 = human( FUTURE,
  past_tense = 'titanic sunk {0} ago',
  future_tense = 'titanic will sink in {0} from now'
)

print output1
# titanic sunk 1 year, 127 days ago
print output2
# titanic will sink in 2 days, 3 hours from now

Now we will document how to use delta2dict:

# subtract two datetime objects for a timedelta object
delta = PRESENT - db_date

# create a dictionary of units out of the timedelta
print delta2dict( delta )

Need more examples?

You should look at test_ago.py

How do I thank you?

You should follow me on twitter http://twitter.com/russellbal

License

Public Domain

Public Revision Control

https://bitbucket.org/russellballestrini/ago

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ago-0.0.6.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

ago-0.0.6-py2.7.egg (3.6 kB view details)

Uploaded Egg

File details

Details for the file ago-0.0.6.tar.gz.

File metadata

  • Download URL: ago-0.0.6.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for ago-0.0.6.tar.gz
Algorithm Hash digest
SHA256 a64811a5a44cd3ba687d800986edf0f7a97859b8da75d3347c915b58b0869b44
MD5 e2fdc21fb922b4fc21ec19c6eac6bd46
BLAKE2b-256 242400a3e8d9a263091e695fad26e4090d1e9cab1ee5f3fd2ab698a19b9b4539

See more details on using hashes here.

File details

Details for the file ago-0.0.6-py2.7.egg.

File metadata

  • Download URL: ago-0.0.6-py2.7.egg
  • Upload date:
  • Size: 3.6 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for ago-0.0.6-py2.7.egg
Algorithm Hash digest
SHA256 15e71b3fe89673a2d6d6b79b4f2f9a5656ecc4036375c3788202e2bc7d136bb9
MD5 9751560a305e2475fdb69d1a2e57ee6b
BLAKE2b-256 32bb700504ee8e1f9394a8cffcc298c778f0b605e08ffa4515d6647068718f36

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page