Timestamp property adapter for couchdbkit


import time, datetime
from couchdbkit.schema.properties import Property
class TimestampProperty(Property):
    
    """
        
    """
    
    @staticmethod
    def now(self):
        return datetime.datetime.now()
    
    def to_python(self, value):
        if isinstance(value, basestring ):
            try:
                value = float(value)
                value = datetime.datetime.fromtimestamp(value)
            except ValueError, e:
                raise e
        elif isinstance( value, float):
            try:                
                value = datetime.datetime.fromtimestamp(value)
            except ValueError, e:
                raise e
        else:
            #if it's not basestring or float... what the hell is it?
            #@TODO remove on acceptance
            raise Exception(value, [(k,getattr(value,k)) for k in dir(value)])
        
        return value
    
    def to_json(self, value):
        if value is None:
            return value
        return time.mktime(value.timetuple())