Skip to content

Python PrettyTime - A Python module allowing Rails-style pretty times like t(3).days.ago

Notifications You must be signed in to change notification settings

jdotjdot/PrettyTime

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PrettyTime

Version 0.0.5a

Had enough of dates and times in Python? Sick of boilerplate like datetime.date.today() + datetime.timedelta(weeks=12) when you're just trying to say "three months from now"?

Welcome to PrettyTime:

t(3).months.from_.today

PrettyTime is a small Python package that intends to create a better interface for working with dates and times in Python. It was inspired by Rails' syntax for dates and times, like:

3.days.ago
2.hours.from_now

Though Python does not allow the same type of built-in monkey-patching, you can get decently close. An example of what PrettyTime currently offers:

>>> t(4).hours.from_.now
datetime.datetime(2014, 6, 7, 3, 51, 51, 422545)
>>> t(1).years.from_.today
datetime.date(2015, 6, 6)
>>> t()
datetime.datetime(2014, 7, 28, 16, 58, 1, 229448)

Usage

Download

Install from PyPI:

pip install prettytime

Alternatively, you can clone the GitHub repository:

git clone https://github.com/jdotjdot/PrettyTime

Import

from prettytime import t

Use

Because you can't override Python literals, all integers must be wrapped by t(). Everything else tries to be normal English.

Just using t() by itself with no arguments returns the time now, directly calling datetime.datetime.now(). You can get today's date with t().date()

Commands currently supported:

Date/Time Relative Optional Optional
second(s) ago next week
minute(s) from_ last month
hour(s) before now year
day(s) after today
week(s) tomorrow
month(s) yesterday
year(s) then

To get the time difference from an arbitrary date, you can pass in a date or datetime object (including those generated by PrettyTime) into the then() method.

Examples:

>>> from prettytime import *
>>> t(3).days.from_.next.year
datetime.date(2015, 6, 15)
>>> t(4).years.ago
datetime.date(2010, 6, 12)
>>> t(10).months.before.last.week
datetime.date(2013, 8, 5)
>>> t(7).minutes.after.tomorrow
datetime.datetime(2014, 6, 13, 23, 57, 44, 38401)
>>> t(2).days.from_.then(datetime.date(2015, 2, 3))
datetime.date(2015, 2, 5)
>>> t(3).days.from_.today == t(3).days.from_.then(t().date())
True

Planned changes:

Changelog:

  • 1/28/2018 - Python 3 compatibility
  • 7/28/2014 - t() returns a datetime.datetime.now() object

About

Python PrettyTime - A Python module allowing Rails-style pretty times like t(3).days.ago

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages