def assertTimeStampsAlmostEqual(self, first, second=None, **kwargs):
        """ Test that *first* (a datetime.datetime object) is close in time to *second*.

        kwargs contains arguments for constructing a timedelta object, which defines how
        close the two datetimes should be.

        For example, passing seconds=10, minutes=2 will test that the two dates are within
        2 minutes and 10 seconds of each other.

        By default, the function is run with a constructor of seconds=10, and compared to the
        current time.

        """

        if not second:
            second = datetime.datetime.now()

        defaults = {'seconds':10}
        defaults.update(kwargs)
        td = datetime.timedelta(**defaults)

        if first == second:
            return
        elif first < second and first + td >= second:
            return
        elif first > second and second + td >= first:
            return
        else:
            msg_start = 'Timestamps Not Almost Equal: '
            human_readable_delta = ', '.join(['%s %s'%(v, k) for k,v in defaults.iteritems()])
            msg_main = '%s not within %s of %s.'%(iso8601.format_utc_date(first),
                                                  human_readable_delta,
                                                  iso8601.format_utc_date(second))
            raise self.failureException('%s%s' % (msg_start, msg_main))
Example #2
0
 def default(self, o):
     if isinstance(o, datetime.datetime) or isinstance(o, datetime.time):
         return format_utc_date(o)
     elif isinstance(o, datetime.date):
         return format_utc_date(o, date_only=True)
     else:
         return super(IndivoJSONEncoder, self).default(o)
    def assertTimeStampsAlmostEqual(self, first, second=None, **kwargs):
        """ Test that *first* (a datetime.datetime object) is close in time to *second*.

        kwargs contains arguments for constructing a timedelta object, which defines how
        close the two datetimes should be.

        For example, passing seconds=10, minutes=2 will test that the two dates are within
        2 minutes and 10 seconds of each other.

        By default, the function is run with a constructor of seconds=10, and compared to the
        current time.

        """

        if not second:
            second = datetime.datetime.now()

        defaults = {'seconds':10}
        defaults.update(kwargs)
        td = datetime.timedelta(**defaults)

        if first == second:
            return
        elif first < second and first + td >= second:
            return
        elif first > second and second + td >= first:
            return
        else:
            msg_start = 'Timestamps Not Almost Equal: '
            human_readable_delta = ', '.join(['%s %s'%(v, k) for k,v in defaults.iteritems()])
            msg_main = '%s not within %s of %s.'%(iso8601.format_utc_date(first),
                                                  human_readable_delta,
                                                  iso8601.format_utc_date(second))
            raise self.failureException('%s%s' % (msg_start, msg_main))
Example #4
0
 def default(self, o):
     if isinstance(o, datetime.datetime) or isinstance(o, datetime.time):
         return format_utc_date(o)
     elif isinstance(o, datetime.date):
         return format_utc_date(o, date_only=True) 
     else:
         return super(IndivoJSONEncoder, self).default(o)
    def handle_field(self, obj, field):
        """
        Called to handle each field on an object (except for ForeignKeys and
        ManyToManyFields)
        """
        field_element = etree.Element("Field", name=field.name)

        # Get a "string version" of the object's data.
        if getattr(obj, field.name) is not None:
            value = field._get_val_from_obj(obj)
            if isinstance(value, datetime.datetime) or isinstance(value, datetime.time):
                value = format_utc_date(value)
            elif isinstance(value, datetime.date):
                value = format_utc_date(value, date_only=True)
            elif isinstance(value, bool):
                value = 'true' if value else 'false'
            else:
                 value = field.value_to_string(obj)
            field_element.text = value
        
        self.current.append(field_element)
    def handle_field(self, obj, field):
        """
        Called to handle each field on an object (except for ForeignKeys and
        ManyToManyFields)
        """
        field_element = etree.Element("Field", name=field.name)

        # Get a "string version" of the object's data.
        if getattr(obj, field.name) is not None:
            value = field._get_val_from_obj(obj)
            if isinstance(value, datetime.datetime) or isinstance(
                    value, datetime.time):
                value = format_utc_date(value)
            elif isinstance(value, datetime.date):
                value = format_utc_date(value, date_only=True)
            elif isinstance(value, bool):
                value = 'true' if value else 'false'
            else:
                value = field.value_to_string(obj)
            field_element.text = value

        self.current.append(field_element)
Example #7
0
def format_iso8601_date(timestamp):
  if timestamp:
    return iso8601.format_utc_date(timestamp, date_only = True)
  else:
    return ""
Example #8
0
def format_iso8601_datetime(timestamp):
  if timestamp:
    return iso8601.format_utc_date(timestamp)
  else:
    return ""
def format_iso8601_date(timestamp):
    if timestamp:
        return iso8601.format_utc_date(timestamp, date_only=True)
    else:
        return ""
def format_iso8601_datetime(timestamp):
    if timestamp:
        return iso8601.format_utc_date(timestamp)
    else:
        return ""