Skip to content

Commit 92c4a50

Browse files
tbruckmaierkarlicoss
authored andcommitted
Added support for open clock dates
1 parent e79228f commit 92c4a50

4 files changed

Lines changed: 18 additions & 10 deletions

File tree

orgparse/date.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ class OrgDateClock(OrgDate):
548548

549549
_allow_short_range = False
550550

551-
def __init__(self, start, end, duration=None, active=None):
551+
def __init__(self, start, end=None, duration=None, active=None):
552552
"""
553553
Create OrgDateClock object
554554
"""
@@ -600,20 +600,25 @@ def from_str(cls, line: str) -> 'OrgDateClock':
600600
match = cls._re.search(line)
601601
if not match:
602602
return cls(None, None)
603-
groups = [int(d) for d in match.groups()]
604-
ymdhm1 = groups[:5]
605-
ymdhm2 = groups[5:10]
606-
hm3 = groups[10:]
603+
604+
ymdhm1 = [int(d) for d in match.groups()[:5]]
605+
606+
# second part starting with "--", does not exist for open clock dates
607+
has_end = bool(match.group(6))
608+
if has_end:
609+
ymdhm2 = [int(d) for d in match.groups()[6:11]]
610+
hm3 = [int(d) for d in match.groups()[11:]]
611+
607612
return cls(
608613
datetime.datetime(*ymdhm1), # type: ignore[arg-type]
609-
datetime.datetime(*ymdhm2), # type: ignore[arg-type]
610-
hm3[0] * 60 + hm3[1],
614+
datetime.datetime(*ymdhm2) if has_end else None, # type: ignore[arg-type]
615+
hm3[0] * 60 + hm3[1] if has_end else None,
611616
)
612617

613618
_re = re.compile(
614619
r'^(?!#).*CLOCK:\s+'
615-
r'\[(\d+)\-(\d+)\-(\d+)[^\]\d]*(\d+)\:(\d+)\]--'
616-
r'\[(\d+)\-(\d+)\-(\d+)[^\]\d]*(\d+)\:(\d+)\]\s+=>\s+(\d+)\:(\d+)'
620+
r'\[(\d+)\-(\d+)\-(\d+)[^\]\d]*(\d+)\:(\d+)\]'
621+
r'(--\[(\d+)\-(\d+)\-(\d+)[^\]\d]*(\d+)\:(\d+)\]\s+=>\s+(\d+)\:(\d+))?'
617622
)
618623

619624

orgparse/tests/data/04_logbook.org

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
* LOGBOOK drawer test
22
:LOGBOOK:
3+
CLOCK: [2012-10-26 Fri 16:01]
34
CLOCK: [2012-10-26 Fri 14:50]--[2012-10-26 Fri 15:00] => 0:10
45
CLOCK: [2012-10-26 Fri 14:30]--[2012-10-26 Fri 14:40] => 0:10
56
CLOCK: [2012-10-26 Fri 14:10]--[2012-10-26 Fri 14:20] => 0:10

orgparse/tests/data/04_logbook.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
data = [dict(
44
heading='LOGBOOK drawer test',
55
clock=[
6+
OrgDateClock((2012, 10, 26, 16, 1)),
67
OrgDateClock((2012, 10, 26, 14, 50), (2012, 10, 26, 15, 00)),
78
OrgDateClock((2012, 10, 26, 14, 30), (2012, 10, 26, 14, 40)),
89
OrgDateClock((2012, 10, 26, 14, 10), (2012, 10, 26, 14, 20)),

orgparse/tests/test_date.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ def test_date_as_string() -> None:
2929

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

33-
3435
def test_date_as_datetime() -> None:
3536
testdate = (2021, 9, 3)
3637
testdatetime = (2021, 9, 3, 16, 19, 13)

0 commit comments

Comments
 (0)