<?php if (!defined('PmWiki')) exit();

 # TODO:  translation of weekdays is now fixed for german, modify to multilanguage !
 # TODO: not yet implemented:
 # TODO: format: startdate TO enddate+time
 # TODO: format: startdate+time TO enddate

# see page Test.ParseDate and Cookbook.ParseDate
  
@DEBUG(__FILE__);

markup('testparsedate','directives','/^TESTPARSEDATE:(.*)/e',"TestParseDate('$1')");

### DEBUG(localeconv(),1);

function TestParseDate($text)
{
  $GLOBALS['TestParseDate'] = TRUE;
  list($text2,$from,$to) = ParseDate($text,'','','');
  return "* passed: '$text'\n* parsed:\n** text=\"$text2\"\n** from=$from\n** to=$to\n";
}

# DEBUG(mktime(2,1,2003),1);
# DEBUG(mktime(2,1,03),1);

## calling format of mktime(h,m,s,m,d,y,?)

function ParseDate($datestring,$fmtdate,$fmttime,$sep)
{
  if (preg_match('/^\s*$/',$datestring))
  {
    return array("",0,0);
  }
  #---
  # datestring starting with " /" or " ???" will be ignored:
  if (preg_match('/^\s*(\/|\?{3})/',$datestring))
  {
    return array($datestring,0,0);
  }
  
  #---
  if ($fmtdate=="") $fmtdate = '%a %d.%m.%Y'; # default date format = weekday dd.mm.yyyy
  if ($fmttime=="") $fmttime = '%H:%M'; # default time format = hh:mm
  if ($sep=="") $sep = ' $[to] '; # default separator between start and enddate
 
  #---
  $err = array(" //%red%$datestring<br />$[unknown date format] !<br />[-[[Cookbook/ParseDate#formats|=>$[help]]]-]%%//",0,0);

  if (preg_match('/^\s*(\d{1,2})\.(\d{1,2})\.((19|20){1}\d{1,2})\s*(\d{1,2}):(\d{2})\s*(bis|-)\s*(\d{1,2})\.(\d{1,2})\.((19|20{1})\d{2})\s*(\d{1,2}):(\d{2})/i',$datestring,$m))
  {
    $D = '[d]d.[m]m.yyyy [h]h:mm -/bis dd.mm.yyyy [h]h:mm';
    $from = mktime($m[4],$m[5],0,$m[2],$m[1],$m[3]);
    $to = mktime($m[10],$m[11],0,$m[8],$m[7],$m[9]);;
    #DEBUG($datestring,1); DEBUG($m,1); DEBUG($D,1);
  }
  else if (preg_match('/^\s*(\d\d?)\.(\d\d?)\.((19|20){1}\d{2})\s*(bis|-)\s*(\d\d?)\.(\d\d?)\.((19|20){1}\d{2})/i',$datestring,$m))
  {
    $D = '[d]d.[m]m.yyyy -/bis [d]d.[m]m.yyyy';
    $from = mktime(0,0,0,$m[2],$m[1],$m[3]);
    $to = mktime(0,0,0,$m[7],$m[6],$m[8]);
    # DEBUG($datestring,1); DEBUG($m,1); DEBUG($D,1);
  }
  else if (preg_match('/^\s*(\d{1,2})\.(\d{1,2})\.(\d{2})\s*(bis|-)\s*(\d{1,2})\.(\d{1,2})\.(\d{2})/i',$datestring,$m))
  {
    $D = '[d]d.[m]m.yy -/bis [d]d.[m]m.yy';
    $from = mktime(0,0,0,$m[2],$m[1],$m[3]);
    $to = mktime(0,0,0,$m[6],$m[5],$m[7]);
    #DEBUG($datestring,1); DEBUG($m,1); DEBUG($D,1);
  }
  else if (preg_match('/^\s*(\d{1,2})\.(\d{1,2})\.\s*(bis|-)\s*(\d{1,2})\.(\d{1,2})\.((19|20)\d{2})/i',$datestring,$m))
  {
    $D = '[d]d.[m]m. -/bis [d]d.[m]m.yyyy';
    $from = mktime(0,0,0,$m[2],$m[1],$m[6]);
    $to = mktime(0,0,0,$m[5],$m[4],$m[6]);
    #DEBUG($datestring,1); DEBUG($m,1); DEBUG($D,1);
  }
  else if (preg_match('/^\s*(\d{1,2})\.(\d{1,2})\.\s*(bis|-)\s*(\d{1,2})\.(\d{1,2})\.(\d{2})/i',$datestring,$m))
  {
    $D = '[d]d.[m]m. -/bis [d]d.[m]m.yy';
    $from = mktime(0,0,0,$m[2],$m[1],$m[6]);
    $to = mktime(0,0,0,$m[5],$m[4],$m[6]);
    #DEBUG($datestring,1); DEBUG($m,1); DEBUG($D,1);
  }
  else if (preg_match('/^\s*(\d{1,2})\.(\d{1,2})\.\s*(bis|-)\s*(\d{1,2})\.(\d{1,2})\./i',$datestring,$m))
  {
    $D = '[d]d.[m]m. -/bis [d]d.[m]m.';
    $from = mktime(0,0,0,$m[2],$m[1],date('Y'));
    $to = mktime(0,0,0,$m[5],$m[4],date('Y'));
    #DEBUG($datestring,1); DEBUG($m,1); DEBUG($D,1);
  }
  else if (preg_match('/^\s*(\d{1,2})\.(\d{1,2})\.((19|20){1}\d{2})\s*(\d{1,2}):(\d{2})/',$datestring,$m))
  {
    $D = '[d]d.[m]m.yyyy [h]h:mm';
    $from = mktime($m[5],$m[6],0,$m[2],$m[1],$m[3]);
    $to = 0;
    #DEBUG($datestring,1); DEBUG($m,1); DEBUG($D,1);
  }
  else if (preg_match('/^\s*(\d{1,2})\.(\d{1,2})\.((19|20){1}\d{2})\s*(\d{1,2}):(\d{1})/',$datestring,$m))
  {
    return $err;
  }
  else if (preg_match('/^\s*(\d{1,2})\.(\d{1,2})\.((19|20)\d{1,2})/',$datestring,$m))
  {
    $D = '[d]d.[m]m.yyyy';
    $from = mktime(0,0,0,$m[2],$m[1],$m[3]);
    $to = 0;
    # DEBUG($datestring,1); DEBUG($m,1); DEBUG($D,1);
  }
  else if (preg_match('/^\s*(\d{1,2})\.(\d{1,2})\.(\d{2})/',$datestring,$m))
  {
    $D = '[d]d.[m]m.yy';
    $from = mktime(0,0,0,$m[2],$m[1],2000+$m[3]);
    $to   = 0;
    # DEBUG($datestring,1); DEBUG($m,1); DEBUG($D,1);
  }
  else if (preg_match('/^\s*(\d{1,2})\.(\d{1,2})\.\s*(\d{1,2}):(\d{2})/',$datestring,$m))
  {
    $D = '[d]d.[m]m. [h]:mm';
    $from = mktime($m[3],$m[4],0,$m[2],$m[1],date('Y'));
    $to   = 0;
    #DEBUG($datestring,1); DEBUG($m,1); DEBUG($D,1);
  }
  else if (preg_match('/^\s*(\d{1,2})\.(\d{1,2})\.\s*(\d{1,2}):(\d{1})/',$datestring,$m))
  {
    return $err;
  }
  else if (preg_match('/^\s*(\d{1,2})\.(\d{1,2})\./',$datestring,$m))
  {
    $D = '[d]d.[m]m.';
    $from = mktime(0,0,0,$m[2],$m[1],date('Y'));
    $to   = 0;
    # DEBUG($datestring,1); DEBUG($m,1); DEBUG($D,1);
  }
  /*
  else if ($from = strtotime($datestring))
  {
    $D = 'strtotime';
    $to = 0;
  }*/
  else
  {
    return $err;
  }
  
  if ($GLOBALS['TestParseDate'])
    { $datetimestring = "%blue%FMT$D: %%"; }
  else
    { $datetimestring = ''; }
  
  
  #DEBUG($m,1);
  #DEBUG(mktime(2,1,2003),1);
  #DEBUG($from,1);

  if (date('His',$from) === "000000")
    { $fmt = $fmtdate; }
  else
    { $fmt = $fmtdate.' '.$fmttime; }
    
  $datetimestring .= strftime($fmt,$from);
  if ($to !== 0)
  {
    if (date('His',$from) === "000000")
      { $fmt = $fmtdate; }
    else
      { $fmt = $fmtdate.' '.$fmttime; }
    $datetimestring .= $sep.strftime($fmt,$to); 
  }
  
  # TODO: if german, other translations ... 
 
  if ($to > 0 and $from > $to)
    { $datetimestring .= "<br />%red%$[start]>$[end] !%%"; }
    
  return array($datetimestring,$from,$to,$kw);
}

?>