Branch: Tag:

1998-11-20

1998-11-20 22:36:37 by Mirar (Pontus Hagland) <pike@sort.mirar.org>

stardate added, parse function in calendar.gregorian (and derivates)

Rev: lib/modules/Calendar.pmod/Gregorian.pmod:1.17
Rev: lib/modules/Calendar.pmod/Stardate.pmod:1.1

8:   //! class      array(string) month_names= -  ({"January","February","March","April","May","June","July","August", +  ({"January","February","Mars","April","May","June","July","August",    "September","October","November","December"});      array(string) week_day_names=
53:      //-- standard methods -----------------------------------------------    +  string is() +  { +  return "year"; +  } +     array(string) lesser()    {    return ({"month","week","day"});
282:      //-- standard methods -----------------------------------------------    +  string is() +  { +  return "month"; +  } +     array(string) lesser()    {    return ({"day"});
425:      //-- standard methods -----------------------------------------------    +  string is() +  { +  return "week"; +  } +     array(string) lesser()    {    return ({"day"});
592:      //-- standard methods -----------------------------------------------    +  string is() +  { +  return "day"; +  } +     array(string) greater()    {    return ({"year","month","week"});
602:    return ({"hour"});    }    -  void create(int ... arg) +  void create(int|object ... arg)    {    if (!sizeof(arg))    {
610:    y=1900+t->year;    d=t->yday;    } +  else if (sizeof(arg)==1) +  { +  int jd; +  +  if (objectp(arg[0])) +  jd=(int)((arg[0]->julian_day||arg[0]->julian_day_f)());    else -  +  jd=arg[0]; +  +  object yo; +  y=(int)(jd/365.2425)-4712; +  yo=vYear(y); +  while (yo->julian_day(0)>jd) yo--; +  write("y="+yo->number()+" yd="+yo->julian_day(0)+" nod="+yo->number_of_days()+"\n"); +  while (jd-yo->julian_day(0)>=yo->number_of_days())    { -  +  yo++; +  write("y="+yo->number()+" yd="+yo->julian_day(0)+" nod="+yo->number_of_days()+"\n"); +  +  } +  y=yo->number(); +  write("y="+y+"\n"); +  d=jd-vYear(y)->julian_day(0); +  write("d="+d+"\n"); +  } +  else +  {    y=arg[0];    d=arg[1];    }
783:      //-- standard methods -----------------------------------------------    +  string is() +  { +  return "hour"; +  } +     array(string) greater()    {    return ({"day"});
959:      //-- standard methods -----------------------------------------------    +  string is() +  { +  return "minute"; +  } +     array(string) greater()    {    return ({"hour"});
1152:      //-- standarm setsoms -----------------------------------------------    +  string is() +  { +  return "second"; +  } +     array(string) greater()    {    return ({"minute","hour","day","month","year"});
1331:      };    + //-- parse functions -----------------------------------------------    -  + //! method object parse(string fmt,string arg) + //! parse a date, create relevant object + //! fmt is in the format "abc%xdef..." + //! where abc and def is matched, and %x is + //! one of those time units: + //! %Y absolute year + //! %y year (70-99 is 1970-1999, 0-69 is 2000-2069) + //! %M month (number, name or short name) (needs %y) + //! %W week (needs %y) + //! %D date (needs %y, %m) + //! %a day (needs %y) + //! %e weekday (needs %y, %w) + //! %h hour (needs %d, %D or %W) + //! %m minute (needs %h) + //! %s second (needs %s) +  + object parse(string fmt,string arg) + { +  string nfmt; +  nfmt=replace(fmt, +  ({"%Y","%y","%M","%W","%D","%a","%e","%h","%m","%s"}), +  ({"%s","%s","%s","%s","%s","%s","%s","%s","%s","%s"})); +  array q=Array.map(replace(fmt,"%%","")/"%", +  lambda(string s){ return s[..0];})-({""}); +  array res=Array.map(array_sscanf(arg,nfmt), +  lambda(string s) +  { +  if (s[0]>='0' && s[0]<='9') +  return array_sscanf(s,"%d")[0]; +  else +  return s; +  }); +  +  if (sizeof(res)<sizeof(q)) +  return 0; // parse error +  +  mapping m=mkmapping(q,res); +  +  if (m->Y) m->year=Year(m->Y); +  else if (m->y) +  { +  if (m->y<70) m->y+=2000; +  else if (m->y<100) m->y+=1900; +  m->year=Year(m->y); +  } +  else m->year=Year(); +  +  object low=m->year; +  +  if (m->M) +  m->month=low=m->year->month(m->M); +  if (m->W) +  m->week=low=m->year->week(m->W); +  +  if (m->D) +  m->day=low=(m->month||Month())->day(m->D); +  else if (m->a) +  m->day=low=m->year->day(m->a); +  else if (m->e) +  m->day=low=(m->week||Week())->day(m->e); +  +  if (m->h) +  low=m->hour=(m->day||Day())->hour(m->h); +  if (m->m) +  low=m->minute=(m->hour||Hour())->minute(m->m); +  if (m->s) +  low=m->second=(m->minute||Minute())->second(m->s); +  +  return low; + } +