Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
T
topydo
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
topydo
Commits
b46691b6
Commit
b46691b6
authored
Sep 09, 2014
by
Bram Schoenmakers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow for supplying an offset for relative dates.
parent
0788dad0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
7 deletions
+16
-7
RelativeDate.py
RelativeDate.py
+7
-7
test/RelativeDateTest.py
test/RelativeDateTest.py
+9
-0
No files found.
RelativeDate.py
View file @
b46691b6
...
...
@@ -3,7 +3,7 @@
from
datetime
import
date
,
timedelta
import
re
def
_convert_pattern
(
p_length
,
p_periodunit
):
def
_convert_pattern
(
p_length
,
p_periodunit
,
p_offset
=
date
.
today
()
):
"""
Converts a pattern in the form [0-9][dwmy] and returns a date from today
with the period of time added to it.
...
...
@@ -13,15 +13,15 @@ def _convert_pattern(p_length, p_periodunit):
p_length
=
int
(
p_length
)
if
p_periodunit
==
'd'
:
result
=
date
.
today
()
+
timedelta
(
p_length
)
result
=
p_offset
+
timedelta
(
p_length
)
elif
p_periodunit
==
'w'
:
result
=
date
.
today
()
+
timedelta
(
weeks
=
p_length
)
result
=
p_offset
+
timedelta
(
weeks
=
p_length
)
elif
p_periodunit
==
'm'
:
# we'll consider a month to be 30 days
result
=
date
.
today
()
+
timedelta
(
30
*
p_length
)
result
=
p_offset
+
timedelta
(
30
*
p_length
)
elif
p_periodunit
==
'y'
:
# we'll consider a year to be 365 days (yeah, I'm aware of leap years)
result
=
date
.
today
()
+
timedelta
(
365
*
p_length
)
result
=
p_offset
+
timedelta
(
365
*
p_length
)
return
result
...
...
@@ -50,7 +50,7 @@ def _convert_weekday_pattern(p_weekday):
shift
=
(
target_day
-
day
)
%
7
return
date
.
today
()
+
timedelta
(
shift
)
def
relative_date_to_date
(
p_date
):
def
relative_date_to_date
(
p_date
,
p_offset
=
date
.
today
()
):
"""
Transforms a relative date into a date object.
...
...
@@ -70,7 +70,7 @@ def relative_date_to_date(p_date):
if
relative
:
length
=
relative
.
group
(
'length'
)
period
=
relative
.
group
(
'period'
)
result
=
_convert_pattern
(
length
,
period
)
result
=
_convert_pattern
(
length
,
period
,
p_offset
)
elif
weekday
:
result
=
_convert_weekday_pattern
(
weekday
.
group
(
0
))
...
...
test/RelativeDateTest.py
View file @
b46691b6
...
...
@@ -60,6 +60,11 @@ class RelativeDateTester(unittest.TestCase):
result
=
RelativeDate
.
relative_date_to_date
(
'tod'
)
self
.
assertEquals
(
result
,
self
.
today
)
def
test_today3
(
self
):
result
=
RelativeDate
.
relative_date_to_date
(
'today'
,
\
date
.
today
()
+
timedelta
(
1
))
self
.
assertEquals
(
result
,
self
.
today
)
def
test_tomorrow1
(
self
):
result
=
RelativeDate
.
relative_date_to_date
(
'Tomorrow'
)
self
.
assertEquals
(
result
,
self
.
tomorrow
)
...
...
@@ -83,3 +88,7 @@ class RelativeDateTester(unittest.TestCase):
def
test_monday4
(
self
):
result
=
RelativeDate
.
relative_date_to_date
(
'mondayy'
)
self
.
assertFalse
(
result
)
def
test_offset1
(
self
):
result
=
RelativeDate
.
relative_date_to_date
(
'1d'
,
self
.
tomorrow
)
self
.
assertEquals
(
result
,
date
.
today
()
+
timedelta
(
2
))
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment