diff --git a/README.md b/README.md index 0ce417b..4fd0f81 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,18 @@ datetime.date(2015, 2, 5) True ``` +## Pretty-Printing Time + +The `._print()` method can be used to get a string representation of the PrettyTime object in a human-readable format. This method returns a string that lists the time units (seconds, minutes, hours, days, weeks, months, years) that are not zero in the PrettyTime object. + +Example: + +```python +>>> from prettytime import t +>>> print(t(3).days._print()) +'3 days' +``` + ## Planned changes: + Add [`django-pretty-times`](https://pypi.python.org/pypi/django-pretty-times/0.1.0)-like functionality to allow pretty printing as well diff --git a/prettytime.py b/prettytime.py index e192b74..f34780f 100644 --- a/prettytime.py +++ b/prettytime.py @@ -110,6 +110,14 @@ def __init__(self, num=None): self.num = None self.today = datetime.datetime.today() + def _print(self): + if datetime.datetime.today() < self.today: + return str(self.num) + " " + self.attr + " from now" + elif datetime.datetime.today() == self.today: + return "today" + else: + return str(self.num) + " " + self.attr + " ago" + def _make(self, timedict): return PrettyDelta(**timedict)