Convert a Stripe API timestamp response (unix epoch) to a native datetime.

	:rtype: datetime
15766
def convert_tstamp(response):
	
	if response is None:
		# Allow passing None to convert_tstamp()
		return response

	# Overrides the set timezone to UTC - I think...
	tz = timezone.utc if settings.USE_TZ else None

	return datetime.datetime.fromtimestamp(response, tz)
1345500
def strip_datetime(value):
        
        if isinstance(value, basestring):
            try:
                return parse_datetime(value)
            except ValueError:
                return
        elif isinstance(value, integer_types):
            try:
                return datetime.datetime.utcfromtimestamp(value / 1e3)
            except (ValueError, OverflowError, OSError):
                return
440901
def _datetime_to_timestamp(self, v):
        

        # stole from https://docs.python.org/3/library/datetime.html#datetime.datetime.timestamp
        if timezone.is_aware(v):
            return (v - timezone.datetime(1970, 1, 1, tzinfo=timezone.utc)).total_seconds()
        else:
            return (v - timezone.datetime(1970, 1, 1)).total_seconds()
347918
def _convert_datetime_str(response):
    '''
    
    '''
    if response:
        return dict([(k, '{0}'.format(v)) if isinstance(v, datetime.date) else (k, v) for k, v in six.iteritems(response)])
    return None
917476
def iso_datetime(timestamp=None):
    
    if timestamp is None:
        timestamp = time.time()
    return datetime.datetime.fromtimestamp(timestamp).isoformat(' ')[:19]
1132951
def epoch(ctx, datetime):
    
    return conversions.to_decimal(str(conversions.to_datetime(datetime, ctx).timestamp()), ctx)
507246
def _dfromtimestamp(timestamp):
    
    try:
        return datetime.date.fromtimestamp(timestamp)
    except OSError:
        timestamp -= time.timezone
        d = datetime.date(1970, 1, 1) + datetime.timedelta(seconds=timestamp)
        if _isdst(d):
            timestamp += 3600
            d = datetime.date(1970, 1, 1) + datetime.timedelta(seconds=timestamp)
        return d
32999
def _date_to_unix(arg):
    
    if isinstance(arg, datetime):
        arg = arg.utctimetuple()
    elif isinstance(arg, integer_types + (float,)):
        return int(arg)
    year, month, day, hour, minute, second = arg[:6]
    days = date(year, month, 1).toordinal() - _epoch_ord + day - 1
    hours = days * 24 + hour
    minutes = hours * 60 + minute
    seconds = minutes * 60 + second
    return seconds
7154
def _init_dates(self):
        
        if self.total_transactions == 0:
            return None
        self.epoch_start = Result.select(Result.epoch).order_by(Result.epoch.asc()).limit(1).get().epoch
        self.epoch_finish = Result.select(Result.epoch).order_by(Result.epoch.desc()).limit(1).get().epoch
        self.start_datetime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(self.epoch_start))
        self.finish_datetime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(self.epoch_finish))
1245267
def to_unix(cls, timestamp):
        
        if not isinstance(timestamp, datetime.datetime):
            raise TypeError('Time.milliseconds expects a datetime object')
        base = time.mktime(timestamp.timetuple())
        return base
385569
def timestamp_normalized(self):
        
        if not self.timestamp:
            return None

        if isinstance(self.timestamp, six.integer_types):
            return self.timestamp

        if isinstance(self.timestamp, timedelta):
            tmp = datetime.now() + self.timestamp
        else:
            tmp = self.timestamp

        return int(time.mktime(tmp.timetuple()) * 1e+6 + tmp.microsecond)
27828
def convert_datetime_type(obj):
    ''' 

    '''
    # Pandas NaT
    if pd and obj is pd.NaT:
        return np.nan

    # Pandas Period
    if pd and isinstance(obj, pd.Period):
        return obj.to_timestamp().value / 10**6.0

    # Pandas Timestamp
    if pd and isinstance(obj, _pd_timestamp): return obj.value / 10**6.0

    # Pandas Timedelta
    elif pd and isinstance(obj, pd.Timedelta): return obj.value / 10**6.0

    # Datetime (datetime is a subclass of date)
    elif isinstance(obj, dt.datetime):
        diff = obj.replace(tzinfo=None) - DT_EPOCH
        return diff.total_seconds() * 1000.

    # Date
    elif isinstance(obj, dt.date):
        return (dt.datetime(*obj.timetuple()[:6]) - DT_EPOCH).total_seconds() * 1000

    # NumPy datetime64
    elif isinstance(obj, np.datetime64):
        epoch_delta = obj - NP_EPOCH
        return (epoch_delta / NP_MS_DELTA)

    # Time
    elif isinstance(obj, dt.time):
        return (obj.hour * 3600 + obj.minute * 60 + obj.second) * 1000 + obj.microsecond / 1000.
207578
def paypaltime2datetime(s):
    """"""
    naive = datetime.datetime.strptime(s, PayPalNVP.TIMESTAMP_FORMAT)
    if not settings.USE_TZ:
        return naive
    else:
        # TIMESTAMP_FORMAT is UTC
        return timezone.make_aware(naive, timezone.utc)
437136
def _since_epoch(cdate):
    

    if len(cdate) < 29:  # somethings broken
        if len(cdate) < 5:
            return utc_now()

    cdate = cdate[5:] # assume short weekday, i.e. do not support obsolete RFC 1036 date format
    t = -1
    for time_format in TIME_FORMAT :
        try:
            t = time.strptime(cdate, time_format)   # e.g. 18-Apr-2014 12:30:51 GMT
        except ValueError:
            pass
        else:
            break

    if t == -1:
        raise (Exception,
               'ValueError: Date "{0}" does not match any of: {1}'.format(
                   cdate,TIME_FORMAT))

    return calendar.timegm(t)
807697
def get_timestamp(dt=None):
    '''
    
    '''

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

    return calendar.timegm(t)
1086853
def get_from_gnucash26_date(date_str: str) -> date:
    
    date_format = "%Y%m%d"
    result = datetime.strptime(date_str, date_format).date()
    return result
786319
def _convert_asset_timestamp_fields(dict_):
    
    for key in _asset_timestamp_fields & viewkeys(dict_):
        value = pd.Timestamp(dict_[key], tz='UTC')
        dict_[key] = None if isnull(value) else value
    return dict_