<commit_before><?php
/**
 * sfDateFormat class file.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the BSD License.
 *
 * Copyright(c) 2004 by <NAME>. All rights reserved.
 *
 * To contact the author write to {@link mailto:<EMAIL> Q<NAME>ue}
 * The latest version of PRADO can be obtained from:
 * {@link http://prado.sourceforge.net/}
 *
 * @author     <NAME> <weizhuo[at]gmail[dot]com>
 * @version    $Id$
 * @package    symfony
 * @subpackage i18n
 */

/**
 * Get the encoding utilities
 */
require_once(dirname(__FILE__).'/util.php');

/**
 * sfDateFormat class.
 * 
 * The sfDateFormat class allows you to format dates and times with 
 * predefined styles in a locale-sensitive manner. Formatting times 
 * with the sfDateFormat class is similar to formatting dates.
 *
 * Formatting dates with the sfDateFormat class is a two-step process. 
 * First, you create a formatter with the getDateInstance method. 
 * Second, you invoke the format method, which returns a string containing 
 * the formatted date. 
 *
 * DateTime values are formatted using standard or custom patterns stored 
 * in the properties of a DateTimeFormatInfo.
 *
 * @author <NAME> <weizhuo[at]gmail[dot]com>
 * @version v1.0, last update on Sat Dec 04 14:10:49 EST 2004
 * @package System.I18N.core
 */
class sfDateFormat
{
  /**
   * A list of tokens and their function call.
   * @var array 
   */
  protected $tokens = array(
    'G'=>'Era',
    'y'=>'year',
    'M'=>'mon',
    'd'=>'mday',
    'h'=>'Hour12',
    'H'=>'hours',
    'm'=>'minutes',
    's'=>'seconds',
    'E'=>'wday',
    'D'=>'yday',
    'F'=>'DayInMonth',
    'w'=>'WeekInYear',
    'W'=>'WeekInMonth',
    'a'=>'AMPM',
    'k'=>'HourInDay',
    'K'=>'HourInAMPM',
    'z'=>'TimeZone'
  );

  /**
   * A list of methods, to be used by the token function calls.
   * @var array 
   */
  protected $methods = array();

  /**
   * The sfDateTimeFormatInfo, containing culture specific patterns and names.
   * @var sfDateTimeFormatInfo   
   */
  protected $formatInfo;

  /**
   * Initialize a new sfDateFormat.
   *
   * @param mixed either, null, a sfCultureInfo instance, a DateTimeFormatInfo instance, or a locale.
   * @return sfDateFormat instance
   */
  function __construct($formatInfo = null)
  {
    if (is_null($formatInfo))
    {
      $this->formatInfo = sfDateTimeFormatInfo::getInvariantInfo();
    }
    else if ($formatInfo instanceof sfCultureInfo)
    {
      $this->formatInfo = $formatInfo->DateTimeFormat;
    }
    else if ($formatInfo instanceof sfDateTimeFormatInfo)
    {
      $this->formatInfo = $formatInfo;
    }
    else
    {
      $this->formatInfo = sfDateTimeFormatInfo::getInstance($formatInfo);
    }

    $this->methods = get_class_methods($this);
  }
  
  /**
   * Guesses a date without calling strtotime.
   *
   * @param mixed the time as integer or string in strtotime format.
   * @param string the input pattern; default is sql date or timestamp
   * @return string formatted date time. 
   */
  public function getDate($time, $pattern = null)
  {    
    // if the type is not a php timestamp
    if (is_string($time))
    {
      if (!$pattern)
      {
        if (strlen($time) == 10)
        {
          $pattern = 'yyyy-MM-dd';
        }
        else   // otherwise, default:
        {
          $pattern = 'yyyy-MM-dd HH:mm:ss';
        }
      }
    
      $tokens = $this->getTokens($pattern);
      $pregPattern = '';
      $matchNames = array();
      foreach($tokens as $token)
      {
        if ($matchName = $this->getFunctionName($token))
        {
          #$pregPattern .= sprintf('(\d{%s})', strlen($token));
          $pregPattern .= '(\d+)';
          $matchNames[] = $matchName;
        }
        else
          $pregPattern .= $token;
      }
      preg_match('@'.$pregPattern.'@', $time, $matches);
      
      array_shift($matches);      
      
      if (count($matchNames) == count($matches))
      {
        $result = array_combine($matchNames, $matches);       
        $date = $result;
      }
    }
    
    // the last attempt has failed we fall back on the default method
    if (!isset($date))
    {
      if (is_string($time))
      {
        $time = @strtotime($time);
      }
      $date = @getdate($time);
    }
    
    // we set default values for the time
    foreach (array('hours', 'minutes', 'seconds') as $timeDiv)
    {
      if (!isset($date[$timeDiv]))
      {
        $date[$timeDiv] = 0;
      }
    }
        
    return $date;
  }

  /**
   * Format a date according to the pattern.
   *
   * @param mixed the time as integer or string in strtotime format.
   * @return string formatted date time. 
   */
  public function format($time, $pattern = 'F', $inputPattern = null, $charset = 'UTF-8')
  {

    $date = $this->getDate($time, $inputPattern);

    if (is_null($pattern))
    {
      $pattern = 'F';<commit_msg>fixed coding standard<commit_after><?php
/**
 * sfDateFormat class file.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the BSD License.
 *
 * Copyright(c) 2004 by <NAME>. All rights reserved.
 *
 * To contact the author write to {@link mailto:<EMAIL> Qiang Xue}
 * The latest version of PRADO can be obtained from:
 * {@link http://prado.sourceforge.net/}
 *
 * @author     <NAME> <weizhuo[at]gmail[dot]com>
 * @version    $Id$
 * @package    symfony
 * @subpackage i18n
 */

/**
 * Get the encoding utilities
 */
require_once(dirname(__FILE__).'/util.php');

/**
 * sfDateFormat class.
 * 
 * The sfDateFormat class allows you to format dates and times with 
 * predefined styles in a locale-sensitive manner. Formatting times 
 * with the sfDateFormat class is similar to formatting dates.
 *
 * Formatting dates with the sfDateFormat class is a two-step process. 
 * First, you create a formatter with the getDateInstance method. 
 * Second, you invoke the format method, which returns a string containing 
 * the formatted date. 
 *
 * DateTime values are formatted using standard or custom patterns stored 
 * in the properties of a DateTimeFormatInfo.
 *
 * @author <NAME> <weizhuo[at]gmail[dot]com>
 * @version v1.0, last update on Sat Dec 04 14:10:49 EST 2004
 * @package System.I18N.core
 */
class sfDateFormat
{
  /**
   * A list of tokens and their function call.
   * @var array 
   */
  protected $tokens = array(
    'G'=>'Era',
    'y'=>'year',
    'M'=>'mon',
    'd'=>'mday',
    'h'=>'Hour12',
    'H'=>'hours',
    'm'=>'minutes',
    's'=>'seconds',
    'E'=>'wday',
    'D'=>'yday',
    'F'=>'DayInMonth',
    'w'=>'WeekInYear',
    'W'=>'WeekInMonth',
    'a'=>'AMPM',
    'k'=>'HourInDay',
    'K'=>'HourInAMPM',
    'z'=>'TimeZone'
  );

  /**
   * A list of methods, to be used by the token function calls.
   * @var array 
   */
  protected $methods = array();

  /**
   * The sfDateTimeFormatInfo, containing culture specific patterns and names.
   * @var sfDateTimeFormatInfo   
   */
  protected $formatInfo;

  /**
   * Initialize a new sfDateFormat.
   *
   * @param mixed either, null, a sfCultureInfo instance, a DateTimeFormatInfo instance, or a locale.
   * @return sfDateFormat instance
   */
  function __construct($formatInfo = null)
  {
    if (is_null($formatInfo))
    {
      $this->formatInfo = sfDateTimeFormatInfo::getInvariantInfo();
    }
    else if ($formatInfo instanceof sfCultureInfo)
    {
      $this->formatInfo = $formatInfo->DateTimeFormat;
    }
    else if ($formatInfo instanceof sfDateTimeFormatInfo)
    {
      $this->formatInfo = $formatInfo;
    }
    else
    {
      $this->formatInfo = sfDateTimeFormatInfo::getInstance($formatInfo);
    }

    $this->methods = get_class_methods($this);
  }
  
  /**
   * Guesses a date without calling strtotime.
   *
   * @param mixed the time as integer or string in strtotime format.
   * @param string the input pattern; default is sql date or timestamp
   * @return string formatted date time. 
   */
  public function getDate($time, $pattern = null)
  {
    // if the type is not a php timestamp
    if (is_string($time))
    {
      if (!$pattern)
      {
        if (strlen($time) == 10)
        {
          $pattern = 'yyyy-MM-dd';
        }
        else   // otherwise, default:
        {
          $pattern = 'yyyy-MM-dd HH:mm:ss';
        }
      }

      $tokens = $this->getTokens($pattern);
      $pregPattern = '';
      $matchNames = array();
      foreach ($tokens as $token)
      {
        if ($matchName = $this->getFunctionName($token))
        {
          $pregPattern .= '(\d+)';
          $matchNames[] = $matchName;
        }
        else
        {
          $pregPattern .= $token;
        }
      }
      preg_match('@'.$pregPattern.'@', $time, $matches);

      array_shift($matches);

      if (count($matchNames) == count($matches))
      {
        $result = array_combine($matchNames, $matches);
        $date = $result;
      }
    }

    // the last attempt has failed we fall back on the default method
    if (!isset($date))
    {
      if (is_string($time))
      {
        $time = @strtotime($time);
      }
      $date = @getdate($time);
    }

    // we set default values for the time
    foreach (array('hours', 'minutes', 'seconds') as $timeDiv)
    {
      if (!isset($date[$timeDiv]))
      {
        $date[$timeDiv] = 0;
      }
    }

    return $date;
  }

  /**
   * Format a date according to the pattern.
   *
   * @param mixed the time as integer or string in strtotime format.
   * @return string formatted date time. 
   */
  public function format($time, $pattern = 'F', $inputPattern = null, $charset = 'UTF-8')
  {
    $date = $this->getDate($time, $inputPattern);

    if (is_null($pattern))
    {
      $pattern = 'F';
{'fraction_non_alphanumeric': 0.13954680611094022, 'fraction_numerical': 0.005741823028811648, 'mean_word_length': 2.494804729487639, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 68, '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': '7126422', 'n_tokens_mistral': 3122, 'n_tokens_neox': 3033, 'n_words': 876}