def presentation_datetime(val): """ Convert input value into DAS presentation datetime format (YYYY-MM-DD HH:MM:SS) """ das_format = "%Y-%m-%d %H:%M:%S" # isoformat, see datetime.isoformat() value = str(val) pat = date_yyyymmdd_pattern pat2 = unix_time_pattern pat3 = rr_time_pattern if pat.match(value): # we accept YYYYMMDD res = "%s-%s-%s 00:00:00" % (value[:4], value[4:6], value[6:8]) elif pat2.match(value): res = time.strftime(das_format, time.gmtime(val)) elif pat3.match(value): dformat = "%a %d-%m-%y %H:%M:%S" # Sun 15-05-11 17:25:00 tup = time.strptime(value.split('.')[0], dformat) res = time.strftime(das_format, tup) else: msg = 'Unacceptable date format, value=%s, type=%s' % (val, type(val)) raise Exception(msg) if das_time_pattern.match(res): return res else: msg = 'Fail to convert input value="%s" into DAS date format res="%s"' \ % (val, res) raise Exception(msg)
def test_fix_times(self): "Test fix_dates function" for val in [20110101, time.time(), 'Sun 20-03-11 03:11:16']: row = {'file': [{'creation_time': val}, {'foo': 1}]} fix_times(row) val = row['file'][0]['creation_time'] res = True if das_time_pattern.match(val) else False self.assertEqual(res, True)