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

version» Context lines:

pike.git/lib/modules/Calendar.pmod/YMD.pmod:945:       int number_of_hours() { return (number_of_seconds()+3599)/3600; }    cHour hour(void|int n) { return get_unit("hour",n); }    array(cHour) hours(int ...range)    { return get_timeofday("hours",0,3600,Hour,@range); }       int number_of_minutes() { return (number_of_seconds()+59)/60; }    cMinute minute(void|int n,int ... time)    {    if (sizeof(time)) -  { -  if (n==number_of_hours()) -  return (hour(-1)+1)->minute(time[0]); -  return hour(n)->minute(time[0]); -  } +  return minute(n*60+time[0]);    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)    {
pike.git/lib/modules/Calendar.pmod/YMD.pmod:2428:    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 (zonename[0]=='"') sscanf(zonename,"\"%s\"",zonename); +  sscanf(zonename,"%*[ \t]%s",zonename); +     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) + TimeRange parse(string fmt,string arg,void|TimeRange context)   {    [string nfmt,array q]=(parse_format_cache[fmt]||({0,0}));       if (!nfmt)    {   // nfmt=replace(fmt," %","%*[ \t]%"); // whitespace -> whitespace   #define ALNU "%[^- -,./:-?[-`{-¿]"   #define AMPM "%[ampAMP]"   #define NUME "%[0-9]" -  + #define ZONE "%[-+0-9A-Za-z/]"    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"})); +  NUME,NUME,NUME,ZONE,"%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 -  +  if (sizeof(q)==0) error("format doesn't contain anything to parse\n"); +  if (q[-1]=="z") nfmt=replace(nfmt,ZONE,"%s");    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->n!="") return 0;    -  + // werror("%O\n",m); +    // werror("bopa %O\n %O\n %O\n %O\n",fmt,arg,nfmt,m);          TimeRange low;       Calendar cal=this_object();         // #define NOCATCH   #ifndef NOCATCH
pike.git/lib/modules/Calendar.pmod/YMD.pmod:2543:    else if (m->y)    {    if (strlen(m->y)<3)    {    m->y=(int)m->y;    if (m->y<70) m->y+=2000;    else if (m->y<100) m->y+=1900;    }    low=m->year=cal->Year(m->y);    } -  else low=m->year=cal->Year(); +  else low=m->year=context?context->year():cal->Year();       if (m->M)    {    m->month=low=m->year->month(m->M);    }    if (m->W)    m->week=low=m->year->week("w"+m->W);       if (!zero_type(m->D)) -  m->day=low=(m->month||cal->Month())->day((int)m->D); +  m->day=low=(m->month||(context?context->month():cal->Month())) +  ->day((int)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); +  m->day=low=(m->week||(context?context->week():cal->Week())) +  ->day(m->e);    else -  low=m->day=cal->Day(); +  low=m->day=context?context->day():cal->Day();       if (m->day && zero_type(m->Y) && 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++)    {
pike.git/lib/modules/Calendar.pmod/YMD.pmod:2643:    else    return low;      #ifndef NOCATCH    })   #endif    return 0;   }      //! function Day dwim_day(string date) + //! function Day dwim_day(string date,TimeRange context)   //! Tries a number of different formats on the given date (in order):   //! <pre>   //! <ref>parse</ref> format as in   //! "%y-%M-%D (%M) -W%W-%e (%e)" "2000-03-20 (Mar) -W12-1 (Mon)"   //! "%M%/%D/%y" "3/20/2000"   //! "%D%*[ /]%M%*[ /-,]%y" "20/3/2000" "20 mar 2000" "20/3 -00"   //! "%e%*[ ]%D%*[ /]%M%*[ /-,]%y" "Mon 20 Mar 2000" "Mon 20/3 2000"   //! "%y-%M-%D" "2000-03-20", "00-03-20"   //! "%d" "20000320", "000320"   //! "-%y%*[ /]%D%*[ /]%M" "-00 20/3" "-00 20 mar"
pike.git/lib/modules/Calendar.pmod/YMD.pmod:2706:   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");      */    - array dwim_day_strings= + static constant dwim_day_strings=   ({    "%y-%M-%D (%*s) -W%W-%e (%e)",    "%y-%M-%D",    "%M/%D/%y",    "%D%*[ /]%M%*[- /,]%y",    "%M %D%*[- /,]%y",    "%e%*[, ]%D%*[a-z:]%*[ /]%M%*[-/ ,]%y",    "%e%*[, ]%M%*[ ,]%D%*[ ,]%y",    "-%y%*[ /]%D%*[ /]%M",    "-%y%*[ /]%M%*[ /]%D",
pike.git/lib/modules/Calendar.pmod/YMD.pmod:2729:    "%D.%M.%y",    "%D%*[- /]%M",    "%M%*[- /]%D",    "%M-%D-%y",    "%D-%M-%y",    "%e%*[- /wv]%W%*[ -/]%y",    "%e%*[- /wv]%W",    "%d"   });    - cDay dwim_day(string day) + cDay dwim_day(string day,void|TimeRange context)   {    cDay d;       foreach ( dwim_day_strings,    string dayformat) -  if ( (d=parse(dayformat+"%n",day)) ) +  if ( (d=parse(dayformat+"%n",day,context)) )    return d;       cDay t=Day(); -  if ( (d=parse("%e",day)) ) +  if ( (d=parse("%e",day,context)) )    {    if (d>=t) return d;    else return (d->week()+1)->place(d);    }    -  if (strlen(day)==4) catch { return parse("%M/%D",day/2*"/"); }; +  if (strlen(day)==4) catch { return parse("%M/%D",day/2*"/",context); };    -  if (day=="today") return t; -  if (day=="tomorrow") return t+1; -  if (day=="yesterday") return t-1; +  if (day=="today") return context?context->day():t; +  if (day=="tomorrow") return (context?context->day():t)+1; +  if (day=="yesterday") return (context?context->day():t)-1;    if (sscanf(day,"last %s",day))    {    cDay d=dwim_day(day);    return (d->week()-1)->place(d);    }    if (sscanf(day,"next %s",day))    {    cDay d=dwim_day(day);    return (d->week()+1)->place(d);    }       error("Failed to dwim day from %O\n",day);   }    - TimeofDay dwim_time(string what) + TimeofDay dwim_time(string what,void|TimeRange cx)   {    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 +  sscanf(what,"%*[ \t]%s",what);    -  +  if (t=parse("%e %M %D %h:%m:%s %Y",what,cx)) 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",
pike.git/lib/modules/Calendar.pmod/YMD.pmod:2809:    "%*[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; +  if ( (t=parse(todformat+"%*[ ]%n",what,cx)) ) return t;    }    else    { -  if ( (t=parse(dayformat+"%*[ ,]"+todformat,what)) ) return t; -  if ( (t=parse(todformat+"%*[ ,]"+dayformat,what)) ) return t; +  if ( (t=parse(dayformat+"%*[ ,]"+todformat,what,cx)) ) return t; +  if ( (t=parse(todformat+"%*[ ,]"+dayformat,what,cx)) ) return t;    }    }       error("Failed to dwim time from %O\n",what);   }      //-- auxillary functions------------------------------------------------      //!   //! function datetime(int|void unix_time)