<commit_before>package org.relique.jdbc.csv;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringConverter {

	private String dateFormat;
	private String timeFormat;
	private GregorianCalendar calendar;
	private Pattern timestampPattern;

	public StringConverter(String dateformat, String timeformat, String timeZoneName){
		dateFormat = dateformat;
		timeFormat = timeformat;
		TimeZone timeZone = TimeZone.getTimeZone(timeZoneName);
		calendar = new GregorianCalendar();
		calendar.clear();
		calendar.setTimeZone(timeZone);
		timestampPattern = Pattern.compile("([0-9][0-9][0-9][0-9])-([0-9]?[0-9])-([0-9]?[0-9])[ T]([0-9]?[0-9]):([0-9]?[0-9]):([0-9]?[0-9]).*");
	}

	public String parseString(String str) {
		return str;
	}

	public boolean parseBoolean(String str) {
		return Boolean.valueOf(str).booleanValue();
	}

	public byte parseByte(String str) {
		try {
			return (str == null) ? 0 : Byte.parseByte(str);
		} catch (RuntimeException e) {
			return 0;
		}
	}

	public short parseShort(String str) {
		try {
			return (str == null) ? 0 : Short.parseShort(str);
		} catch (RuntimeException e) {
			return 0;
		}
	}

	public int parseInt(String str) {
		try {
			return (str == null) ? 0 : Integer.parseInt(str);
		} catch (RuntimeException e) {
			return 0;
		}
	}

	public long parseLong(String str) {
		try {
			return (str == null) ? 0 : Long.parseLong(str);
		} catch (RuntimeException e) {
			return 0;
		}
	}

	public float parseFloat(String str) {
		try {
			if(str != null)
				str = str.replace(",", ".");
			return (str == null) ? 0 : Float.parseFloat(str);
		} catch (RuntimeException e) {
			return 0;
		}
	}

	public double parseDouble(String str) {
		try {
			if(str != null)
				str = str.replace(",", ".");
			return (str == null) ? 0 : Double.parseDouble(str);
		} catch (RuntimeException e) {
			return 0;
		}
	}

	public byte[] parseBytes(String str) {
		try {
			return (str == null) ? null : str.getBytes();
		} catch (RuntimeException e) {
			return null;
		}
	}

	public BigDecimal parseBigDecimal(String str) {
		try {
			return (str == null) ? null : new BigDecimal(str);
		} catch (RuntimeException e) {
			return null;
		}
	}

	/**
	 * transforms the date string into its equivalent ISO8601
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	private String makeISODate(String date, String format) {
		// first memorize the original order of the groups.
		format = format.toLowerCase();
		int dpos = format.indexOf('d');
		int mpos = format.indexOf('m');
		int ypos = format.indexOf('y');

		int day = 1, month = 1, year = 1;
		if(dpos > mpos) day+=1; else month+=1;
		if(dpos > ypos) day+=1; else year+=1;
		if(mpos > ypos) month+=1; else year+=1;

		// then build the regular expression
		Pattern part;
		Matcher m;
		
		part = Pattern.compile("d+");
		m = part.matcher(format);
		if (m.find())
			format = format.replace(m.group(), "([0-9]{" + (m.end()-m.start()) + ",2})");
		
		part = Pattern.compile("m+");
		m = part.matcher(format);
		if (m.find())
			format = format.replace(m.group(), "([0-9]{" + (m.end()-m.start()) + ",2})");

		part = Pattern.compile("y+");
		m = part.matcher(format);
		if (m.find())
			format = format.replace(m.group(), "([0-9]{" + (m.end()-m.start()) + ",4})");
		
		format = format + ".*";
		
		Pattern pattern = Pattern.compile(format);
		m = pattern.matcher(date);
		if(m.matches())
			// and return the groups in the ISO8601 order.
			return m.group(year) + "-" + m.group(month) + "-" + m.group(day);
		else
			return "1970-01-01";
	}
	
	public Date parseDate(String str) {
		try {
			Date sqlResult = Date.valueOf(makeISODate(str, dateFormat));
			return sqlResult;
		} catch (RuntimeException e) {<commit_msg>Fixed method StringConverter.makeISODate() to ensure that day and month values have two digits so they can be parsed by java.sql.Date.valueOf().<commit_after>package org.relique.jdbc.csv;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringConverter {

	private String dateFormat;
	private String timeFormat;
	private GregorianCalendar calendar;
	private Pattern timestampPattern;

	public StringConverter(String dateformat, String timeformat, String timeZoneName){
		dateFormat = dateformat;
		timeFormat = timeformat;
		TimeZone timeZone = TimeZone.getTimeZone(timeZoneName);
		calendar = new GregorianCalendar();
		calendar.clear();
		calendar.setTimeZone(timeZone);
		timestampPattern = Pattern.compile("([0-9][0-9][0-9][0-9])-([0-9]?[0-9])-([0-9]?[0-9])[ T]([0-9]?[0-9]):([0-9]?[0-9]):([0-9]?[0-9]).*");
	}

	public String parseString(String str) {
		return str;
	}

	public boolean parseBoolean(String str) {
		return Boolean.valueOf(str).booleanValue();
	}

	public byte parseByte(String str) {
		try {
			return (str == null) ? 0 : Byte.parseByte(str);
		} catch (RuntimeException e) {
			return 0;
		}
	}

	public short parseShort(String str) {
		try {
			return (str == null) ? 0 : Short.parseShort(str);
		} catch (RuntimeException e) {
			return 0;
		}
	}

	public int parseInt(String str) {
		try {
			return (str == null) ? 0 : Integer.parseInt(str);
		} catch (RuntimeException e) {
			return 0;
		}
	}

	public long parseLong(String str) {
		try {
			return (str == null) ? 0 : Long.parseLong(str);
		} catch (RuntimeException e) {
			return 0;
		}
	}

	public float parseFloat(String str) {
		try {
			if(str != null)
				str = str.replace(",", ".");
			return (str == null) ? 0 : Float.parseFloat(str);
		} catch (RuntimeException e) {
			return 0;
		}
	}

	public double parseDouble(String str) {
		try {
			if(str != null)
				str = str.replace(",", ".");
			return (str == null) ? 0 : Double.parseDouble(str);
		} catch (RuntimeException e) {
			return 0;
		}
	}

	public byte[] parseBytes(String str) {
		try {
			return (str == null) ? null : str.getBytes();
		} catch (RuntimeException e) {
			return null;
		}
	}

	public BigDecimal parseBigDecimal(String str) {
		try {
			return (str == null) ? null : new BigDecimal(str);
		} catch (RuntimeException e) {
			return null;
		}
	}

	/**
	 * transforms the date string into its equivalent ISO8601
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	private String makeISODate(String date, String format) {
		// first memorize the original order of the groups.
		format = format.toLowerCase();
		int dpos = format.indexOf('d');
		int mpos = format.indexOf('m');
		int ypos = format.indexOf('y');

		int day = 1, month = 1, year = 1;
		if(dpos > mpos) day+=1; else month+=1;
		if(dpos > ypos) day+=1; else year+=1;
		if(mpos > ypos) month+=1; else year+=1;

		// then build the regular expression
		Pattern part;
		Matcher m;
		
		part = Pattern.compile("d+");
		m = part.matcher(format);
		if (m.find())
			format = format.replace(m.group(), "([0-9]{" + (m.end()-m.start()) + ",2})");
		
		part = Pattern.compile("m+");
		m = part.matcher(format);
		if (m.find())
			format = format.replace(m.group(), "([0-9]{" + (m.end()-m.start()) + ",2})");

		part = Pattern.compile("y+");
		m = part.matcher(format);
		if (m.find())
			format = format.replace(m.group(), "([0-9]{" + (m.end()-m.start()) + ",4})");
		
		format = format + ".*";
		
		Pattern pattern = Pattern.compile(format);
		m = pattern.matcher(date);
		if (m.matches()) {
			// and return the groups in ISO8601 format.
			String yearGroup = m.group(year);
			String monthGroup = m.group(month);
			if (monthGroup.length() < 2)
				monthGroup = "0" + monthGroup;
			String dayGroup = m.group(day);
			if (dayGroup.length() < 2)
				dayGroup = "0" + dayGroup;
			String retval = yearGroup + "-" + monthGroup + "-" + dayGroup;
			return retval;
		}
		else
			return "1970-01-01";
	}
	
	public Date parseDate(String str) {
		try {
			String isoDate = makeISODate(str, dateFormat);
			Date sqlResult = Date.valueOf(isoDate);
			return sqlResult;
		} catch (RuntimeException e) {
{'fraction_non_alphanumeric': 0.1626688402421984, 'fraction_numerical': 0.017699115044247787, 'mean_word_length': 3.5589171974522293, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6809943', 'n_tokens_mistral': 3341, 'n_tokens_neox': 2955, 'n_words': 740}