Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added __str__() to OrgDate #44

Merged
merged 2 commits into from
Oct 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions orgparse/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,28 @@ def gene_timestamp_regex(brtype, prefix=None, nocookie=False):
return regex.format(prefix=prefix, ignore=ignore)


def date_time_format(date) -> str:
"""
Format a date or datetime in default org format

@param date The date

@return Formatted date(time)
"""
default_format_date = "%Y-%m-%d %a"
default_format_datetime = "%Y-%m-%d %a %H:%M"
is_datetime = isinstance(date, datetime.datetime)

return date.strftime(default_format_datetime if is_datetime else default_format_date)


def is_same_day(date0, date1) -> bool:
"""
Check if two dates or datetimes are on the same day
"""
return (OrgDate._date_to_tuple(date0)[:3] == OrgDate._date_to_tuple(date1)[:3])


TIMESTAMP_NOBRACE_RE = re.compile(
gene_timestamp_regex('nobrace', prefix=''),
re.VERBOSE)
Expand All @@ -154,6 +176,14 @@ class OrgDate(object):

"""

"""
When formatting the date to string via __str__, and there is an end date on
the same day as the start date, allow formatting in the short syntax
<2021-09-03 Fri 16:01--17:30>? Otherwise the string represenation would be
<2021-09-03 Fri 16:01>--<2021-09-03 Fri 17:30>
"""
_allow_short_range = True

def __init__(self, start, end=None, active=None):
"""
Create :class:`OrgDate` object
Expand Down Expand Up @@ -232,6 +262,24 @@ def __repr__(self):
else:
return '{0}({1!r}, {2!r}, {3!r})'.format(*args)

def __str__(self):
fence = ("<", ">") if self.is_active() else ("[", "]")

start = date_time_format(self.start)
end = None

if self.has_end():
if self._allow_short_range and is_same_day(self.start, self.end):
start += "--%s" % self.end.strftime("%H:%M")
else:
end = date_time_format(self.end)

ret = "%s%s%s" % (fence[0], start, fence[1])
if end:
ret += "--%s%s%s" % (fence[0], end, fence[1])

return ret

def __nonzero__(self):
return bool(self._start)

Expand Down Expand Up @@ -498,6 +546,8 @@ class OrgDateClock(OrgDate):

_active_default = False

_allow_short_range = False

def __init__(self, start, end, duration=None, active=None):
"""
Create OrgDateClock object
Expand Down
31 changes: 30 additions & 1 deletion orgparse/tests/test_date.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,36 @@
from orgparse.date import OrgDate
from orgparse.date import OrgDate, OrgDateScheduled, OrgDateDeadline, OrgDateClock, OrgDateClosed
import datetime


def test_date_as_string() -> None:

testdate = datetime.date(2021, 9, 3)
testdate2 = datetime.date(2021, 9, 5)
testdatetime = datetime.datetime(2021, 9, 3, 16, 19, 13)
testdatetime2 = datetime.datetime(2021, 9, 3, 17, 0, 1)
testdatetime_nextday = datetime.datetime(2021, 9, 4, 0, 2, 1)

assert str(OrgDate(testdate)) == "<2021-09-03 Fri>"
assert str(OrgDate(testdatetime)) == "<2021-09-03 Fri 16:19>"
assert str(OrgDate(testdate, active=False)) == "[2021-09-03 Fri]"
assert str(OrgDate(testdatetime, active=False)) == "[2021-09-03 Fri 16:19]"

assert str(OrgDate(testdate, testdate2)) == "<2021-09-03 Fri>--<2021-09-05 Sun>"
assert str(OrgDate(testdate, testdate2)) == "<2021-09-03 Fri>--<2021-09-05 Sun>"
assert str(OrgDate(testdate, testdate2, active=False)) == "[2021-09-03 Fri]--[2021-09-05 Sun]"
assert str(OrgDate(testdate, testdate2, active=False)) == "[2021-09-03 Fri]--[2021-09-05 Sun]"

assert str(OrgDateScheduled(testdate)) == "<2021-09-03 Fri>"
assert str(OrgDateScheduled(testdatetime)) == "<2021-09-03 Fri 16:19>"
assert str(OrgDateDeadline(testdate)) == "<2021-09-03 Fri>"
assert str(OrgDateDeadline(testdatetime)) == "<2021-09-03 Fri 16:19>"
assert str(OrgDateClosed(testdate)) == "[2021-09-03 Fri]"
assert str(OrgDateClosed(testdatetime)) == "[2021-09-03 Fri 16:19]"

assert str(OrgDateClock(testdatetime, testdatetime2)) == "[2021-09-03 Fri 16:19]--[2021-09-03 Fri 17:00]"
assert str(OrgDateClock(testdatetime, testdatetime_nextday)) == "[2021-09-03 Fri 16:19]--[2021-09-04 Sat 00:02]"


def test_date_as_datetime() -> None:
testdate = (2021, 9, 3)
testdatetime = (2021, 9, 3, 16, 19, 13)
Expand Down