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

version» Context lines:

pike.git/lib/modules/Calendar.pmod/YMD.pike:52:   //   // Each entry is an @expr{array(float)@}:   // @array   // @elem float 0   // End year for the range.   // @elem float 1   // Year offset.   // @elem float 2   // Year divisor.   // @elem float 3.. - // Polynomial factors with the highest exponent first. + // Polynomial factors with the highest exponent last.   // @endarray   protected constant deltat_polynomials = ({    ({ -500.0, -1820.0, 100.0,    -20.0, 0.0, 32.0, }),    ({ 500.0, 0.0, 100.0, -  0.0090316521, 0.022174192, -0.1798452, -  -5.952053, 33.78311, -1014.41, 10583.6, }), +  10583.6, -1014.41, 33.78311, -5.952053, +  -0.1798452, 0.022174192, 0.0090316521, }),    ({ 1600.0, -1000.0, 100.0, -  0.0083572073, -0.005050998, -0.8503463, -  0.319781, 71.23472, -556.01, 1574.2, }), +  1574.2, -556.01, 71.23472, 0.319781, +  -0.8503463, -0.005050998, 0.0083572073, }),    ({ 1700.0, -1600.0, 1.0, -  0.000140272128, -0.01532, -0.9808, 120.0, }), +  120.0, -0.9808, -0.01532, 0.000140272128, }),    ({ 1800.0, -1700.0, 1.0, -  0.000000851788756, 0.00013336, -0.0059285, 0.1603, 8.83, }), +  8.83, 0.1603, -0.0059285, 0.00013336, -0.000000851788756, }),    ({ 1860.0, -1800.0, 1.0, -  0.000000000875, -0.0000001699, 0.0000121272, -0.00037436, -  0.0041116, 0.0068612, -0.332447, 13.72, }), +  13.72, -0.332447, 0.0068612, 0.0041116, -0.00037436, +  0.0000121272, -0.0000001699, 0.000000000875, }),    ({ 1900.0, -1860.0, 1.0, -  0.000004288643, -0.0004473624, 0.01680668, -0.251754, 0.5737, 7.62, }), +  7.62, 0.5737, -0.251754, 0.01680668, -0.0004473624, 0.000004288643, }),    ({ 1920.0, -1900.0, 1.0, -  -0.000197, 0.0061966, -0.0598939, 1.494119, -2.79, }), +  -2.79, 1.494119, -0.0598939, 0.0061966, -0.000197, }),    ({ 1941.0, -1920.0, 1.0, -  0.0020936, -0.076100, 0.84493, 21.20, }), +  21.20, 0.84493, -0.076100, 0.0020936, }),    ({ 1961.0, -1950.0, 1.0, -  0.0003926188, 0.0042918455, 0.407, 29.07, }), +  29.07, 0.407, -0.0042918455, 0.0003926188, }),    ({ 1986.0, -1975.0, 1.0, -  0.001392758, 0.0038461538, 1.067, 45.45, }), +  45.45, 1.067, -0.0038461538, -0.001392758, }),    ({ 2005.0, -2000.0, 1.0, -  0.00002373599, 0.000651814, 0.0017275, -0.060374, 0.3345, 63.86, }), +  63.86, 0.3345, -0.060374, 0.0017275, 0.000651814, 0.00002373599, }),    ({ 2050.0, -2000.0, 1.0, -  0.005589, 0.32217, 62.92 }), +  62.92, 0.32217, 0.005589, }),    ({ 2150.0, -1820.0, 100.0, -  32.0-185.724, 56.28, -20.0, }), +  -20.0-185.724, 56.28, 32.0, }),    ({ Math.inf, -1820.0, 100.0, -  32.0, 0.0, -20.0, }), +  -20.0, 0.0, 32.0, }),   });      //! method float deltat(int unadjusted_utc)   //! Terrestrial Dynamical Time difference from standard time.   //!   //! returns   //! An approximation of the difference between TDT and UTC   //! in fractional seconds at the specified time.   //!   //! The zero point is 1901-06-25T14:23:01 UTC
pike.git/lib/modules/Calendar.pmod/YMD.pike:119:    float y = 1970.0 + unadjusted_utc/31556952.0;       array(float) polynomial;    int l, c, h = sizeof(deltat_polynomials);    do {    c = (l + h)/2;    polynomial = deltat_polynomials[c];    if (y < polynomial[0]) h = c;    else l = c + 1;    } while (l < h); +  polynomial = deltat_polynomials[l]; +     float u = (y + polynomial[1])/polynomial[2];       float deltat = 0.0; -  +  float p = 1.0;    foreach(polynomial; int i; float factor) {    if (i < 3) continue; -  deltat = deltat * u + factor; +  deltat += factor * p; +  p = p * u;    }    return deltat;   }      //------------------------------------------------------------------------   //! class YMD   //! Base (virtual) time period of the Roman-kind of calendar.   //! inherits TimeRange   //------------------------------------------------------------------------   
pike.git/lib/modules/Calendar.pmod/YMD.pike:214:    int __hash() { return jd; }      // --- query      //! method float fraction_no()   //! method int hour_no()   //! method int julian_day()   //! method int leap_year()   //! method int minute_no()   //! method int month_day() + //! method int month_days()   //! method int month_no()   //! method int second_no()   //! method int utc_offset()   //! method int week_day()   //! method int week_no()   //! method int year_day()   //! method int year_no()   //! method string month_name()   //! method string month_shortname()   //! method string month_day_name()
pike.git/lib/modules/Calendar.pmod/YMD.pike:302:    if (w==CALUNKNOWN) make_week();    return w;    }       int month_day()    {    if (md==CALUNKNOWN) make_month();    return md;    }    +  int month_days() +  { +  if (mnd==CALUNKNOWN) make_month(); +  return mnd; +  } +     int week_day()    {    if (wd==CALUNKNOWN) make_week();    return wd;    }       int year_day()    {    return yd;    }
pike.git/lib/modules/Calendar.pmod/YMD.pike:447:    ]);    }    }      // --- string format ----      //! method string format_iso_ymd();   //! method string format_ymd();   //! method string format_ymd_short();   //! method string format_ymd_xshort(); + //! method string format_mdy();   //! method string format_iso_week();   //! method string format_iso_week_short();   //! method string format_week();   //! method string format_week_short();   //! method string format_month();   //! method string format_month_short();   //! method string format_iso_time();   //! method string format_time();   //! method string format_time_short();   //! method string format_time_xshort();
pike.git/lib/modules/Calendar.pmod/YMD.pike:559:    week_day_shortname(),    month_day(),month_shortname(),y);    }       string format_ymd()    {    if (m==CALUNKNOWN) make_month();    return sprintf("%04d-%02d-%02d",y,m,md);    }    +  string format_mdy() +  { +  if (m==CALUNKNOWN) make_month(); +  return sprintf("%d/%d/%d",m,md,y%100); +  } +     string format_ymd_short()    {    if (m==CALUNKNOWN) make_month();    return sprintf("%04d%02d%02d",y,m,md);    }       string format_ymd_xshort()    {    if (m==CALUNKNOWN) make_month();    return sprintf("%02d%02d%02d",y%100,m,md);
pike.git/lib/modules/Calendar.pmod/YMD.pike:805:    {    int m=number_of_days();    if (m<=1 || m+yd-1<year()->number_of_days()) return 1;    return 1+y-year_from_julian_day(jd+m-1)[0];    }       array(cYear) years(void|int from, void|int to)    {    int n=number_of_years();    -  if (zero_type (from)) { +  if (undefinedp (from)) {    from = 1;    to = n;    }    else -  if (zero_type (to)) +  if (undefinedp (to))    error("Illegal numbers of arguments to years()\n");    else    {    if (from>n) return ({}); else if (from<1) from=1;    if (to>n) to=n; else if (to<from) return ({});    }       return map(enumerate(1+to-from,1,y+from-1),    lambda(int x)    { return Year("ymd_yn",rules,x,1); });    }       cYear year(void|int m)    { -  if (zero_type (m)) m=1; +  if (undefinedp (m)) m=1;       if (!n&&m==-1)    return Year("ymd_y",rules,y,yjd,1);       if (m<0) m += 1 + number_of_years();       array(TimeRange) res=years(m,m);    if (sizeof(res)==1) return res[0];    error("Not in range (Year 1..%d exist)\n",    number_of_years());    }         // days    -  + //! method int number_of_days() + //! Get the number of days in the current range. +     int number_of_days();    -  + //! method array(Day) days(int|void from, int|void to) + //! Get the days in the current range. +     array(cDay) days(void|int from, void|int to)    {    int n=number_of_days();    -  if (zero_type (from)) { +  if (undefinedp (from)) {    from = 1;    to = n;    }    else -  if (zero_type (to)) +  if (undefinedp (to))    error("Illegal number of arguments to days()\n");    else    {    if (from>n) return ({}); else if (from<1) from=1;    if (to>n) to=n; else if (to<from) return ({});    }       int zy=y;    int zyd=yd+from-1;    int zjd=jd+from-1;
pike.git/lib/modules/Calendar.pmod/YMD.pike:889:    zjd+=rd;    to-=rd;    }       [zy,zyjd]=year_from_julian_day(zjd);    zyd=zjd-zyjd+1;    }    return res;    }    + //! method Day day() + //! method Day day(int n) + //! Get day number n in the current range. + //! + //! If n is negative, it is counted from the end of the range. +     cDay day(void|int m, mixed... ignored)    { -  if (zero_type (m)) m=1; +  if (undefinedp (m)) m=1;       if (!n)    return Day("ymd_yd",rules,y,yjd,jd,yd,1);       if (m<0) m+=1+number_of_days();       array(TimeRange) res=days(m,m);    if (sizeof(res)==1) return res[0];    if(number_of_days())    error("Not in range (Day 1..%d exist).\n",
pike.git/lib/modules/Calendar.pmod/YMD.pike:1181:    }    result += ({ start->distance(end) });    return result;    }      // ----------------------------------------   // virtual functions needed   // ----------------------------------------       string nice_print(); -  protected string _sprintf(int t,mapping m) +  protected string _sprintf(int t)    {    switch (t)    {    case 't':    return "Calendar."+calendar_name()+".YMD";    default: -  return ::_sprintf(t,m); +  return ::_sprintf(t);    }    }       void create_julian_day(int|float jd);    protected TimeRange _move(int n,YMD step);    TimeRange place(TimeRange what,void|int force);      // not needed       YMD autopromote() { return this; }
pike.git/lib/modules/Calendar.pmod/YMD.pike:1311:    return Year("ymd_y",rules,y,yjd,0);    }       TimeRange end()    {    return Year("ymd_yn",rules,y+n,0);    }      // ----------------    -  protected string _sprintf(int t,mapping m) +  protected string _sprintf(int t)    {    switch (t)    {    case 'O':    if (n!=1)    return sprintf("Year(%s)",nice_print_period());    return sprintf("Year(%s)",nice_print());    case 't':    return "Calendar."+calendar_name()+".Year";    default: -  return ::_sprintf(t,m); +  return ::_sprintf(t);    }    }       string nice_print_period()    {    if (!n) return nice_print()+" sharp";    return sprintf("%s..%s",nice_print(),year(-1)->nice_print());    }       string nice_print()
pike.git/lib/modules/Calendar.pmod/YMD.pike:1618:    [y,yjd]=year_from_julian_day(jd=_jd);    [m,zmd,nd,yd]=month_from_yday(y,1+jd-yjd);    jd=yd+yjd-1;       n=1;    md=1;    nw=wd=w=wy=CALUNKNOWN; // unknown    }    }    -  protected string _sprintf(int t,mapping m) +  protected string _sprintf(int t)    {   // return sprintf("month y=%d yjd=%d m=%d jd=%d yd=%d n=%d nd=%d",   // y,yjd,m,jd,yd,n,number_of_days());    switch (t)    {    case 'O':    if (n!=1)    return sprintf("Month(%s)",nice_print_period());    return sprintf("Month(%s)",nice_print());    case 't':    return "Calendar."+calendar_name()+".Month";    default: -  return ::_sprintf(t,m); +  return ::_sprintf(t);    }    }       string nice_print()    {    return sprintf("%s %s", month_name(), year_name());    }       string format_nice()    {
pike.git/lib/modules/Calendar.pmod/YMD.pike:1925:    [wy,w,zwd,int nd,jd]=week_from_julian_day(_jd);    [y,yjd]=year_from_julian_day(_jd);    yd=1+jd-yjd;       n=1;    wd=1;    md=m=CALUNKNOWN; // unknown    }    }    -  protected string _sprintf(int t,mapping m) +  protected string _sprintf(int t)    {   // return sprintf("week y=%d yjd=%d w=%d jd=%d yd=%d n=%d nd=%d",   // y,yjd,w,jd,yd,n,number_of_days());    switch (t)    {    case 'O':    if (n!=1)    return sprintf("Week(%s)",nice_print_period());    return sprintf("Week(%s)",nice_print());    case 't':    return "Calendar."+calendar_name()+".Week";    default: -  return ::_sprintf(t,m); +  return ::_sprintf(t);    }    }       int year_no()    {    return wy>0?wy:-1+wy;    }       int year_day()    //! Can be less than 1 for the first week of the year if it begins
pike.git/lib/modules/Calendar.pmod/YMD.pike:2302:    {    create_unixtime_default((int)((jd-2440588)*86400));    }    else    {    [y,yjd]=year_from_julian_day(jd=_jd);    yd=1+jd-yjd;    }    }    -  protected string _sprintf(int t,mapping m) +  protected string _sprintf(int t)    {    switch (t)    {    case 'O':    catch {    if (n!=1)    return sprintf("Day(%s)",nice_print_period());    return sprintf("Day(%s)",nice_print());    };    return sprintf("Day(%d)", unix_time());    case 't':    return "Calendar."+calendar_name()+".Day";    default: -  return ::_sprintf(t,m); +  return ::_sprintf(t);    }    }       string nice_print()    {    if (m==CALUNKNOWN) make_month();    if (wd==CALUNKNOWN) make_week();    return    sprintf("%s %s %s %s",    week_day_shortname(),
pike.git/lib/modules/Calendar.pmod/YMD.pike:2920: Inside #if undefined(NOCATCH)
  #ifndef NOCATCH    if (catch {   #else    werror("%O\n",m);   #endif    if (m->n && m->n!="") return 0;       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) +  if (!undefinedp(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 (sizeof(m->d)==6)    {    [y,mo,d]=(array(int))(m->d/2);
pike.git/lib/modules/Calendar.pmod/YMD.pike:2944:    y+=dwim_year->past_century;    }    else if (sizeof(m->d)==8)    [y,mo,d]=(array(int))array_sscanf(m->d,"%4s%2s%2s");    else return 0;       low=m->day=cal->Day(y,mo,d);    }    else    { -  if (!zero_type(m->Y)) m->year=cal->Year(m->Y); +  if (!undefinedp(m->Y)) m->year=cal->Year(m->Y);    else if (m->y)    {    if (sizeof(m->y)<3)    {    m->y=(int)m->y;    // FIXME? these should be adjustable for different calendars.    if (m->y<dwim_year->past_lower)    m->y+=dwim_year->current_century;    else if (m->y<dwim_year->past_upper)    m->y+=dwim_year->past_century;
pike.git/lib/modules/Calendar.pmod/YMD.pike:2967:    }    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)) +  if (!undefinedp(m->D))    m->day=low=(m->month||(context?context->month():cal->Month()))    ->day((int)m->D); -  else if (!zero_type(m->a)) +  else if (!undefinedp(m->a))    m->day=low=(m->month || m->year)->day(m->a); -  else if (!zero_type(m->e)) +  else if (!undefinedp(m->e))    m->day=low=(m->week||(context?context->week():cal->Week()))    ->day(m->e);    else    low=m->day=context?context->day():cal->Day();    -  if (m->day && zero_type(m->Y) && zero_type(m->y) && m->e) +  if (m->day && undefinedp(m->Y) && undefinedp(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;
pike.git/lib/modules/Calendar.pmod/YMD.pike:3027:    if (m->t)    {    if (sizeof(m->t)==6)    [h,mi,s]=(array(int))(m->t/2),g="second";    else if (sizeof(m->t)==4)    [h,mi]=(array(int))(m->t/2),g="minute";    else return 0;    }    else    { -  if (!zero_type(m->h)) h=m->h,g="hour"; -  if (!zero_type(m->m)) mi=m->m,g="minute"; -  if (!zero_type(m->s)) s=m->s,g="second"; -  if (!zero_type(m->f)) sub_second=(float)("0."+m->f+"0"*9)[..10]; +  if (!undefinedp(m->h)) h=m->h,g="hour"; +  if (!undefinedp(m->m)) mi=m->m,g="minute"; +  if (!undefinedp(m->s)) s=m->s,g="second"; +  if (!undefinedp(m->f)) sub_second=(float)("0."+m->f+"0"*9)[..10];    }    -  if (!zero_type(m->p)) +  if (!undefinedp(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    low = dwim_zone(low,m->z,g,h,mi,s);    else if (g)    low = dwim_tod(low,g,h,mi,s); -  else if (!zero_type(m->S)) +  else if (!undefinedp(m->S))    low = Second(m->S);    if (sub_second)    low = low->fraction(sub_second);    return low;      #ifndef NOCATCH    })   #endif    return 0;   }
pike.git/lib/modules/Calendar.pmod/YMD.pike:3212:   //! "dwim" means do-what-i-mean.      TimeofDay dwim_time(string what,void|TimeRange cx)   {    TimeofDay t;      // #define COLON "$*[ :]"   #define COLON ":"   #define SPACED(X) replace(X," ","%*[ ]")    -  what = String.trim_all_whites(what); +  what = String.trim(what);       if (sizeof(what)>22 &&    (t=http_time(what,cx))) return t;    if (sizeof(what)>12 &&    (t=parse(SPACED("%e %M %D %h:%m:%s %Y"),what,cx))) return t; // ctime    if (sizeof(what)>15 &&    (t=parse(SPACED("%e %M %D %h:%m:%s %z %Y"),what,cx))) return t;    if (sizeof(what)>19 &&    (t=parse(SPACED("%e %M %D %h:%m:%s %z DST %Y"),what,cx))) return t;       foreach ( dwim_day_strings +    ({""}),    string dayformat )    foreach ( ({ "%t %z",    "T%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", -  +  "T%h"COLON"%m"COLON"%s %z", +  "T%h"COLON"%m"COLON"%s%z", +  "T%h"COLON"%m"COLON"%s", +  "T%h"COLON"%m %z", +  "T%h"COLON"%m%z", +  "T%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%*[ ]%p", +  "%t%z", +  "%t", +  }),    string todformat )    {   // werror("try: %O\n %O\n",   // dayformat+"%*[ ,]"+todformat,   // todformat+"%*[ ,]"+dayformat);    if (dayformat=="")    {    if ( (t=parse(todformat+"%*[ ]%n",what,cx)) ) return t;    }    else    { -  if ( (t=parse(dayformat+"%*[ ,:T]"+todformat,what,cx)) ) 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);   }      // Parses time according to HTTP 1.1 (RFC 2616) HTTP-date token.   TimeofDay http_time(string what, void|TimeRange cx)   {