def guess_date_format(self, date_column): """Try to guess the date format used in the current ``.csv`` file, by reading from the first row of the ``date_column`` column. """ infile = open(self.csv_file, 'r') reader = csv.DictReader(infile) row = reader.next() infile.close() # Return the guessed format return dates.guess_format(row[date_column])
def test_guess_format(self): """Test the `dates.guess_format` function. """ date_formats = [ ('2010/01/28 12:34:56 PM', '%Y/%m/%d %I:%M:%S %p'), ('01/28/10 1:25:49 PM', '%m/%d/%y %I:%M:%S %p'), ('01/28/2010 13:25:49.123', '%m/%d/%Y %H:%M:%S.%f'), # Consider supporting alternative separators #('2010-01-28 12:34:56 PM', '%Y-%m-%d %I:%M:%S %p'), ] for date, format in date_formats: self.assertEqual(dates.guess_format(date), format)