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

version» Context lines:

pike.git/lib/modules/Calendar.pmod/YMD.pmod:6:   //! ie, one with years, months, weeks and days   //!      #pike __REAL_VERSION__      // #pragma strict_types      import ".";   inherit Time:Time;    + #include "constants.h" +    #define this this_object()      // ----------------   // virtual methods to tell how this calendar works   // ----------------      static array(int) year_from_julian_day(int jd);   static int julian_day_from_year(int year);   static int year_remaining_days(int y,int yday);   
pike.git/lib/modules/Calendar.pmod/YMD.pmod:958:    return get_unit("minute",n);    }    array(cMinute) minutes(int ...range)    { return get_timeofday("minutes",0,60,Minute,@range); }       int number_of_seconds() { return end()->unix_time()-unix_time(); }    cSecond second(void|int n,int ...time)    {    if (sizeof(time)==2)    { -  if (n==number_of_hours()) -  return (hour(-1)+1)->minute(time[0])->second(time[1]); -  return hour(n)->minute(time[0])->second(time[1]); +  return second(n*3600+time[0]*60+time[1]); + // return hour(n)->minute(time[0])->second(time[1]);    }    return get_unit("second",n);    }    array(cSecond) seconds(int ...range)    { return get_timeofday("seconds",0,1,Second,@range); }       float number_of_fractions() { return (float)number_of_seconds(); }    cSecond fraction(void|float|int n)    {    return fractions()[0];
pike.git/lib/modules/Calendar.pmod/YMD.pmod:2397:   //! </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); +  TimeRange tr; +  if (catch { +  tr=origin[whut](h,m,s); +  } && h==24 && m==0 && s==0) // special case +  return origin->end()->second(); +     if (tr->hour_no()!=h || tr->minute_no()!=m)    {   // werror("%O %O %O -> %O %O %O (%O)\n",   // tr->hour_no(),tr->minute_no(),tr->second_no(),   // h,m,s,tr);    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)
pike.git/lib/modules/Calendar.pmod/YMD.pmod:2421:    }    return tr;   }      // 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 dwim_tod(origin->set_timezone(zone),whut,@args);   }    -  + static mapping(string:array) parse_format_cache=([]); +    TimeRange parse(string fmt,string arg)   { -  string nfmt; +  [string nfmt,array q]=(parse_format_cache[fmt]||({0,0}));    -  nfmt=replace(fmt," %","%*[ \t]%"); // whitespace -> whitespace +  if (!nfmt) +  { + // nfmt=replace(fmt," %","%*[ \t]%"); // whitespace -> whitespace   #define ALNU "%[^- -,./:-?[-`{-¿]"   #define AMPM "%[ampAMP]"   #define NUME "%[0-9]" -  nfmt=replace(nfmt, +  +  nfmt=replace(fmt,    ({"%Y","%y","%M","%W","%D","%a","%e","%h","%m","%s","%p",    "%t","%f","%d","%z","%n"}),    ({ALNU,ALNU,ALNU,"%d",NUME,"%d",ALNU,"%d","%d","%d",AMPM,    NUME,NUME,NUME,"%[-+0-9A-Za-z/]","%s"})); -  +  + #if 1 +  q=array_sscanf(fmt,"%{%*[^%]%%%1s%}")*({})*({})-({"*","%"}); + #else + // slower alternatives:    array q=Array.map(replace(fmt,({"%*","%%"}),({"",""}))/"%",    lambda(string s){ return s[..0];})-({""}); -  +  +  array q=({}); +  array pr=(array)fmt; +  int i=-1; +  while ((i=search(pr,'%',i+1))!=-1) q+=({sprintf("%c",pr[i+1])}); + #endif +  parse_format_cache[fmt]=({nfmt,q}); +  } +     array res=array_sscanf(arg,nfmt);    -  +  int i=search(res,""); +  if (i!=-1 && i<sizeof(res)-1) return 0; +     if (sizeof(res)<sizeof(q))    return 0; // parse error       mapping m=mkmapping(q,res); -  +  if (i!=-1 && m->z!="") return 0;    -  + // werror("bopa %O\n %O\n %O\n %O\n",fmt,arg,nfmt,m); +     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,(int)m->D);    -  +     if (m->d)    {    int y,mo,d;       if (strlen(m->d)==6)    {    [y,mo,d]=(array(int))(m->d/2);    if (y<70) y+=2000; else y+=1900;    }    else if (strlen(m->d)==8)
pike.git/lib/modules/Calendar.pmod/YMD.pmod:2600:    return 0; // need "am" or "pm"    }    }       if (m->z) // zone    return dwim_zone(low,m->z,g,h,mi,s);    else if (g)    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):   //! <pre>   //! <ref>parse</ref> format as in
pike.git/lib/modules/Calendar.pmod/YMD.pmod:2736:   }      TimeofDay dwim_time(string what)   {    string a,h,m,s;    TimeofDay t;      // #define COLON "$*[ :]"   #define COLON ":"    +  if (t=parse("%e %M %D %h:%m:%s %Y",what)) return t; // ctime +     foreach ( dwim_day_strings +    ({""}),    string dayformat )    foreach ( ({ "%t %z",    "T%t %z",    "T%t",    "%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"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", }), +  "%*[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