Serialize given datetime to timestamp.
5750
def _serialize(self, value, attr, obj, **kwargs):
        """"""
        if value is None:
            return None

        return int(datetime_to_timestamp(value))
205487
def deserialize_time(data):
    """"""
    parsed = parser.parse(data)
    return parsed.time().replace(tzinfo=parsed.tzinfo)
647030
def current_timestamp(self) -> datetime:
        """"""
        timestamp = DB.get_hash_value(self._key, 'current_timestamp')
        return datetime_from_isoformat(timestamp)
266050
def timestamp(self, timestamp):
        """"""
        if timestamp is None:
            self._timestamp = datetime.utcnow()
        elif isinstance(timestamp, datetime):
            self._timestamp = timestamp
        else:
            self._timestamp = datetime.strptime(timestamp, fmt)
904760
def parse_timestamp(x):
    """"""
    dt = dateutil.parser.parse(x)
    if dt.tzinfo is None:
        dt = dt.replace(tzinfo=pytz.utc)
    return dt
555532
def datetime_to_timestamp(dt):
    """"""
    delta = dt - datetime.utcfromtimestamp(0)
    return delta.seconds + delta.days * 24 * 3600
1098190
def str_to_datetime(timestamp, str_format='%Y-%m-%d_%H:%M:%S.fff'):
    
    date_time = datetime.strptime(timestamp.split('.')[0],
                                  str_format.split('.')[0])
    if '.' in timestamp:
        date_time += timedelta(seconds=eval('0.%s' % timestamp.split('.')[1]))
    if DEFAULT_TIMEZONE_AWARE:
        date_time = date_time.replace(
            tzinfo=psycopg2.tz.FixedOffsetTimezone(
                offset=DEFAULT_TIMEZONE * 60, name=None))
    return date_time
265625
def format_timestamp(timestamp):
        
        tz_info = tz.tzutc()
        return datetime.fromtimestamp(timestamp, tz=tz_info).strftime("%Y-%m-%dT%H:%M:%S.000Z")
907660
def deserialize(self, raw_data, **kwargs):
        """"""
        super(DateTimeField, self).deserialize(raw_data, **kwargs)
        return self.converted
1280076
def proto_ts_from_datetime_str(dt):
    

    ts = Timestamp()
    if (dt is not None):
        try:
            ts.FromJsonString(dt)
        except ParseError:
            pass
    return ts
919013
def serialize(self, data):
        
        return json.dumps(self._serialize_datetime(data), ensure_ascii=False)
603914
def timestamp_from_datetime(dt):
    
    dt = dt.replace(tzinfo=utc)
    if hasattr(dt, "timestamp") and callable(dt.timestamp):
        return dt.replace(tzinfo=utc).timestamp()
    return (dt - datetime(1970, 1, 1, tzinfo=utc)).total_seconds()
1153096
def pretty_timestamp(timestamp, date_format='%a-%m_%d_%y:%H:%M:%S'):
    """"""
    return time.strftime(date_format,
                         time.localtime(int(str(timestamp)[:10])))
4825
def json_datetime_serial(obj):
    """"""
    if isinstance(obj, (datetime, date)):
        serial = obj.isoformat()
        return serial

    if ObjectId is not None and isinstance(obj, ObjectId):
        # TODO: try to use bson.json_util instead
        return str(obj)

    raise TypeError("Type not serializable")
757933
def from_timestamp(timestamp, tz_offset):
    """"""
    utc_dt = datetime.fromtimestamp(timestamp, utc)
    try:
        local_dt = utc_dt.astimezone(tzoffset(tz_offset))
        return local_dt
    except ValueError:
        return utc_dt
807697
def get_timestamp(dt=None):
    '''
    
    '''

    if dt is None: dt = datetime.datetime.utcnow()
    t = dt.utctimetuple()

    return calendar.timegm(t)
271746
def serialize(obj):
    
    LOGGER.debug('serialize(%s)', obj)

    if isinstance(obj, datetime.date):
        return simplejson.dumps(obj, default=encoders.as_date)

    elif hasattr(obj, '__dict__'):
        return simplejson.dumps(obj, default=encoders.as_object)

    return simplejson.dumps(obj)