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
e393029a
Commit
e393029a
authored
Jul 05, 2014
by
Bram Schoenmakers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add function to convert a relative date to an absolute date.
parent
24fe4336
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
154 additions
and
0 deletions
+154
-0
RelativeDate.py
RelativeDate.py
+72
-0
test/RelativeDateTest.py
test/RelativeDateTest.py
+82
-0
No files found.
RelativeDate.py
0 → 100644
View file @
e393029a
from
datetime
import
date
,
timedelta
import
re
def
_convert_pattern
(
p_length
,
p_period
):
result
=
None
p_length
=
int
(
p_length
)
if
p_period
==
'd'
:
result
=
date
.
today
()
+
timedelta
(
p_length
)
elif
p_period
==
'w'
:
result
=
date
.
today
()
+
timedelta
(
weeks
=
p_length
)
elif
p_period
==
'm'
:
# we'll consider a month to be 30 days
result
=
date
.
today
()
+
timedelta
(
30
*
p_length
)
elif
p_period
==
'y'
:
# we'll consider a year to be 365 days (yeah, I'm aware of leap years)
result
=
date
.
today
()
+
timedelta
(
365
*
p_length
)
return
result
def
_convert_weekday_pattern
(
p_weekday
):
day_value
=
{
'mo'
:
0
,
'tu'
:
1
,
'we'
:
2
,
'th'
:
3
,
'fr'
:
4
,
'sa'
:
5
,
'su'
:
6
}
targetDayString
=
p_weekday
[:
2
].
lower
()
targetDay
=
day_value
[
targetDayString
]
day
=
date
.
today
().
weekday
()
shift
=
(
targetDay
-
day
)
%
7
return
date
.
today
()
+
timedelta
(
shift
)
def
relative_date_to_date
(
p_date
):
"""
Transforms a relative date into a date object.
The following formats are understood:
* [0-9][dwmy]
* 'today' or 'tomorrow'
* days of the week (in full or abbreviated)
"""
result
=
None
p_date
=
p_date
.
lower
()
relative
=
re
.
match
(
'(?P<length>[0-9]+)(?P<period>[dwmy])$'
,
p_date
,
re
.
I
)
weekday
=
re
.
match
(
'mo(n(day)?)?$|tu(e(sday)?)?$|we(d(nesday)?)?$|th(u(rsday)?)?$|fr(i(day)?)?$|sa(t(urday)?)?$|su(n(day)?)?$'
,
p_date
)
if
relative
:
length
=
relative
.
group
(
'length'
)
period
=
relative
.
group
(
'period'
)
result
=
_convert_pattern
(
length
,
period
)
elif
weekday
:
result
=
_convert_weekday_pattern
(
weekday
.
group
(
0
))
elif
re
.
match
(
'tod(ay)?$'
,
p_date
):
result
=
_convert_pattern
(
'0'
,
'd'
)
elif
re
.
match
(
'tom(orrow)?$'
,
p_date
):
result
=
_convert_pattern
(
'1'
,
'd'
)
return
result
test/RelativeDateTest.py
0 → 100644
View file @
e393029a
from
datetime
import
date
,
timedelta
import
unittest
import
RelativeDate
class
RelativeDateTester
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
today
=
date
.
today
()
self
.
tomorrow
=
self
.
today
+
timedelta
(
1
)
self
.
monday
=
self
.
today
+
timedelta
(
7
-
self
.
today
.
weekday
()
%
7
)
def
test_zero_days
(
self
):
result
=
RelativeDate
.
relative_date_to_date
(
'0d'
)
self
.
assertEquals
(
result
,
self
.
today
)
def
test_one_day
(
self
):
result
=
RelativeDate
.
relative_date_to_date
(
'1d'
)
self
.
assertEquals
(
result
,
self
.
tomorrow
)
def
test_one_week
(
self
):
result
=
RelativeDate
.
relative_date_to_date
(
'1w'
)
self
.
assertEquals
(
result
,
date
.
today
()
+
timedelta
(
weeks
=
1
))
def
test_one_month
(
self
):
result
=
RelativeDate
.
relative_date_to_date
(
'1m'
)
self
.
assertEquals
(
result
,
date
.
today
()
+
timedelta
(
30
))
def
test_one_year
(
self
):
result
=
RelativeDate
.
relative_date_to_date
(
'1y'
)
self
.
assertEquals
(
result
,
date
.
today
()
+
timedelta
(
365
))
def
test_zero_months
(
self
):
result
=
RelativeDate
.
relative_date_to_date
(
'0m'
)
self
.
assertEquals
(
result
,
self
.
today
)
def
test_zero_years
(
self
):
result
=
RelativeDate
.
relative_date_to_date
(
'0y'
)
self
.
assertEquals
(
result
,
self
.
today
)
def
test_garbage1
(
self
):
result
=
RelativeDate
.
relative_date_to_date
(
'0dd'
)
self
.
assertFalse
(
result
)
def
test_garbage2
(
self
):
result
=
RelativeDate
.
relative_date_to_date
(
'-0d'
)
self
.
assertFalse
(
result
)
def
test_one_day_capital
(
self
):
result
=
RelativeDate
.
relative_date_to_date
(
'1D'
)
self
.
assertEquals
(
result
,
self
.
tomorrow
)
def
test_today1
(
self
):
result
=
RelativeDate
.
relative_date_to_date
(
'today'
)
self
.
assertEquals
(
result
,
self
.
today
)
def
test_today2
(
self
):
result
=
RelativeDate
.
relative_date_to_date
(
'tod'
)
self
.
assertEquals
(
result
,
self
.
today
)
def
test_tomorrow1
(
self
):
result
=
RelativeDate
.
relative_date_to_date
(
'Tomorrow'
)
self
.
assertEquals
(
result
,
self
.
tomorrow
)
def
test_tomorrow2
(
self
):
result
=
RelativeDate
.
relative_date_to_date
(
'tom'
)
self
.
assertEquals
(
result
,
self
.
tomorrow
)
def
test_monday1
(
self
):
result
=
RelativeDate
.
relative_date_to_date
(
'monday'
)
self
.
assertEquals
(
result
,
self
.
monday
)
def
test_monday2
(
self
):
result
=
RelativeDate
.
relative_date_to_date
(
'mo'
)
self
.
assertEquals
(
result
,
self
.
monday
)
def
test_monday3
(
self
):
result
=
RelativeDate
.
relative_date_to_date
(
'mon'
)
self
.
assertEquals
(
result
,
self
.
monday
)
def
test_monday4
(
self
):
result
=
RelativeDate
.
relative_date_to_date
(
'mondayy'
)
self
.
assertFalse
(
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