# Path: floyd/util/dateformat.py
# class LocalTimezone(tzinfo):
#     "Proxy timezone information from time module."
#     def __init__(self, dt):
#         tzinfo.__init__(self)
#         self._tzname = self.tzname(dt)
# 
#     def __repr__(self):
#         return smart_str(self._tzname)
# 
#     def utcoffset(self, dt):
#         if self._isdst(dt):
#             return timedelta(seconds=-time.altzone)
#         else:
#             return timedelta(seconds=-time.timezone)
# 
#     def dst(self, dt):
#         if self._isdst(dt):
#             return timedelta(seconds=-time.altzone) - timedelta(seconds=-time.timezone)
#         else:
#             return timedelta(0)
# 
#     def tzname(self, dt):
#         try:
#             return force_unicode(time.tzname[self._isdst(dt)], 'utf-8')
#         except UnicodeDecodeError:
#             return None
# 
#     def _isdst(self, dt):
#         tt = (dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.weekday(), 0, -1)
#         try:
#             stamp = time.mktime(tt)
#         except (OverflowError, ValueError):
#             # 32 bit systems can't handle dates after Jan 2038, and certain
#             # systems can't handle dates before ~1901-12-01:
#             #
#             # >>> time.mktime((1900, 1, 13, 0, 0, 0, 0, 0, 0))
#             # OverflowError: mktime argument out of range
#             # >>> time.mktime((1850, 1, 13, 0, 0, 0, 0, 0, 0))
#             # ValueError: year out of range
#             #
#             # In this case, we fake the date, because we only care about the
#             # DST flag.
#             tt = (2037,) + tt[1:]
#             stamp = time.mktime(tt)
#         tt = time.localtime(stamp)
#         return tt.tm_isdst > 0
# 
# Path: floyd/util/translation.py
# def ungettext(singular, plural, number):
#   return force_unicode(ngettext(singular, plural, number))
# 
# def ugettext(str, format=unicode):
#   return force_unicode(str)


import datetime
import time
from floyd.util.dateformat import LocalTimezone
from floyd.util.translation import ungettext, ugettext

def timesince(d, now=None):
    """
    Takes two datetime objects and returns the time between d and now
    as a nicely formatted string, e.g. "10 minutes".  If d occurs after now,
    then "0 minutes" is returned.

    Units used are years, months, weeks, days, hours, and minutes.
    Seconds and microseconds are ignored.  Up to two adjacent units will be
    displayed.  For example, "2 weeks, 3 days" and "1 year, 3 months" are
    possible outputs, but "2 weeks, 3 hours" and "1 year, 5 days" are not.

    Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since
    """
    chunks = (
      (60 * 60 * 24 * 365, lambda n: ungettext('year', 'years', n)),
      (60 * 60 * 24 * 30, lambda n: ungettext('month', 'months', n)),
      (60 * 60 * 24 * 7, lambda n : ungettext('week', 'weeks', n)),
      (60 * 60 * 24, lambda n : ungettext('day', 'days', n)),
      (60 * 60, lambda n: ungettext('hour', 'hours', n)),
      (60, lambda n: ungettext('minute', 'minutes', n))
    )
    # Convert datetime.date to datetime.datetime for comparison.
    if not isinstance(d, datetime.datetime):
        d = datetime.datetime(d.year, d.month, d.day)
    if now and not isinstance(now, datetime.datetime):
        now = datetime.datetime(now.year, now.month, now.day)

    if not now:
        if d.tzinfo:

# Path: floyd/util/dateformat.py
# class LocalTimezone(tzinfo):
#     "Proxy timezone information from time module."
#     def __init__(self, dt):
#         tzinfo.__init__(self)
#         self._tzname = self.tzname(dt)
# 
#     def __repr__(self):
#         return smart_str(self._tzname)
# 
#     def utcoffset(self, dt):
#         if self._isdst(dt):
#             return timedelta(seconds=-time.altzone)
#         else:
#             return timedelta(seconds=-time.timezone)
# 
#     def dst(self, dt):
#         if self._isdst(dt):
#             return timedelta(seconds=-time.altzone) - timedelta(seconds=-time.timezone)
#         else:
#             return timedelta(0)
# 
#     def tzname(self, dt):
#         try:
#             return force_unicode(time.tzname[self._isdst(dt)], 'utf-8')
#         except UnicodeDecodeError:
#             return None
# 
#     def _isdst(self, dt):
#         tt = (dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.weekday(), 0, -1)
#         try:
#             stamp = time.mktime(tt)
#         except (OverflowError, ValueError):
#             # 32 bit systems can't handle dates after Jan 2038, and certain
#             # systems can't handle dates before ~1901-12-01:
#             #
#             # >>> time.mktime((1900, 1, 13, 0, 0, 0, 0, 0, 0))
#             # OverflowError: mktime argument out of range
#             # >>> time.mktime((1850, 1, 13, 0, 0, 0, 0, 0, 0))
#             # ValueError: year out of range
#             #
#             # In this case, we fake the date, because we only care about the
#             # DST flag.
#             tt = (2037,) + tt[1:]
#             stamp = time.mktime(tt)
#         tt = time.localtime(stamp)
#         return tt.tm_isdst > 0
# 
# Path: floyd/util/translation.py
# def ungettext(singular, plural, number):
#   return force_unicode(ngettext(singular, plural, number))
# 
# def ugettext(str, format=unicode):
#   return force_unicode(str)


# Path: floyd/util/timesince.py
import datetime
import time
from floyd.util.dateformat import LocalTimezone
from floyd.util.translation import ungettext, ugettext


def timesince(d, now=None):
    """
    Takes two datetime objects and returns the time between d and now
    as a nicely formatted string, e.g. "10 minutes".  If d occurs after now,
    then "0 minutes" is returned.

    Units used are years, months, weeks, days, hours, and minutes.
    Seconds and microseconds are ignored.  Up to two adjacent units will be
    displayed.  For example, "2 weeks, 3 days" and "1 year, 3 months" are
    possible outputs, but "2 weeks, 3 hours" and "1 year, 5 days" are not.

    Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since
    """
    chunks = (
      (60 * 60 * 24 * 365, lambda n: ungettext('year', 'years', n)),
      (60 * 60 * 24 * 30, lambda n: ungettext('month', 'months', n)),
      (60 * 60 * 24 * 7, lambda n : ungettext('week', 'weeks', n)),
      (60 * 60 * 24, lambda n : ungettext('day', 'days', n)),
      (60 * 60, lambda n: ungettext('hour', 'hours', n)),
      (60, lambda n: ungettext('minute', 'minutes', n))
    )
    # Convert datetime.date to datetime.datetime for comparison.
    if not isinstance(d, datetime.datetime):
        d = datetime.datetime(d.year, d.month, d.day)
    if now and not isinstance(now, datetime.datetime):
        now = datetime.datetime(now.year, now.month, now.day)

    if not now:
        if d.tzinfo:

            now = datetime.datetime.now(LocalTimezone(d))