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

version» Context lines:

pike.git/lib/modules/Calendar.pmod/YMD.pmod:2318:   //! %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" + //! %n empty string (to be put at the end of formats)   //! </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:2346:   //! EST America/Panama [UTC-5]   //! GST Asia/Dubai [UTC+4]   //! IST Asia/Jerusalem [UTC+2]   //! WST Australia/Perth [UTC+8]   //! </pre>   //!   //! This mapping is modifiable in the ruleset, see   //! <ref>Ruleset.set_abbr2zone</ref>.       + // dwim time of day; needed to correct timezones   // this API may change without further notice -  + static TimeRange dwim_tod(TimeRange origin,string whut,int h,int m,int s) + { +  TimeRange tr=origin[whut](h,m,s); +  if (tr->hour_no()!=h || tr->minute_no()!=m) +  { +  if (tr->hour_no()!=h) +  tr=tr->add(h-tr->hour_no(),Hour); +  if (tr->minute_no()!=m) +  tr=tr->add(m-tr->minute_no(),Minute); +  if (tr->second_no()!=s) +  tr=tr->add(s-tr->second_no(),Second); +  if (tr->hour_no()!=h || tr->minute_no()!=m || +  tr->second_no()!=s) return 0; // no such hour +  } +  return tr; + }    - TimeRange dwim_zone(TimeRange origin,string zonename, + // dwim timezone and call dwim time of day above + // this API may change without further notice + static TimeRange dwim_zone(TimeRange origin,string zonename,    string whut,int ...args)   {    if (zonename=="") return 0;    if (origin->rules->abbr2zone[zonename])    zonename=origin->rules->abbr2zone[zonename];    Ruleset.Timezone zone=Timezone[zonename];    if (!zone)    {    array pz=TZnames.abbr2zones[zonename];    if (!pz) return 0;    foreach (pz,string zn)    {    TimeRange try=dwim_zone(origin,zn,whut,@args);    if (try->tzname()==zonename) return try;    }    return 0;    }    else -  return origin->set_timezone(zone)[whut](@args); +  return dwim_tod(origin->set_timezone(zone),whut,@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","%p", -  "%t","%f","%d","%z",}), +  "%t","%f","%d","%z","%n"}),    ({ALNU,ALNU,ALNU,"%d","%d","%d",ALNU,"%d","%d","%d",AMPM, -  NUME,NUME,NUME,"%[-+0-9A-Za-z/]"})); +  NUME,NUME,NUME,"%[-+0-9A-Za-z/]","%s"}));    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);       TimeRange low;       Calendar cal=this_object();      // #define NOCATCH   #ifndef NOCATCH    if (catch {   #else    werror("%O\n",m);   #endif -  +  if (m->n && m->n!="") return 0; +     string x;    if (m->Y)    m->Y=default_rules->language[f_year_number_from_name](m->Y);       if (!zero_type(m->Y) && m->D && (int)m->M)    low=m->day=cal->Day(m->Y,(int)m->M,m->D);       if (m->d)    {    int y,mo,d;
pike.git/lib/modules/Calendar.pmod/YMD.pmod:2527:    if (h!=12) h+=12;    break;    default:    return 0; // need "am" or "pm"    }    }       if (m->z) // zone    return dwim_zone(low,m->z,g,h,mi,s);    else if (g) -  return low[g](h,mi,s); +  return dwim_tod(low,g,h,mi,s);    else    return low;   #ifndef NOCATCH    })   #endif    return 0;   }      //! function Day dwim_day(string date)   //! Tries a number of different formats on the given date (in order):
pike.git/lib/modules/Calendar.pmod/YMD.pmod:2654:    }       error("Failed to dwim day from %O\n",day);   }      TimeofDay dwim_time(string what)   {    string a,h,m,s;    TimeofDay t;    + // #define COLON "$*[ :]" + #define COLON ":" +     foreach ( dwim_day_strings +    ({""}),    string dayformat )    foreach ( ({ "%t %z",    "T%t %z",    "T%t", -  "%h:%*[ :]%m%*[ :]%s %p %z", -  "%h:%*[ :]%m%*[ :]%s %p", -  "%h:%*[ :]%m%*[ :]%s %z", -  "%h:%*[ :]%m%*[ :]%s%z", -  "%h:%*[ :]%m%*[ :]%s", -  "%h:%*[ :]%m %p %z", -  "%h:%*[ :]%m %p", -  "%h:%*[ :]%m %z", -  "%h:%*[ :]%m%z", -  "%h:%*[ :]%m", +  "%h"COLON"%m"COLON"%s %p %z", +  "%h"COLON"%m"COLON"%s %p", +  "%h"COLON"%m"COLON"%s %z", +  "%h"COLON"%m"COLON"%s%z", +  "%h"COLON"%m"COLON"%s", +  "%h"COLON"%m %p %z", +  "%h"COLON"%m %p", +  "%h"COLON"%m %z", +  "%h"COLON"%m%z", +  "%h"COLON"%m",    "%h%*[ ]%p", -  "%[a-zA-Z.] %h:%*[ :]%m%*[ :]%s %p %z", -  "%[a-zA-Z.] %h:%*[ :]%m%*[ :]%s %p", -  "%[a-zA-Z.] %h:%*[ :]%m%*[ :]%s %z", -  "%[a-zA-Z.] %h:%*[ :]%m%*[ :]%s%z", -  "%[a-zA-Z.] %h:%*[ :]%m%*[ :]%s", -  "%[a-zA-Z.] %h:%*[ :]%m %p %z", -  "%[a-zA-Z.] %h:%*[ :]%m %p", -  "%[a-zA-Z.] %h:%*[ :]%m %z", -  "%[a-zA-Z.] %h:%*[ :]%m%z", -  "%[a-zA-Z.] %h:%*[ :]%m", +  "%[a-zA-Z.] %h"COLON"%m"COLON"%s %p %z", +  "%[a-zA-Z.] %h"COLON"%m"COLON"%s %p", +  "%[a-zA-Z.] %h"COLON"%m"COLON"%s %z", +  "%[a-zA-Z.] %h"COLON"%m"COLON"%s%z", +  "%[a-zA-Z.] %h"COLON"%m"COLON"%s", +  "%[a-zA-Z.] %h"COLON"%m %p %z", +  "%[a-zA-Z.] %h"COLON"%m %p", +  "%[a-zA-Z.] %h"COLON"%m %z", +  "%[a-zA-Z.] %h"COLON"%m%z", +  "%[a-zA-Z.] %h"COLON"%m",    "%[a-zA-Z.] %h%*[ ]%p", }),    string todformat )    {   // werror("try: %O\n %O\n",   // dayformat+"%*[ ,]"+todformat,   // todformat+"%*[ ,]"+dayformat); -  +  if (dayformat=="") +  { +  if ( (t=parse(todformat+"%*[ ]%n",what)) ) return t; +  } +  else +  {    if ( (t=parse(dayformat+"%*[ ,]"+todformat,what)) ) return t;    if ( (t=parse(todformat+"%*[ ,]"+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:   //! <pre>