<commit_before>package org.ossim.omar

import java.math.MathContext
import java.text.SimpleDateFormat

import javax.servlet.http.HttpServletRequest

import org.codehaus.groovy.grails.web.servlet.mvc.GrailsParameterMap

/**
 * Created by IntelliJ IDEA.
 * User: sbortman
 * Date: Jun 16, 2008
 * Time: 1:58:06 PM
 * To change this template use File | Settings | File Templates.
 */

class DateUtil
{
  def rng = new Random()

  def createDateBetweenYears(def startYear, def endYear)
  {
    def year = startYear + rng.nextInt(endYear - startYear + 1)
    def month = rng.nextInt(12)

    def calendar = new GregorianCalendar(year, month, 1);
    def day = rng.nextInt(calendar.getActualMaximum(Calendar.DAY_OF_MONTH) + 1)

    def hour = rng.nextInt(24)
    def minute = rng.nextInt(60)
    def second = rng.nextInt(60)
    def millisecond = rng.nextInt(1000)

    def date = Date.parse("yyyy-MM-dd HH:mm:ss.SSS",
        "${year}-${month}-${day} ${hour}:${minute}:${second}.${millisecond}")

    return date
  }

  public static Date parseDate(String dateString)
  {
    TimeZone utc = null
    SimpleDateFormat sdf = null
    Date date = null
    switch ( dateString )
    {
      case ~/[0-9]{4}[0-1][0-9][0-3][0-9]/:
        sdf = new SimpleDateFormat("yyyyMMdd");
//        println "one: ${dateString}"
        break
      case ~/[0-9]{4}-[0-1][0-9]-[0-3][0-9]/:
        sdf = new SimpleDateFormat("yyyy-MM-dd");
        //println "one: ${dateString}"
        break
      case ~/[0-9]{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9]Z/:
        utc = TimeZone.getTimeZone("UTC");
        sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        //println "two: ${dateString}"
        break
      case ~/[0-9]{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9].[0-9]{3}Z/:
        utc = TimeZone.getTimeZone("UTC");
        sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        //println "three: ${dateString}"
        break
      case ~/[0-9]{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5]?[0-9].[0-9]{1,}Z/:
        def x = dateString.split('T')
        def y = x[0].split('-')
        def z = (x[1] - 'Z').split(':')
        def r = new BigDecimal(z[2]).round(new MathContext(5)) as String

        if ( r.size() == 1 )
        r = "00.000"

        z[2] = r
        dateString = "${y[0]}-${y[1]}-${y[2]}T${z[0]}:${z[1]}:${z[2]}Z"
        utc = TimeZone.getTimeZone("UTC");
        sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        //println "four: ${dateString}"
        break
    }

    if ( sdf )
    {
      if ( utc )
      {
        sdf.setTimeZone(utc);
      }

      date = sdf.parse(dateString)
    }

    return date
  }

  static parseDateGivenFormats(String dateString, def dateFormats=null)
  {
	def date = null

    if ( !dateFormats )
    {
      dateFormats = [
		  "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
          "MM'/'DD'/'yyyy HH:mm:ss",
          "yyyyMMdd'T'HH:mm:ss",
          "yyyyMMdd'T'HH:mm:ss.ssss",
		  "EEE MMM dd HH:mm:ss ZZZ yyyy"
      ]
    }

    if ( dateString )
    {
      	for ( def dateFormat in dateFormats )
		{
			try
      		{
        		date = Date.parse(dateFormat, dateString)

        		if ( date )
        		{
          			break
        		}
      		}
      		catch (Exception e)
      		{
       println "Cannot parse ${dateString}: using ${dateFormat}"
      		}
		}
	}

    return date
  }

  public static Date initializeDate(String dateField, Map params)
  {
    Date date = null

    if ( params[dateField] )
    {
      if ( params[dateField] ==~ "(date.)?struct" )
      {
		println "STRUCT ${params[dateField]}"
	    def paramMap = new GrailsParameterMap(params, {} as HttpServletRequest)
		date = paramMap.getProperty(dateField)		
      }
      else if(params[dateField] instanceof Date)
      {
		println "DATE ${params[dateField]}"
        date = params[dateField]
      }
      else if(params[dateField] instanceof String)
      {
		println "STRING ${params[dateField]}"
        date = parseDateGivenFormats(params[dateField])
      }
    }

	if ( date && params["${dateField}_timezone"])
	{
		def tz = params["${dateField}_timezone"]
		println "TZ ${tz} "
		date = setTimeZoneForDate( date, TimeZone.getTimeZone(tz))
	}
	
    return date
  }

  public static Date rollToEndOfDay(Date inputDate)
  {
    Date outputDate = null

    // Change the time portion of the date
    // to be the end of the day. 23:59:59.999
    if ( inputDate )
    {
      def cal = Calendar.instance
      cal.time = inputDate
      cal.set(Calendar.HOUR, 23)
      cal.set(Calendar.MINUTE, 59)
      cal.set(Calendar.SECOND, 59)
      cal.set(Calendar.MILLISECOND, 999)
      outputDate = cal.time<commit_msg>commented out debugging<commit_after>package org.ossim.omar

import java.math.MathContext
import java.text.SimpleDateFormat

import javax.servlet.http.HttpServletRequest

import org.codehaus.groovy.grails.web.servlet.mvc.GrailsParameterMap

/**
 * Created by IntelliJ IDEA.
 * User: sbortman
 * Date: Jun 16, 2008
 * Time: 1:58:06 PM
 * To change this template use File | Settings | File Templates.
 */

class DateUtil
{
  def rng = new Random()

  def createDateBetweenYears(def startYear, def endYear)
  {
    def year = startYear + rng.nextInt(endYear - startYear + 1)
    def month = rng.nextInt(12)

    def calendar = new GregorianCalendar(year, month, 1);
    def day = rng.nextInt(calendar.getActualMaximum(Calendar.DAY_OF_MONTH) + 1)

    def hour = rng.nextInt(24)
    def minute = rng.nextInt(60)
    def second = rng.nextInt(60)
    def millisecond = rng.nextInt(1000)

    def date = Date.parse("yyyy-MM-dd HH:mm:ss.SSS",
        "${year}-${month}-${day} ${hour}:${minute}:${second}.${millisecond}")

    return date
  }

  public static Date parseDate(String dateString)
  {
    TimeZone utc = null
    SimpleDateFormat sdf = null
    Date date = null
    switch ( dateString )
    {
      case ~/[0-9]{4}[0-1][0-9][0-3][0-9]/:
        sdf = new SimpleDateFormat("yyyyMMdd");
//        println "one: ${dateString}"
        break
      case ~/[0-9]{4}-[0-1][0-9]-[0-3][0-9]/:
        sdf = new SimpleDateFormat("yyyy-MM-dd");
        //println "one: ${dateString}"
        break
      case ~/[0-9]{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9]Z/:
        utc = TimeZone.getTimeZone("UTC");
        sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        //println "two: ${dateString}"
        break
      case ~/[0-9]{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9].[0-9]{3}Z/:
        utc = TimeZone.getTimeZone("UTC");
        sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        //println "three: ${dateString}"
        break
      case ~/[0-9]{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5]?[0-9].[0-9]{1,}Z/:
        def x = dateString.split('T')
        def y = x[0].split('-')
        def z = (x[1] - 'Z').split(':')
        def r = new BigDecimal(z[2]).round(new MathContext(5)) as String

        if ( r.size() == 1 )
        r = "00.000"

        z[2] = r
        dateString = "${y[0]}-${y[1]}-${y[2]}T${z[0]}:${z[1]}:${z[2]}Z"
        utc = TimeZone.getTimeZone("UTC");
        sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        //println "four: ${dateString}"
        break
    }

    if ( sdf )
    {
      if ( utc )
      {
        sdf.setTimeZone(utc);
      }

      date = sdf.parse(dateString)
    }

    return date
  }

  static parseDateGivenFormats(String dateString, def dateFormats=null)
  {
	def date = null

    if ( !dateFormats )
    {
      dateFormats = [
		  "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
          "MM'/'DD'/'yyyy HH:mm:ss",
          "yyyyMMdd'T'HH:mm:ss",
          "yyyyMMdd'T'HH:mm:ss.ssss",
		  "EEE MMM dd HH:mm:ss ZZZ yyyy"
      ]
    }

    if ( dateString )
    {
      	for ( def dateFormat in dateFormats )
		{
			try
      		{
        		date = Date.parse(dateFormat, dateString)

        		if ( date )
        		{
          			break
        		}
      		}
      		catch (Exception e)
      		{
//       println "Cannot parse ${dateString}: using ${dateFormat}"
      		}
		}
	}

    return date
  }

  public static Date initializeDate(String dateField, Map params)
  {
    Date date = null

    if ( params[dateField] )
    {
      if ( params[dateField] ==~ "(date.)?struct" )
      {
//		println "STRUCT ${params[dateField]}"
	    def paramMap = new GrailsParameterMap(params, {} as HttpServletRequest)
		date = paramMap.getProperty(dateField)		
      }
      else if(params[dateField] instanceof Date)
      {
//		println "DATE ${params[dateField]}"
        date = params[dateField]
      }
      else if(params[dateField] instanceof String)
      {
//		println "STRING ${params[dateField]}"
        date = parseDateGivenFormats(params[dateField])
      }
    }

	if ( date && params["${dateField}_timezone"])
	{
		def tz = params["${dateField}_timezone"]
//		println "TZ ${tz} "
		date = setTimeZoneForDate( date, TimeZone.getTimeZone(tz))
	}
	
    return date
  }

  public static Date rollToEndOfDay(Date inputDate)
  {
    Date outputDate = null

    // Change the time portion of the date
    // to be the end of the day. 23:59:59.999
    if ( inputDate )
    {
      def cal = Calendar.instance
      cal.time = inputDate
      cal.set(Calendar.HOUR, 23)
      cal.set(Calendar.MINUTE, 59)
      cal.set(Calendar.SECOND, 59)
      cal.set(Calendar.MILLISECOND, 999)
      outputDate = cal.time
{'fraction_non_alphanumeric': 0.1692438042787545, 'fraction_numerical': 0.03346748570218174, 'mean_word_length': 2.335570469798658, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2367300', 'n_tokens_mistral': 3742, 'n_tokens_neox': 3528, 'n_words': 663}