def long_to_datetime(x):
"""Converts a long integer representing the number of microseconds since
``datetime.min`` to a datetime object.
"""
days = x // 86400000000 # Microseconds in a day
x -= days * 86400000000
seconds = x // 1000000 # Microseconds in a second
x -= seconds * 1000000
return datetime.min + timedelta(days=days, seconds=seconds, microseconds=x)
# Ambiguous datetime object
python类min()的实例源码
def last_updated(self):
doc = next(self.query(properties=[self.lu_field]).sort(
[(self.lu_field, pymongo.DESCENDING)]).limit(1), None)
# Handle when collection has docs but `NoneType` lu_field.
return (self.lu_func[0](doc[self.lu_field]) if (doc and doc[self.lu_field])
else datetime.min)
def tslice(unit, start=ben(), end=None, step=1, count=float('inf')):
"""
tslice(unit, start=None, end=None, step=1, count=None) -> generator of Blackhole object
unit in ['year', 'month', 'day', 'hour', 'minute', 'second', 'microsecond']
this is some kind xrange-like
:param unit:
:param start:
:param end:
:param step:
:param count:
:return:
"""
if unit not in Blackhole._units:
raise AttributeError()
if isinstance(start, basestring):
start = ben(start)
if isinstance(end, basestring):
end = ben(end)
cnt = 0
if step > 0:
end = end or ben(datetime.max)
while start < end and cnt < count:
yield start
start = start.shifted(**{unit: step})
cnt += 1
elif step < 0:
end = end or ben(datetime.min)
while start > end and cnt < count:
yield start
start = start.shifted(**{unit: step})
cnt += 1
def testTimestamp(self):
query = self.db.query
for datestyle in ('ISO', 'Postgres, MDY', 'Postgres, DMY',
'SQL, MDY', 'SQL, DMY', 'German'):
self.db.set_parameter('datestyle', datestyle)
d = datetime(2016, 3, 14)
q = "select $1::timestamp"
r = query(q, (d,)).getresult()[0][0]
self.assertIsInstance(r, datetime)
self.assertEqual(r, d)
d = datetime(2016, 3, 14, 15, 9, 26)
q = "select $1::timestamp"
r = query(q, (d,)).getresult()[0][0]
self.assertIsInstance(r, datetime)
self.assertEqual(r, d)
d = datetime(2016, 3, 14, 15, 9, 26, 535897)
q = "select $1::timestamp"
r = query(q, (d,)).getresult()[0][0]
self.assertIsInstance(r, datetime)
self.assertEqual(r, d)
q = ("select '10000-08-01 AD'::timestamp,"
" '0099-01-08 BC'::timestamp")
r = query(q).getresult()[0]
self.assertIsInstance(r[0], datetime)
self.assertIsInstance(r[1], datetime)
self.assertEqual(r[0], datetime.max)
self.assertEqual(r[1], datetime.min)
q = "select 'infinity'::timestamp, '-infinity'::timestamp"
r = query(q).getresult()[0]
self.assertIsInstance(r[0], datetime)
self.assertIsInstance(r[1], datetime)
self.assertEqual(r[0], datetime.max)
self.assertEqual(r[1], datetime.min)
def testTimestamptz(self):
query = self.db.query
timezones = dict(CET=1, EET=2, EST=-5, UTC=0)
for timezone in sorted(timezones):
tz = '%+03d00' % timezones[timezone]
try:
tzinfo = datetime.strptime(tz, '%z').tzinfo
except ValueError: # Python < 3.2
tzinfo = pg._get_timezone(tz)
self.db.set_parameter('timezone', timezone)
for datestyle in ('ISO', 'Postgres, MDY', 'Postgres, DMY',
'SQL, MDY', 'SQL, DMY', 'German'):
self.db.set_parameter('datestyle', datestyle)
d = datetime(2016, 3, 14, tzinfo=tzinfo)
q = "select $1::timestamptz"
r = query(q, (d,)).getresult()[0][0]
self.assertIsInstance(r, datetime)
self.assertEqual(r, d)
d = datetime(2016, 3, 14, 15, 9, 26, tzinfo=tzinfo)
q = "select $1::timestamptz"
r = query(q, (d,)).getresult()[0][0]
self.assertIsInstance(r, datetime)
self.assertEqual(r, d)
d = datetime(2016, 3, 14, 15, 9, 26, 535897, tzinfo)
q = "select $1::timestamptz"
r = query(q, (d,)).getresult()[0][0]
self.assertIsInstance(r, datetime)
self.assertEqual(r, d)
q = ("select '10000-08-01 AD'::timestamptz,"
" '0099-01-08 BC'::timestamptz")
r = query(q).getresult()[0]
self.assertIsInstance(r[0], datetime)
self.assertIsInstance(r[1], datetime)
self.assertEqual(r[0], datetime.max)
self.assertEqual(r[1], datetime.min)
q = "select 'infinity'::timestamptz, '-infinity'::timestamptz"
r = query(q).getresult()[0]
self.assertIsInstance(r[0], datetime)
self.assertIsInstance(r[1], datetime)
self.assertEqual(r[0], datetime.max)
self.assertEqual(r[1], datetime.min)
def cast_timestamptz(value, connection):
"""Cast a timestamptz value."""
if value == '-infinity':
return datetime.min
if value == 'infinity':
return datetime.max
value = value.split()
if value[-1] == 'BC':
return datetime.min
fmt = connection.date_format()
if fmt.endswith('-%Y') and len(value) > 2:
value = value[1:]
if len(value[3]) > 4:
return datetime.max
fmt = ['%d %b' if fmt.startswith('%d') else '%b %d',
'%H:%M:%S.%f' if len(value[2]) > 8 else '%H:%M:%S', '%Y']
value, tz = value[:-1], value[-1]
else:
if fmt.startswith('%Y-'):
tz = _re_timezone.match(value[1])
if tz:
value[1], tz = tz.groups()
else:
tz = '+0000'
else:
value, tz = value[:-1], value[-1]
if len(value[0]) > 10:
return datetime.max
fmt = [fmt, '%H:%M:%S.%f' if len(value[1]) > 8 else '%H:%M:%S']
if _has_timezone:
value.append(_timezone_as_offset(tz))
fmt.append('%z')
return datetime.strptime(' '.join(value), ' '.join(fmt))
return datetime.strptime(' '.join(value), ' '.join(fmt)).replace(
tzinfo=_get_timezone(tz))
def cast_timestamptz(value, connection):
"""Cast a timestamptz value."""
if value == '-infinity':
return datetime.min
if value == 'infinity':
return datetime.max
value = value.split()
if value[-1] == 'BC':
return datetime.min
fmt = connection.date_format()
if fmt.endswith('-%Y') and len(value) > 2:
value = value[1:]
if len(value[3]) > 4:
return datetime.max
fmt = ['%d %b' if fmt.startswith('%d') else '%b %d',
'%H:%M:%S.%f' if len(value[2]) > 8 else '%H:%M:%S', '%Y']
value, tz = value[:-1], value[-1]
else:
if fmt.startswith('%Y-'):
tz = _re_timezone.match(value[1])
if tz:
value[1], tz = tz.groups()
else:
tz = '+0000'
else:
value, tz = value[:-1], value[-1]
if len(value[0]) > 10:
return datetime.max
fmt = [fmt, '%H:%M:%S.%f' if len(value[1]) > 8 else '%H:%M:%S']
if _has_timezone:
value.append(_timezone_as_offset(tz))
fmt.append('%z')
return datetime.strptime(' '.join(value), ' '.join(fmt))
return datetime.strptime(' '.join(value), ' '.join(fmt)).replace(
tzinfo=_get_timezone(tz))
def __init__(self, batch_name, sd, mimetype_files=None):
self.sd = sd
self.batch = batch_name
self.log_fp = None
self.num_files = 0
self.total_time = 0
self.min_time = timedelta.max
self.max_time = timedelta.min
self.earliest_time = datetime.max
self.latest_time = datetime.min
self.queue = self.sd.get_obj('output_queue')
self.domain = self.sd.get_obj('output_domain')
def calculateID(self, file_name_fullpath):
# Get the creation date for the first PersistenceItem in the audit (they will all be the same)
instanceID = datetime.min
tmp_instanceID = None
try:
file_object = loadFile(file_name_fullpath)
root = ET.parse(file_object).getroot()
file_object.close()
reg_key = root.find('PersistenceItem')
reg_modified = reg_key.get('created')
try:
tmp_instanceID = datetime.strptime(reg_modified, "%Y-%m-%dT%H:%M:%SZ")
except ValueError as e:
tmp_instanceID = datetime.max
logger.warning("Invalid reg_modified date found!: %s (%s)" % (reg_modified, file_name_fullpath))
instanceID = tmp_instanceID
except Exception:
traceback.print_exc(file=sys.stdout)
# If we found no PersistenceItem date we go with plan B (but most probably this is corrupt and will fail later)
if instanceID is None:
file_object = loadFile(file_name_fullpath)
content = file_object.read()
instanceID = hashlib.md5(content).hexdigest()
file_object.close()
return instanceID
def calculateID(self, file_name_fullpath):
# Get the creation date for the first PersistenceItem in the audit (they will all be the same)
instanceID = datetime.min
tmp_instanceID = None
try:
file_object = loadFile(file_name_fullpath)
root = ET.parse(file_object).getroot()
file_object.close()
reg_key = root.find('AppCompatItemExtended')
reg_modified = reg_key.get('created')
try:
tmp_instanceID = datetime.strptime(reg_modified, "%Y-%m-%dT%H:%M:%SZ")
except ValueError as e:
tmp_instanceID = datetime.max
logger.warning("Invalid reg_modified date found!: %s (%s)" % (reg_modified, file_name_fullpath))
instanceID = tmp_instanceID
except Exception:
traceback.print_exc(file=sys.stdout)
# If we found no PersistenceItem date we go with plan B (but most probably this is corrupt and will fail later)
if instanceID is None:
file_object = loadFile(file_name_fullpath)
content = file_object.read()
instanceID = hashlib.md5(content).hexdigest()
file_object.close()
return instanceID
appcompat_mirregistryaudit.py 文件源码
项目:appcompatprocessor
作者: mbevilacqua
项目源码
文件源码
阅读 30
收藏 0
点赞 0
评论 0
def calculateID(self, file_name_fullpath):
instanceID = datetime.min
tmp_instanceID = None
try:
file_object = loadFile(file_name_fullpath)
root = ET.parse(file_object).getroot()
file_object.close()
for reg_key in root.findall('RegistryItem'):
tmp_reg_key = reg_key.find('Modified')
if tmp_reg_key is not None:
reg_modified = tmp_reg_key.text
try:
tmp_instanceID = datetime.strptime(reg_modified, "%Y-%m-%dT%H:%M:%SZ")
except ValueError as e:
tmp_instanceID = datetime.max
logger.warning("Invalid reg_modified date found!: %s (%s)" % (reg_modified, file_name_fullpath))
if instanceID < tmp_instanceID:
instanceID = tmp_instanceID
else:
logger.warning("Found RegistryItem with no Modified date (Mir bug?): %s" % file_name_fullpath)
except Exception:
logger.exception("Error on calculateID for: %s" % file_name_fullpath)
# If we found no Modified date in any of the RegistryItems we go with plan B (but most probably ShimCacheParser will fail to parse anyway)
if instanceID is None:
file_object = loadFile(file_name_fullpath)
content = file_object.read()
instanceID = hashlib.md5(content).hexdigest()
file_object.close()
return instanceID
def calculateID(self, file_name_fullpath):
instanceID = datetime.min
tmp_instanceID = None
try:
file_object = loadFile(file_name_fullpath)
root = ET.parse(file_object).getroot()
file_object.close()
for reg_key in root.findall('RegistryItem'):
tmp_reg_key = reg_key.find('Modified')
if tmp_reg_key is not None:
reg_modified = tmp_reg_key.text
try:
tmp_instanceID = datetime.strptime(reg_modified, "%Y-%m-%dT%H:%M:%SZ")
except ValueError as e:
tmp_instanceID = datetime.max
logger.warning("Invalid reg_modified date found!: %s (%s)" % (reg_modified, file_name_fullpath))
if instanceID < tmp_instanceID:
instanceID = tmp_instanceID
else:
logger.warning("Found RegistryItem with no Modified date (Mir bug?): %s" % file_name_fullpath)
except Exception:
logger.exception("Error on calculateID for: %s" % file_name_fullpath)
# If we found no Modified date in any of the RegistryItems we go with plan B (but most probably ShimCacheParser will fail to parse anyway)
if instanceID is None:
file_object = loadFile(file_name_fullpath)
content = file_object.read()
instanceID = hashlib.md5(content).hexdigest()
file_object.close()
return instanceID
def calculateID(self, file_name_fullpath):
# Get the creation date for the first PersistenceItem in the audit (they will all be the same)
instanceID = datetime.min
tmp_instanceID = None
try:
file_object = loadFile(file_name_fullpath)
root = ET.parse(file_object).getroot()
file_object.close()
reg_key = root.find('AmCacheItem')
reg_modified = reg_key.get('created')
try:
tmp_instanceID = datetime.strptime(reg_modified, "%Y-%m-%dT%H:%M:%SZ")
except ValueError as e:
tmp_instanceID = datetime.max
logger.warning("Invalid reg_modified date found!: %s (%s)" % (reg_modified, file_name_fullpath))
instanceID = tmp_instanceID
except Exception:
traceback.print_exc(file=sys.stdout)
# If we found no PersistenceItem date we go with plan B (but most probably this is corrupt and will fail later)
if instanceID is None:
file_object = loadFile(file_name_fullpath)
content = file_object.read()
instanceID = hashlib.md5(content).hexdigest()
file_object.close()
return instanceID
def make_windows_timestamp_value_getter(value_name):
"""
return a function that fetches the value from the registry key
as a Windows timestamp.
"""
f = make_value_getter(value_name)
def _value_getter(key):
try:
if key[0].get_value_by_name(value_name) != None:
return parse_windows_timestamp(key[0].get_value_by_name(value_name).get_data_as_integer() or 0)
else: return datetime.min
except ValueError:
return datetime.min
return _value_getter
def make_unix_timestamp_value_getter(value_name):
"""
return a function that fetches the value from the registry key
as a UNIX timestamp.
"""
f = make_value_getter(value_name)
def _value_getter(key):
try:
if key[0].get_value_by_name(value_name) != None:
return parse_unix_timestamp(key[0].get_value_by_name(value_name).get_data_as_integer() or 0)
else: return datetime.min
except ValueError:
return datetime.min
return _value_getter
def datetime_to_long(dt):
"""Converts a datetime object to a long integer representing the number
of microseconds since ``datetime.min``.
"""
return timedelta_to_usecs(dt.replace(tzinfo=None) - dt.min)
def long_to_datetime(x):
"""Converts a long integer representing the number of microseconds since
``datetime.min`` to a datetime object.
"""
days = x // 86400000000 # Microseconds in a day
x -= days * 86400000000
seconds = x // 1000000 # Microseconds in a second
x -= seconds * 1000000
return datetime.min + timedelta(days=days, seconds=seconds, microseconds=x)
# Ambiguous datetime object
def find_date_range(self):
date_min = datetime.max
date_max = datetime.min
for tweet in self.get_collection_iterators():
date_to_process = datetime.strptime(tweet['created_at'],'%a %b %d %H:%M:%S +0000 %Y')
if date_to_process <= date_min:
date_min = date_to_process
if date_to_process >= date_max:
date_max = date_to_process
return {"date_min":date_min,"date_max":date_max}
def find_date_range(self):
date_min = datetime.max
date_max = datetime.min
for tweet in self.collection.get_iterator():
date_to_process = datetime.strptime(tweet['created_at'],'%a %b %d %H:%M:%S +0000 %Y')
if date_to_process <= date_min:
date_min = date_to_process
if date_to_process >= date_max:
date_max = date_to_process
return {"date_min":date_min,"date_max":date_max}
def datetime_to_long(dt):
"""Converts a datetime object to a long integer representing the number
of microseconds since ``datetime.min``.
"""
return timedelta_to_usecs(dt.replace(tzinfo=None) - dt.min)