<commit_before>/*******************************************************************************
 * Copyright (c) 2004, 2005 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

package org.eclipse.birt.core.data;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.birt.core.exception.BirtException;

import com.ibm.icu.text.SimpleDateFormat;

/**
 * DateFormatISO8601 is a utility class for formatting and parsing dates
 * according to date format defined by ISO8601.
 */

public class DateFormatISO8601
{

	// all SimpleDateFormatter of ICU
	private static Map simpleDateFormatterMap = null;

	static
	{
		// date format pattern defined in ISO8601
		// notice the order is significant.
		String[] dateFormatPattern = {
				"yyyy-MM-dd HH:mm:ss.S z",
				"yyyy-MM-dd HH:mm:ss.Sz",
				"yyyy-MM-dd HH:mm:ss.S",
				"yyyy-MM-dd HH:mm:ss z",
				"yyyy-MM-dd HH:mm:ssz",
				"yyyy-MM-dd HH:mm:ss",
				"yyyy-MM-dd HH:mm z",
				"yyyy-MM-dd HH:mmz",
				"yyyy-MM-dd HH:mm",
				"yyyy-MM-dd",
				"yyyy-MM",
				"yyyy"
		};
		SimpleDateFormat simpleDateFormatter = null;
		PatternKey patterKey = null;

		simpleDateFormatterMap = new HashMap( 64 );
		for ( int i = 0; i < dateFormatPattern.length; i++ )
		{
			patterKey = getPatterKey( dateFormatPattern[i] );
			simpleDateFormatter = new SimpleDateFormat( dateFormatPattern[i] );
			simpleDateFormatter.setLenient( false );
//			simpleDateFormatter.setCalendar( Calendar.getInstance( TimeZone.getTimeZone( "GMT" ) ) );
			simpleDateFormatterMap.put( patterKey, simpleDateFormatter );
		}
	}

	/**
	 * Parse a date/time string.
	 * @param source
	 * @return
	 * @throws ParseException
	 */
	public static Date parse( String source ) throws BirtException,
			ParseException
	{
		if( source == null )
		{
			return null;
		}
		Date resultDate = null;
		source = cleanDate( source );
		Object simpleDateFormatter = simpleDateFormatterMap.get( getPatterKey( source ) );
		if ( simpleDateFormatter != null )
		{
			try
			{
				resultDate = ( (SimpleDateFormat) simpleDateFormatter ).parse( source );
				return resultDate;
			}
			catch ( ParseException e1 )
			{
			}
		}
		// for the String can not be parsed, throws a BirtException
		if ( resultDate == null )
		{
			throw new ParseException( "an not convert the value of " + source,
					0 );
		}

		// never access here
		return resultDate;
	}
	
	/**
	 * 
	 * @param s
	 * @return
	 */
	private static String cleanDate( String s )
	{
		s = s.trim( );
		if ( s.indexOf( 'T' ) < 12 )
		{
			s = s.replaceFirst( "T", " " );
		}
		
		int zoneIndex = s.indexOf( "GMT" );
		if( zoneIndex > 0 )
		{
			return s.substring( 0, zoneIndex );
		}
		zoneIndex = s.indexOf( 'Z' );
		if ( zoneIndex == s.length( ) - 1 )
		{
			return s.substring( 0, zoneIndex );
		}
		zoneIndex = s.indexOf( '+' );
		if ( zoneIndex > 0 )
		{
			return s.substring( 0, zoneIndex );
		}
		
		return s;
	}

	/**
	 * 
	 * @param source
	 * @return
	 */
	private static PatternKey getPatterKey( String source )
	{
		int colonNumber = 0;
		int blankNumber = 0;
		int hyphenNumber = 0;<commit_msg>fix a liittle bug in DataFormatISO8601 class<commit_after>/*******************************************************************************
 * Copyright (c) 2004, 2005 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

package org.eclipse.birt.core.data;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.birt.core.exception.BirtException;

import com.ibm.icu.text.SimpleDateFormat;

/**
 * DateFormatISO8601 is a utility class for formatting and parsing dates
 * according to date format defined by ISO8601.
 */

public class DateFormatISO8601
{

	// all SimpleDateFormatter of ICU
	private static Map simpleDateFormatterMap = null;

	static
	{
		// date format pattern defined in ISO8601
		// notice the order is significant.
		String[] dateFormatPattern = {
				"yyyy-MM-dd HH:mm:ss.S z",
				"yyyy-MM-dd HH:mm:ss.Sz",
				"yyyy-MM-dd HH:mm:ss.S",
				"yyyy-MM-dd HH:mm:ss z",
				"yyyy-MM-dd HH:mm:ssz",
				"yyyy-MM-dd HH:mm:ss",
				"yyyy-MM-dd HH:mm z",
				"yyyy-MM-dd HH:mmz",
				"yyyy-MM-dd HH:mm",
				"yyyy-MM-dd",
				"yyyy-MM",
				"yyyy"
		};
		SimpleDateFormat simpleDateFormatter = null;
		PatternKey patterKey = null;

		simpleDateFormatterMap = new HashMap( 64 );
		for ( int i = 0; i < dateFormatPattern.length; i++ )
		{
			patterKey = getPatterKey( dateFormatPattern[i] );
			simpleDateFormatter = new SimpleDateFormat( dateFormatPattern[i] );
			simpleDateFormatter.setLenient( false );
//			simpleDateFormatter.setCalendar( Calendar.getInstance( TimeZone.getTimeZone( "GMT" ) ) );
			simpleDateFormatterMap.put( patterKey, simpleDateFormatter );
		}
	}

	/**
	 * Parse a date/time string.
	 * @param source
	 * @return
	 * @throws ParseException
	 */
	public static Date parse( String source ) throws BirtException,
			ParseException
	{
		if( source == null )
		{
			return null;
		}
		Date resultDate = null;
		source = cleanDate( source );
		Object simpleDateFormatter = simpleDateFormatterMap.get( getPatterKey( source ) );
		if ( simpleDateFormatter != null )
		{
			try
			{
				resultDate = ( (SimpleDateFormat) simpleDateFormatter ).parse( source );
				return resultDate;
			}
			catch ( ParseException e1 )
			{
			}
		}
		// for the String can not be parsed, throws a BirtException
		if ( resultDate == null )
		{
			throw new ParseException( "an not convert the value of " + source,
					0 );
		}

		// never access here
		return resultDate;
	}
	
	/**
	 * 
	 * @param s
	 * @return
	 */
	private static String cleanDate( String s )
	{
		s = s.trim( );
		if ( s.indexOf( 'T' ) < 12 )
		{
			s = s.replaceFirst( "T", " " );
		}
		
		int zoneIndex = s.indexOf( "GMT" );
		if( zoneIndex > 0 )
		{
			return s.substring( 0, zoneIndex ).trim( );
		}
		zoneIndex = s.indexOf( 'Z' );
		if ( zoneIndex == s.length( ) - 1 )
		{
			return s.substring( 0, zoneIndex ).trim( );
		}
		zoneIndex = s.indexOf( '+' );
		if ( zoneIndex > 0 )
		{
			return s.substring( 0, zoneIndex ).trim( );
		}
		
		return s;
	}

	/**
	 * 
	 * @param source
	 * @return
	 */
	private static PatternKey getPatterKey( String source )
	{
		int colonNumber = 0;
		int blankNumber = 0;
		int hyphenNumber = 0;
{'fraction_non_alphanumeric': 0.161829251026476, 'fraction_numerical': 0.013025626504318278, 'mean_word_length': 3.2223550508069336, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6650802', 'n_tokens_mistral': 2685, 'n_tokens_neox': 2237, 'n_words': 616}