pike.git / lib / modules / Calendar.pmod / YMD.pmod

version» Context lines:

pike.git/lib/modules/Calendar.pmod/YMD.pmod:2276:   //! %D date (needs %y, %m)   //! %d short date (20000304, 000304)   //! %a day (needs %y)   //! %e weekday (needs %y, %w)   //! %h hour (needs %d, %D or %W)   //! %m minute (needs %h)   //! %s second (needs %m)   //! %f fraction of a second (needs %s)   //! %t short time (205314, 2053)   //! %z zone + //! %p "am" or "pm"   //! </pre>   //!   //! returns 0 if format doesn't match data, or the appropriate time object.   //!   //! note:   //! The zone will be a guess if it doesn't state an exact   //! regional timezone (like "Europe/Stockholm") -   //! most zone abbriviations (like "CET") are used by more   //! then one region with it's own daylight saving rules.   //! Also beware that for instance CST can be up to four different zones,
pike.git/lib/modules/Calendar.pmod/YMD.pmod:2331:    else    return origin->set_timezone(zone)->second(@args);   }      TimeRange parse(string fmt,string arg)   {    string nfmt;       nfmt=replace(fmt," %","%*[ \t]%"); // whitespace -> whitespace   #define ALNU "%[^ -,./:-?[-`{-¿]" + #define AMPM "%[ampAMP]"   #define NUME "%[0-9]"    nfmt=replace(nfmt, -  ({"%Y","%y","%M","%W","%D","%a","%e","%h","%m","%s", -  "%t","%f","%d","%z"}), -  ({ALNU,ALNU,ALNU,"%d","%d","%d",ALNU,"%d","%d","%d", +  ({"%Y","%y","%M","%W","%D","%a","%e","%h","%m","%s","%p", +  "%t","%f","%d","%z",}), +  ({ALNU,ALNU,ALNU,"%d","%d","%d",ALNU,"%d","%d","%d",AMPM,    NUME,NUME,NUME,"%[-+0-9A-Za-z/]"}));    array q=Array.map(replace(fmt,({"%*","%%"}),({"",""}))/"%",    lambda(string s){ return s[..0];})-({""});    array res=array_sscanf(arg,nfmt);       if (sizeof(res)<sizeof(q))    return 0; // parse error       mapping m=mkmapping(q,res);   
pike.git/lib/modules/Calendar.pmod/YMD.pmod:2404:       if (!zero_type(m->D))    if (stringp(m->D)) return 0;    else m->day=low=(m->month||cal->Month())->day(m->D);    else if (!zero_type(m->a))    m->day=low=m->year->day(m->a);    else if (!zero_type(m->e))    m->day=low=(m->week||cal->Week())->day(m->e);    else    low=m->day=cal->Day(); +  +  if (m->day && zero_type(m->Y) && m->e) +  if (m->month) +  { +  // scan for closest year that matches +  cYear y2=m->day->year(); +  object d2; +  int i; +  for (i=0; i<20; i++) +  { +  d2=(y2+i)->place(m->day); +  if (d2 && d2->week()->day(m->e)==d2) break; +  d2=(y2-i)->place(m->day); +  if (d2 && d2->week()->day(m->e)==d2) break;    } -  +  if (i==20) return 0; +  low=m->day=d2; +  } +  else +  { +  // scan for closest month that matches +  cYear m2=m->day->month(); +  object d2; +  int i; +  for (i=0; i<20; i++) +  { +  d2=(m2+i)->place(m->day); +  if (d2 && d2->week()->day(m->e)==d2) break; +  d2=(m2-i)->place(m->day); +  if (d2 && d2->week()->day(m->e)==d2) break; +  } +  if (i==20) return 0; +  low=m->day=d2; +  } +  }       int h=0,mi=0,s=0,g=0;       if (m->t)    {    if (strlen(m->t)==6)    [h,mi,s]=(array(int))(m->t/2),g=1;    else if (strlen(m->t)==4)    [h,mi]=(array(int))(m->t/2),g=1;    else return 0;    }    else    {    if (!zero_type(m->h)) h=m->h,g=1;    if (!zero_type(m->m)) mi=m->m,g=1;    if (!zero_type(m->s)) s=m->s,g=1;    }    -  +  if (!zero_type(m->p)) +  { +  switch (lower_case(m->p)) +  { +  case "am": +  if (h==12) h=0; +  break; +  case "pm": +  if (h!=12) h+=12; +  break; +  default: +  return 0; // need "am" or "pm" +  } +  } +     if (m->z) // zone    return dwim_zone(low,m->z,h,mi,s);    else if (g)    return low->second(h,mi,s);    else    return low;    })    return 0;   }   
pike.git/lib/modules/Calendar.pmod/YMD.pmod:2495:   Calendar.dwim_day("1 w12 2000");   Calendar.dwim_day("mon w12");   Calendar.dwim_day("monday" );   Calendar.dwim_day("1");   Calendar.dwim_day("today");   Calendar.dwim_day("last monday");   Calendar.dwim_day("next monday");      */    - cDay dwim_day(string day) - { -  cDay d; -  -  foreach ( ({ "%y-%M-%D (%*s) -W%W-%e (%e)", + array dwim_day_strings= + ({"%y-%M-%D (%*s) -W%W-%e (%e)",    "%D%*[ /]%M%*[- /,]%y",    "%M %D%*[- /,]%y", -  "%e%*[ ]%D%*[ /]%M%*[-/ ,]%y", +  "%e%*[, ]%D%*[a-z:]%*[ /]%M%*[-/ ,]%y",    "%y-%M-%D",    "-%y%*[ /]%D%*[ /]%M",    "-%y%*[ /]%M%*[ /]%D",    "%y%*[ /]%D%*[ /]%M",    "%y%*[ /]%M%*[ /]%D",    "%D%*[- /]%M",    "%M%*[- /]%D",    "%e%*[- /wv]%W%*[ -/]%y",    "%e%*[- /wv]%W", -  "%d"}), +  "%d"}); +  + cDay dwim_day(string day) + { +  cDay d; +  +  foreach ( dwim_day_strings,    string dayformat)    if ( (d=parse(dayformat,day)) ) return d;       cDay t=Day();    if ( (d=parse("%e",day)) )    {    if (d>=t) return d;    else return (d->week()+1)->place(d);    }   
pike.git/lib/modules/Calendar.pmod/YMD.pmod:2550:   }      TimeofDay dwim_time(string what)   {    string a,h,m,s;    TimeofDay t;       foreach ( ({ " %z","%z",""}),    string zone )    foreach ( ({ "%t", +  "T%t", +  "%h:%*[ :]%m%*[ :]:%s %p",    "%h:%*[ :]%m%*[ :]:%s", -  +  "%h:%*[ :]%m %p",    "%h:%*[ :]%m", -  "T%t"}), +  "%[a-zA-Z.] %h:%*[ :]%m%*[ :]:%s %p", +  "%[a-zA-Z.] %h:%*[ :]%m%*[ :]:%s", +  "%[a-zA-Z.] %h:%*[ :]%m %p", +  "%[a-zA-Z.] %h:%*[ :]%m", }),    string todformat ) -  foreach ( ({ "%y-%M-%D (%*s) -W%W-%e (%e)", -  "%D%*[ /]%M%*[- /,]%y", -  "%M %D%*[- /,]%y", -  "%e%*[ ]%D%*[ /]%M%*[-/ ,]%y", -  "%y-%M-%D", -  "%d", -  "-%y%*[ /]%D%*[ /]%M", -  "-%y%*[ /]%M%*[ /]%D", -  "%y%*[ /]%D%*[ /]%M", -  "%y%*[ /]%M%*[ /]%D", -  "%D%*[- /]%M", -  "%M%*[- /]%D", -  "%e%*[- /wv]%W%*[ -/]%y", -  "%e%*[- /wv]%W", -  ""}), +  foreach ( dwim_day_strings + +  ({""}),    string dayformat )    { -  if ( (t=parse(dayformat+" "+todformat+zone,what)) ) return t; -  if ( (t=parse(todformat+zone+" "+dayformat,what)) ) return t; +  if ( (t=parse(dayformat+"%*[ ,]"+todformat+zone,what)) ) return t; +  if ( (t=parse(todformat+zone+"%*[ ,]"+dayformat,what)) ) return t;    }       error("Failed to dwim time from %O\n",what);   }      //-- auxillary functions------------------------------------------------      //!   //! function datetime(int|void unix_time)   //! Replacement for localtime; gives back a mapping: