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

version» Context lines:

pike.git/lib/modules/Calendar.pmod/Gregorian.pmod:1: -  - //! module Calendar +    //! - //! This module implements calendar calculations, and base classes - //! for time units. - //! - //! example program: - //! <pre> - //! void write_month(object m) - //! { - //! object w; - //! object today; - //! - //! today=function_object(object_program(m))->Day(); - //! - //! write(sprintf(" %|28s\n", - //! Simulate.capitalize(m->name()+" ") - //! +m->year()->name())); - //! - //! w=m->day(1)->week(); - //! - //! write(" "); - //! foreach (Array.map(w->days(),w->day)->week_day_name(),string n) - //! write(sprintf("%3s ",n[0..2])); - //! write("\n"); - //! - //! do - //! { - //! array a; - //! object d; - //! a=Array.map(Array.map(w->days(),w->day), - //! lambda(object d,object m) - //! { if (d->month()!=m) return 0; else return d; },m); - //! - //! write(sprintf("%3s ",w->name())); - //! foreach (a,d) - //! if (d) - //! if (d!=today) write(sprintf(" %2d ",d->month_day())); - //! else write(sprintf(">%2d<",d->month_day())); - //! else write(" "); - //! - //! write("\n"); - //! w++; - //! } - //! while (w->day(0)->month()==m); - //! } - //! </pre> - //! call with, for example, - //! <tt>write_month(Calendar.Swedish.Month());</tt>. - //! - //! class time_unit - //! - //! method array(string) lesser() - //! Gives a list of methods to get lesser (shorter) time units. - //! ie, for a month, this gives back <tt>({"day"})</tt> - //! and the method <tt>day(mixed n)</tt> gives back that - //! day object. The method <tt>days()</tt> gives back a - //! list of possible argument values to the method <tt>day</tt>. - //! Concurrently, <tt>Array.map(o->days(),o->day)</tt> gives - //! a list of day objects in the object <tt>o</tt>. - //! - //! - //! Ie:<pre> - //! array(string) lesser() - gives back a list of possible xxx's. - //! object xxxs() - gives back a list of possible n's. - //! object xxx(mixed n) - gives back xxx n - //! object xxx(object(Xxx) o) - gives back the corresponing xxx - //! </pre> - //! - //! The list of n's (as returned from xxxs) are always in order. - //! - //! There are two n's with special meaning, 0 and -1. - //! 0 always gives the first xxx, equal to - //! my_obj->xxx(my_obj->xxxs()[0]), and -1 gives the last, - //! equal to my_obj->xxx(my_obj->xxxs()[-1]). - //! - //! To get all xxxs in the object, do something like - //! <tt>Array.map(my_obj->xxxs(),my_obj->xxx)</tt>. - //! - //! xxx(object) may return zero, if there was no correspondning xxx. - //! - //! method array(string) greater() - //! Gives a list of methods to get greater (longer) time units - //! from this object. For a month, this gives back <tt>({"year"})</tt>, - //! thus the method <tt>month->year()</tt> gives the year object. - //! - //! method object next() - //! method object prev() - //! method object `+(int n) - //! method object `-(int n) - //! method object `-(object x) - //! next and prev gives the logical next and previous object. - //! The <tt>+</tt> operator gives that logical relative object, - //! ie <tt>my_day+14</tt> gives 14 days ahead. - //! <tt>-</tt> works the same way, but can also take an object - //! of the same type and give the difference as an integer. - //! +    //! submodule Gregorian   //! time units:   //! <ref>Year</ref>, <ref>Month</ref>, <ref>Week</ref>, <ref>Day</ref>   //!   //! class      array(string) month_names=    ({"January","February","Mars","April","May","June","July","August",    "September","October","November","December"});      array(string) week_day_names=    ({"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"});      /* calculated on need */      mapping week_day_mapping,month_mapping;    -  + class _TimeUnit + { +  inherit Calendar._TimeUnit; +  +  program vYear=function_object(object_program(this_object()))->Year; +  program vDay=function_object(object_program(this_object()))->Day; +  program vMonth=function_object(object_program(this_object()))->Month; +  program vWeek=function_object(object_program(this_object()))->Week; + } +    //== Year ====================================================================      class Year   {   //! class Year   //! A <ref>Calendar.time_unit</ref>.   //!   //! Lesser units: <ref>Month</ref>, <ref>Week</ref>, <ref>Day</ref>   //! Greater units: none   //!   //! -  +  inherit _TimeUnit;      //-- variables ------------------------------------------------------    -  object this=this_object(); -  program vYear=function_object(object_program(this))->Year; -  program vDay=function_object(object_program(this))->Day; -  program vMonth=function_object(object_program(this))->Month; -  program vWeek=function_object(object_program(this))->Week; -  +     int y;       array days_per_month;    array month_start_day;      //-- standard methods -----------------------------------------------       array(string) lesser()    {    return ({"month","week","day"});
pike.git/lib/modules/Calendar.pmod/Gregorian.pmod:354:    // none   };         //      //== Month ===================================================================      class Month   { +  inherit _TimeUnit;   //-- variables ------------------------------------------------------    -  object this=this_object(); -  program vYear=function_object(object_program(this))->Year; -  program vDay=function_object(object_program(this))->Day; -  program vMonth=function_object(object_program(this))->Month; -  program vWeek=function_object(object_program(this))->Week; -  +     int y;    int m;      //-- standard methods -----------------------------------------------       array(string) lesser()    {    return ({"day"});    }   
pike.git/lib/modules/Calendar.pmod/Gregorian.pmod:407:    }       int `==(object x)    {    return    objectp(x) &&    object_program(x)==object_program(this) &&    x->y==y && x->m==m;    }    -  int `!=(object x) -  { -  write("foo\n"); -  return !(this==x); -  } -  +     int `>(object x)    {    return    (object_program(x)==object_program(this) &&    (x->y==y && x->m>m) || (x->y>y));    }       object `+(int n)    {    int m2=m;
pike.git/lib/modules/Calendar.pmod/Gregorian.pmod:437:    return vMonth(y2,m2);    }       int|object `-(object|int n)    {    if (objectp(n) && object_program(n)==vMonth)    return m-n->m+(y-n->y)*12;    return this+(-n);    }    -  object next() -  { -  return this+1; -  } -  -  object prev() -  { -  return this-1; -  } -  +    //-- internal -------------------------------------------------------       int yday()    {    return this->year()->month_start_day[m];    }      //-- nonstandard methods --------------------------------------------       int number_of_days()
pike.git/lib/modules/Calendar.pmod/Gregorian.pmod:518:    return vYear(y);    }   };         //   //== Week ====================================================================      class Week   { +  inherit _TimeUnit;   //-- variables ------------------------------------------------------    -  object this=this_object(); -  program vYear=function_object(object_program(this))->Year; -  program vDay=function_object(object_program(this))->Day; -  program vMonth=function_object(object_program(this))->Month; -  program vWeek=function_object(object_program(this))->Week; -  +     int y;    int w;      //-- standard methods -----------------------------------------------       array(string) lesser()    {    return ({"day"});    }   
pike.git/lib/modules/Calendar.pmod/Gregorian.pmod:609:    return vWeek(y2,w2);    }       int|object `-(int|object n)    {    if (object_program(n)==vWeek && objectp(n))    return (this->day(1)-n->day(1))/7;    return this+(-n);    }    -  object next() -  { -  return this+1; -  } -  -  object prev() -  { -  return this-1; -  } -  +    //-- internal -------------------------------------------------------       int yday()    {    return    ({0,-1,-2,-3,3,2,1})[this->year()->julian_day(0)%7]    +7*(w-1);    }      //-- nonstandard methods --------------------------------------------
pike.git/lib/modules/Calendar.pmod/Gregorian.pmod:699:    {    return vYear(y);    }   };      //   //== Day =====================================================================      class Day   { +  inherit _TimeUnit; +    //-- variables ------------------------------------------------------    -  object this=this_object(); -  program vYear=function_object(object_program(this))->Year; -  program vDay=function_object(object_program(this))->Day; -  program vMonth=function_object(object_program(this))->Month; -  program vWeek=function_object(object_program(this))->Week; -  +     int y;    int d;      //-- standard methods -----------------------------------------------    -  array(string) lesser() -  { -  return ({}); -  } -  +     array(string) greater()    {    return ({"year","month","week"});    }       void create(int ... arg)    {    if (!sizeof(arg))    {    mapping t=localtime(time());
pike.git/lib/modules/Calendar.pmod/Gregorian.pmod:791:    }       int|object `-(object|int n)    {    if (objectp(n) && object_program(n)==vDay)    return (this->julian_day()-n->julian_day());       return this+(-n);    }    -  object next() -  { -  return this+1; -  } -  -  object prev() -  { -  return this-1; -  } -  +    //-- nonstandard methods --------------------------------------------       int julian_day()    {    return vYear(y)->julian_day(d);    }       int year_day()    {    return d;