Skip to content

Commit

Permalink
Fix bug to handle aware datetime object correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
hrtshu committed Jun 10, 2017
1 parent 82a938d commit bf583ec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
17 changes: 11 additions & 6 deletions dtround/dtround.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def _base_func(dt, unit, base, round):
raise TypeError('`unit` must be type of `datetime.timedelta`')
if base is None:
base = dt.__class__(1970, 1, 1)
if type(dt) == datetime:
base = base.replace(tzinfo=dt.tzinfo)
elif type(dt) != type(base):
raise TypeError('`dt` and `base` must be same types')
return base + _td_mul(unit, round(_td_div((dt - base), unit)))
Expand All @@ -44,8 +46,9 @@ def floor(dt, unit=timedelta(days=1), base=None):
rounding-down unit. Default is `datetime.timedelta(days=1)`.
base : datetime.datetime or datetime.date, optional
base datetime/date object. Default is `None`. If `None`,
`datetime.datetime(1970, 1, 1)`/`datetime.date(1970, 1, 1)` is used for
this. This type must be the same as the type of `dt`
`datetime.datetime(1970, 1, 1, tzinfo=dt.tzinfo)`/`datetime.date(1970,
1, 1)` is used for this. This type must be the same as the type of
`dt`.
Returns
-------
Expand Down Expand Up @@ -95,8 +98,9 @@ def ceil(dt, unit=timedelta(days=1), base=None):
rounding-up unit. Default is `datetime.timedelta(days=1)`.
base : datetime.datetime or datetime.date, optional
base datetime/date object. Default is `None`. If `None`,
`datetime.datetime(1970, 1, 1)`/`datetime.date(1970, 1, 1)` is used for
this. This type must be the same as the type of `dt`
`datetime.datetime(1970, 1, 1, tzinfo=dt.tzinfo)`/`datetime.date(1970,
1, 1)` is used for this. This type must be the same as the type of
`dt`.
Returns
-------
Expand Down Expand Up @@ -146,8 +150,9 @@ def round(dt, unit=timedelta(days=1), base=None):
rounding-off unit. Default is `datetime.timedelta(days=1)`.
base : datetime.datetime or datetime.date, optional
base datetime/date object. Default is `None`. If `None`,
`datetime.datetime(1970, 1, 1)`/`datetime.date(1970, 1, 1)` is used for
this. This type must be the same as the type of `dt`
`datetime.datetime(1970, 1, 1, tzinfo=dt.tzinfo)`/`datetime.date(1970,
1, 1)` is used for this. This type must be the same as the type of
`dt`.
Returns
-------
Expand Down
2 changes: 1 addition & 1 deletion dtround/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

__version__ = '0.1.0'
__version__ = '0.1.1'

0 comments on commit bf583ec

Please sign in to comment.