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

Convert dates to datetimes for DateHourParameter #2285

Merged
merged 1 commit into from Nov 16, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions luigi/parameter.py
Expand Up @@ -479,6 +479,12 @@ def serialize(self, dt):
return str(dt)
return dt.strftime(self.date_format)

@staticmethod
def _convert_to_dt(dt):
if not isinstance(dt, datetime.datetime):
dt = datetime.datetime.combine(dt, datetime.time.min)
return dt

def normalize(self, dt):
"""
Clamp dt to every Nth :py:attr:`~_DatetimeParameterBase.interval` starting at
Expand All @@ -487,6 +493,8 @@ def normalize(self, dt):
if dt is None:
return None

dt = self._convert_to_dt(dt)

dt = dt.replace(microsecond=0) # remove microseconds, to avoid float rounding issues.
delta = (dt - self.start).total_seconds()
granularity = (self._timedelta * self.interval).total_seconds()
Expand Down
4 changes: 4 additions & 0 deletions test/date_parameter_test.py
Expand Up @@ -70,6 +70,10 @@ def test_parse(self):
dh = luigi.DateHourParameter().parse('2013-02-01T18')
self.assertEqual(dh, datetime.datetime(2013, 2, 1, 18, 0, 0))

def test_date_to_dh(self):
date = luigi.DateHourParameter().normalize(datetime.date(2000, 1, 1))
self.assertEqual(date, datetime.datetime(2000, 1, 1, 0))

def test_serialize(self):
dh = luigi.DateHourParameter().serialize(datetime.datetime(2013, 2, 1, 18, 0, 0))
self.assertEqual(dh, '2013-02-01T18')
Expand Down