# Path: ob2/util/time.py
# def now_str():
#     """Gets the current datetime in a serializable format (for database)."""
#     return format_time(now())
# 
# def format_time(s):
#     """Converts a datetime object to serializable format."""
#     return s.isoformat()
# 
# def parse_time(s):
#     """Converts serializable datetime to a datetime object."""
#     return DateParser.parse(s)
# 
# def parse_to_relative(target, past_relative_cutoff=86400, future_relative_cutoff=-86400):
#     """
#     Expresses TARGET as it is relative to now(). If the target is more than Y seconds in the past
#     or more than Z seconds in the future, then express TARGET in an absolute way.
# 
#     """
#     if isinstance(target, basestring):
#         target = parse_time(target)
#     delta = now() - target
#     total_seconds = delta.total_seconds()
#     # Example: Jul 9 8:00PM
#     extended_date_format = "%b %-d %-I:%M%p"
#     if total_seconds < -6 * 30.5 * 86400 or total_seconds > 6 * 30.5 * 86400:
#         extended_date_format += " %Y"
#     if total_seconds > past_relative_cutoff or total_seconds < -abs(future_relative_cutoff):
#         return target.strftime(extended_date_format)
#     return delta_to_relative(total_seconds)


import datetime
import pytz
from mock import patch
from unittest2 import TestCase
from ob2.util.time import (
    now_str,
    format_time,
    parse_time,
    parse_to_relative,
)
        self.assertEqual("1 minute ago", parse_to_relative("Jul 3 2015 23:58:59 UTC"))
        self.assertEqual("1 minute ago", parse_to_relative("Jul 3 2015 23:58:01 UTC"))
        self.assertEqual("2 minutes ago", parse_to_relative("Jul 3 2015 23:58:00 UTC"))
        self.assertEqual("59 minutes ago", parse_to_relative("Jul 3 2015 23:00:01 UTC"))
        self.assertEqual("1 hour ago", parse_to_relative("Jul 3 2015 23:00:00 UTC"))
        self.assertEqual("1 hour ago", parse_to_relative("Jul 3 2015 22:00:01 UTC"))
        self.assertEqual("2 hours ago", parse_to_relative("Jul 3 2015 22:00:00 UTC"))
        self.assertEqual("23 hours ago", parse_to_relative("Jul 3 2015 00:00:01 UTC", **kw))
        self.assertEqual("1 day ago", parse_to_relative("Jul 3 2015 00:00:00 UTC", **kw))
        self.assertEqual("1 day ago", parse_to_relative("Jul 2 2015 00:00:01 UTC", **kw))
        self.assertEqual("2 days ago", parse_to_relative("Jul 2 2015 00:00:00 UTC", **kw))
        self.assertEqual("1 month ago", parse_to_relative("Jun 2 2015 00:00:00 UTC", **kw))
        self.assertEqual("5 months ago", parse_to_relative("Jan 20 2015 00:00:00 UTC", **kw))
        self.assertEqual("10 months ago", parse_to_relative("Aug 20 2014 00:00:00 UTC", **kw))
        self.assertEqual("11 months ago", parse_to_relative("Jul 20 2014 00:00:10 UTC", **kw))
        self.assertEqual("1 year ago", parse_to_relative("Jun 20 2014 00:00:10 UTC", **kw))
        self.assertEqual("1 year ago", parse_to_relative("Jul 20 2013 00:00:10 UTC", **kw))
        self.assertEqual("2 years ago", parse_to_relative("Jun 20 2013 00:00:10 UTC", **kw))

        self.assertEqual("59 seconds from now", parse_to_relative("Jul 4 2015 00:00:59 UTC"))
        self.assertEqual("1 minute from now", parse_to_relative("Jul 4 2015 00:01:00 UTC"))
        self.assertEqual("1 minute from now", parse_to_relative("Jul 4 2015 00:01:59 UTC"))
        self.assertEqual("2 minutes from now", parse_to_relative("Jul 4 2015 00:02:00 UTC"))
        self.assertEqual("59 minutes from now", parse_to_relative("Jul 4 2015 00:59:59 UTC"))
        self.assertEqual("1 hour from now", parse_to_relative("Jul 4 2015 01:00:00 UTC"))
        self.assertEqual("1 hour from now", parse_to_relative("Jul 4 2015 01:59:59 UTC"))
        self.assertEqual("2 hours from now", parse_to_relative("Jul 4 2015 02:00:00 UTC"))
        self.assertEqual("23 hours from now", parse_to_relative("Jul 4 2015 23:59:59 UTC", **kw))
        self.assertEqual("1 day from now", parse_to_relative("Jul 5 2015 00:00:00 UTC", **kw))
        self.assertEqual("1 day from now", parse_to_relative("Jul 5 2015 23:59:59 UTC", **kw))

# Path: ob2/util/time.py
# def now_str():
#     """Gets the current datetime in a serializable format (for database)."""
#     return format_time(now())
# 
# def format_time(s):
#     """Converts a datetime object to serializable format."""
#     return s.isoformat()
# 
# def parse_time(s):
#     """Converts serializable datetime to a datetime object."""
#     return DateParser.parse(s)
# 
# def parse_to_relative(target, past_relative_cutoff=86400, future_relative_cutoff=-86400):
#     """
#     Expresses TARGET as it is relative to now(). If the target is more than Y seconds in the past
#     or more than Z seconds in the future, then express TARGET in an absolute way.
# 
#     """
#     if isinstance(target, basestring):
#         target = parse_time(target)
#     delta = now() - target
#     total_seconds = delta.total_seconds()
#     # Example: Jul 9 8:00PM
#     extended_date_format = "%b %-d %-I:%M%p"
#     if total_seconds < -6 * 30.5 * 86400 or total_seconds > 6 * 30.5 * 86400:
#         extended_date_format += " %Y"
#     if total_seconds > past_relative_cutoff or total_seconds < -abs(future_relative_cutoff):
#         return target.strftime(extended_date_format)
#     return delta_to_relative(total_seconds)


# Path: ob2/tests/util_tests/time_test.py
import datetime
import pytz
from mock import patch
from unittest2 import TestCase
from ob2.util.time import (
    now_str,
    format_time,
    parse_time,
    parse_to_relative,
)

        self.assertEqual("1 minute ago", parse_to_relative("Jul 3 2015 23:58:59 UTC"))
        self.assertEqual("1 minute ago", parse_to_relative("Jul 3 2015 23:58:01 UTC"))
        self.assertEqual("2 minutes ago", parse_to_relative("Jul 3 2015 23:58:00 UTC"))
        self.assertEqual("59 minutes ago", parse_to_relative("Jul 3 2015 23:00:01 UTC"))
        self.assertEqual("1 hour ago", parse_to_relative("Jul 3 2015 23:00:00 UTC"))
        self.assertEqual("1 hour ago", parse_to_relative("Jul 3 2015 22:00:01 UTC"))
        self.assertEqual("2 hours ago", parse_to_relative("Jul 3 2015 22:00:00 UTC"))
        self.assertEqual("23 hours ago", parse_to_relative("Jul 3 2015 00:00:01 UTC", **kw))
        self.assertEqual("1 day ago", parse_to_relative("Jul 3 2015 00:00:00 UTC", **kw))
        self.assertEqual("1 day ago", parse_to_relative("Jul 2 2015 00:00:01 UTC", **kw))
        self.assertEqual("2 days ago", parse_to_relative("Jul 2 2015 00:00:00 UTC", **kw))
        self.assertEqual("1 month ago", parse_to_relative("Jun 2 2015 00:00:00 UTC", **kw))
        self.assertEqual("5 months ago", parse_to_relative("Jan 20 2015 00:00:00 UTC", **kw))
        self.assertEqual("10 months ago", parse_to_relative("Aug 20 2014 00:00:00 UTC", **kw))
        self.assertEqual("11 months ago", parse_to_relative("Jul 20 2014 00:00:10 UTC", **kw))
        self.assertEqual("1 year ago", parse_to_relative("Jun 20 2014 00:00:10 UTC", **kw))
        self.assertEqual("1 year ago", parse_to_relative("Jul 20 2013 00:00:10 UTC", **kw))
        self.assertEqual("2 years ago", parse_to_relative("Jun 20 2013 00:00:10 UTC", **kw))

        self.assertEqual("59 seconds from now", parse_to_relative("Jul 4 2015 00:00:59 UTC"))
        self.assertEqual("1 minute from now", parse_to_relative("Jul 4 2015 00:01:00 UTC"))
        self.assertEqual("1 minute from now", parse_to_relative("Jul 4 2015 00:01:59 UTC"))
        self.assertEqual("2 minutes from now", parse_to_relative("Jul 4 2015 00:02:00 UTC"))
        self.assertEqual("59 minutes from now", parse_to_relative("Jul 4 2015 00:59:59 UTC"))
        self.assertEqual("1 hour from now", parse_to_relative("Jul 4 2015 01:00:00 UTC"))
        self.assertEqual("1 hour from now", parse_to_relative("Jul 4 2015 01:59:59 UTC"))
        self.assertEqual("2 hours from now", parse_to_relative("Jul 4 2015 02:00:00 UTC"))
        self.assertEqual("23 hours from now", parse_to_relative("Jul 4 2015 23:59:59 UTC", **kw))
        self.assertEqual("1 day from now", parse_to_relative("Jul 5 2015 00:00:00 UTC", **kw))
        self.assertEqual("1 day from now", parse_to_relative("Jul 5 2015 23:59:59 UTC", **kw))

        self.assertEqual("2 days from now", parse_to_relative("Jul 6 2015 00:00:00 UTC", **kw))