Skip to content

Commit

Permalink
Think this closes #10 and adds Python 3 support?
Browse files Browse the repository at this point in the history
  • Loading branch information
zachwill committed Jun 3, 2016
1 parent 7c77c15 commit 66b6b03
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
16 changes: 13 additions & 3 deletions moment/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
import calendar
from datetime import datetime, timedelta

from .utils import _iteritems


# ----------------------------------------------------------------------------
# Utilities
# ----------------------------------------------------------------------------

def add_month(date, number):
"""Add a number of months to a date."""
Expand All @@ -26,6 +32,10 @@ def update_month(date, month):
return date.replace(year=year, month=month, day=day)


# ----------------------------------------------------------------------------
# Main functionality
# ----------------------------------------------------------------------------

class MutableDate(object):
"""Incapsulate mutable dates in one class."""

Expand All @@ -35,7 +45,7 @@ def __init__(self, date):
def add(self, key=None, amount=None, **kwds):
"""Add time to the original moment."""
if not key and not amount and len(kwds):
for k, v in kwds.iteritems():
for k, v in _iteritems(kwds):
self.add(k, v)
if key == 'years' or key == 'year':
self._date = add_month(self._date, amount * 12)
Expand Down Expand Up @@ -64,7 +74,7 @@ def sub(self, key=None, amount=None, **kwds):
def subtract(self, key=None, amount=None, **kwds):
"""Subtract time from the original moment."""
if not key and not amount and len(kwds):
for k, v in kwds.iteritems():
for k, v in _iteritems(kwds):
self.subtract(k, v)
if key == 'years' or key == 'year':
self._date = subtract_month(self._date, amount * 12)
Expand All @@ -88,7 +98,7 @@ def subtract(self, key=None, amount=None, **kwds):

def replace(self, **kwds):
"""A Pythonic way to replace various date attributes."""
for key, value in kwds.iteritems():
for key, value in _iteritems(kwds):
if key == 'years' or key == 'year':
self._date = self._date.replace(year=value)
elif key == 'months' or key == 'month':
Expand Down
3 changes: 2 additions & 1 deletion moment/parse.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime
from .utils import STRING_TYPES


def parse_date_and_formula(*args):
Expand All @@ -13,7 +14,7 @@ def parse_date_and_formula(*args):
# Python datetime needs the month and day, too.
date = [date[0], 1, 1]
date = datetime(*date)
elif isinstance(date, str) or isinstance(date, unicode):
elif isinstance(date, STRING_TYPES):
if formula is None:
formula = "%Y-%m-%d"
date = datetime.strptime(date, formula)
Expand Down
16 changes: 16 additions & 0 deletions moment/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
Added for Python 3 compatibility.
"""

import sys


STRING_TYPES = (basestring, ) if sys.version_info < (3, 0) else (str, )


def _iteritems(data):
"For Python 3 support."
if sys.version_info < (3, 0):
return data.iteritems()
else:
return data.items()

0 comments on commit 66b6b03

Please sign in to comment.