def _to_ts(val):
if isinstance(val, (int, float)):
return val
elif isinstance(val, (datetime, date)):
return (val-EPOCH).total_seconds()
v = TS_RE.match(val)
if v:
return float(v.group(0))
v = DT_RE.match(val)
if v:
# get all of the passed datetime pieces, don't worry about them being
# terribly valid, the datetime constructor will handle that ;)
v = list(filter(None, v.groups()))
if len(v) == 7:
# truncate to microseconds as necessary
v[6] = v[6][:6]
# extend to microseconds as necessary
v[6] += (6 - len(v[6])) * '0'
v = list(map(int, v))
dt = datetime(*v)
return (dt-EPOCH).total_seconds()
raise Exception("Value %r is not a timestamp, datetime, or date"%(val,))
评论列表
文章目录