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
dd7443f6
Commit
dd7443f6
authored
Jan 11, 2015
by
Bram Schoenmakers
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'fixRelativeDates' of
https://github.com/mruwek/topydo
into mruwek-fixRelativeDates2
parents
8f1153a0
da473a62
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
8 deletions
+42
-8
test/RelativeDateTest.py
test/RelativeDateTest.py
+32
-4
topydo/lib/RelativeDate.py
topydo/lib/RelativeDate.py
+10
-4
No files found.
test/RelativeDateTest.py
View file @
dd7443f6
...
...
@@ -43,12 +43,40 @@ class RelativeDateTester(TopydoTest.TopydoTest):
self
.
assertEquals
(
result
,
date
.
today
()
+
timedelta
(
weeks
=
1
))
def
test_one_month
(
self
):
result
=
relative_date_to_date
(
'1m'
)
self
.
assertEquals
(
result
,
date
.
today
()
+
timedelta
(
30
))
test_date
=
date
(
2015
,
1
,
10
)
result
=
relative_date_to_date
(
'1m'
,
test_date
)
self
.
assertEquals
(
result
,
date
(
2015
,
2
,
10
))
def
test_one_month_ext
(
self
):
test_date1
=
date
(
2015
,
1
,
29
)
test_date2
=
date
(
2016
,
1
,
31
)
test_date3
=
date
(
2015
,
12
,
31
)
test_date4
=
date
(
2015
,
7
,
31
)
test_date5
=
date
(
2015
,
10
,
31
)
result1
=
relative_date_to_date
(
'1m'
,
test_date1
)
result2
=
relative_date_to_date
(
'1m'
,
test_date2
)
result3
=
relative_date_to_date
(
'1m'
,
test_date3
)
result4
=
relative_date_to_date
(
'1m'
,
test_date4
)
result5
=
relative_date_to_date
(
'1m'
,
test_date5
)
self
.
assertEquals
(
result1
,
date
(
2015
,
2
,
28
))
self
.
assertEquals
(
result2
,
date
(
2016
,
2
,
29
))
self
.
assertEquals
(
result3
,
date
(
2016
,
1
,
31
))
self
.
assertEquals
(
result4
,
date
(
2015
,
8
,
31
))
self
.
assertEquals
(
result5
,
date
(
2015
,
11
,
30
))
def
test_one_year
(
self
):
result
=
relative_date_to_date
(
'1y'
)
self
.
assertEquals
(
result
,
date
.
today
()
+
timedelta
(
365
))
test_date
=
date
(
2015
,
1
,
10
)
result
=
relative_date_to_date
(
'1y'
,
test_date
)
self
.
assertEquals
(
result
,
date
(
2016
,
1
,
10
))
def
test_leap_year
(
self
):
test_date
=
date
(
2016
,
2
,
29
)
result1
=
relative_date_to_date
(
'1y'
,
test_date
)
result2
=
relative_date_to_date
(
'4y'
,
test_date
)
self
.
assertEquals
(
result1
,
date
(
2017
,
2
,
28
))
self
.
assertEquals
(
result2
,
date
(
2020
,
2
,
29
))
def
test_zero_months
(
self
):
result
=
relative_date_to_date
(
'0m'
)
...
...
topydo/lib/RelativeDate.py
View file @
dd7443f6
...
...
@@ -17,8 +17,16 @@
""" This module deals with relative dates (2d, 5y, Monday, today, etc.) """
from
datetime
import
date
,
timedelta
import
calendar
import
re
def
_add_months
(
sourcedate
,
months
):
month
=
sourcedate
.
month
-
1
+
months
year
=
sourcedate
.
year
+
month
/
12
month
=
month
%
12
+
1
day
=
min
(
sourcedate
.
day
,
calendar
.
monthrange
(
year
,
month
)[
1
])
return
date
(
year
,
month
,
day
)
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
...
...
@@ -33,11 +41,9 @@ def _convert_pattern(p_length, p_periodunit, p_offset=date.today()):
elif
p_periodunit
==
'w'
:
result
=
p_offset
+
timedelta
(
weeks
=
p_length
)
elif
p_periodunit
==
'm'
:
# we'll consider a month to be 30 days
result
=
p_offset
+
timedelta
(
30
*
p_length
)
result
=
_add_months
(
p_offset
,
p_length
)
elif
p_periodunit
==
'y'
:
# we'll consider a year to be 365 days (yeah, I'm aware of leap years)
result
=
p_offset
+
timedelta
(
365
*
p_length
)
result
=
_add_months
(
p_offset
,
p_length
*
12
)
return
result
...
...
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