Back to index

courier  0.68.2
pcplocale.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 2001 Double Precision, Inc.  See COPYING for
00003 ** distribution information.
00004 */
00005 
00006 
00007 #include "config.h"
00008 #include "pcp.h"
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include <string.h>
00012 #include <ctype.h>
00013 #include <fcntl.h>
00014 #include <errno.h>
00015 
00016 #include "pcp.h"
00017 
00018 const char *pcp_am()
00019 {
00020        char *am_buf=0;
00021        time_t t;
00022        struct tm *tmptr;
00023        char tim_buf[40];
00024        int n;
00025 
00026        if (am_buf)
00027               return (am_buf);
00028 
00029        time(&t);
00030        tmptr=localtime(&t);
00031        tmptr->tm_hour=6;
00032 
00033        n=strftime(tim_buf, sizeof(tim_buf), "%p", tmptr);
00034        tim_buf[n]=0;
00035        am_buf=strdup(tim_buf);
00036        return (am_buf);
00037 }
00038 
00039 const char *pcp_pm()
00040 {
00041        char *am_buf=0;
00042        time_t t;
00043        struct tm *tmptr;
00044        char tim_buf[40];
00045        int n;
00046 
00047        if (am_buf)
00048               return (am_buf);
00049 
00050        time(&t);
00051        tmptr=localtime(&t);
00052        tmptr->tm_hour=18;
00053 
00054        n=strftime(tim_buf, sizeof(tim_buf), "%p", tmptr);
00055        tim_buf[n]=0;
00056        am_buf=strdup(tim_buf);
00057        return (am_buf);
00058 }
00059 
00060 static const char *wday_func(unsigned n, const char *fmt)
00061 {
00062        static char buf[40];
00063        time_t t;
00064        struct tm *tmptr;
00065        int dir=0;
00066 
00067        if (n >= 7)
00068               return ("");
00069 
00070        time(&t);
00071        tmptr=localtime(&t);
00072 
00073        while (tmptr->tm_wday != n)
00074        {
00075               if (dir == 0)
00076                      dir= tmptr->tm_mday > 15 ? -1:1;
00077 
00078               if (dir < 0)
00079               {
00080                      tmptr->tm_mday--;
00081                      tmptr->tm_wday= (tmptr->tm_wday + 6) % 7;
00082               }
00083               else
00084               {
00085                      tmptr->tm_mday++;
00086                      tmptr->tm_wday= (tmptr->tm_wday + 1) % 7;
00087               }
00088        }
00089 
00090        dir=strftime(buf, sizeof(buf), fmt, tmptr);
00091        buf[dir]=0;
00092        return (buf);
00093 }
00094 
00095 const char *pcp_wdayname(unsigned n)
00096 {
00097        return (wday_func(n, "%a"));
00098 }
00099 
00100 const char *pcp_wdayname_long(unsigned n)
00101 {
00102        return (wday_func(n, "%A"));
00103 }
00104 
00105 int pcp_wday(const char *p)
00106 {
00107        int i;
00108 
00109        for (i=0; i<7; i++)
00110        {
00111               const char *q;
00112 
00113               if (strcasecmp(p, pcp_wdayname(i)) == 0)
00114                      return (i);
00115 
00116               q=pcp_wdayname_long(i);
00117 
00118               if (strncasecmp(p, q, strlen(q)) == 0)
00119                      return (i);
00120        }
00121        return (-1);
00122 }
00123 
00124 static const char *month_func(unsigned n, const char *fmt)
00125 {
00126        static char buf[80];
00127        time_t t;
00128        struct tm *tmptr;
00129        int dir=0;
00130 
00131        if (n >= 12)
00132               return ("");
00133 
00134        time(&t);
00135        tmptr=localtime(&t);
00136 
00137        tmptr->tm_mday=1;
00138        tmptr->tm_mon=n;
00139        tmptr->tm_hour=12;
00140        tmptr->tm_min=0;
00141        tmptr->tm_sec=0;
00142 
00143        dir=strftime(buf, sizeof(buf), fmt, tmptr);
00144        buf[dir]=0;
00145        return (buf);
00146 }
00147 
00148 const char *pcp_monthname(unsigned n)
00149 {
00150        return (month_func(n, "%b"));
00151 }
00152 
00153 const char *pcp_monthname_long(unsigned n)
00154 {
00155        return (month_func(n, "%B"));
00156 }
00157 
00158 int pcp_month(const char *p)
00159 {
00160        int i;
00161 
00162        for (i=0; i<12; i++)
00163        {
00164               const char *q;
00165 
00166               if (strcasecmp(p, pcp_monthname(i)) == 0)
00167                      return (i);
00168 
00169               q=pcp_monthname_long(i);
00170 
00171               if (strncasecmp(p, q, strlen(q)) == 0)
00172                      return (i);
00173        }
00174        return (-1);
00175 }
00176 
00177 static int is_digit(const char *p)
00178 {
00179        while (*p)
00180        {
00181               if (!isdigit((int)(unsigned char)*p))
00182                      return (0);
00183               ++p;
00184        }
00185        return (1);
00186 }
00187 
00188 static int fix_year(int year)
00189 {
00190        time_t now=time(NULL);
00191        int nn;
00192 
00193        if (year < 100)
00194        {
00195               struct tm *tmptr;
00196 
00197               tmptr=localtime(&now);
00198 
00199               nn=tmptr->tm_year + 1900;
00200 
00201               if (year >= (nn % 100))
00202                      year += (nn / 100 * 100);
00203               else
00204                      year += (nn / 100 * 100) + 100;
00205        }
00206        return (year);
00207 }
00208 
00209 static int get_year(int mon, int day)
00210 {
00211        time_t t=time(NULL);
00212        struct tm *tmptr=localtime(&t);
00213        int m=tmptr->tm_mon+1;
00214 
00215        int year=tmptr->tm_year + 1900;
00216 
00217        if (m > mon || (m == mon && tmptr->tm_mday > day))
00218               ++year;
00219 
00220        return (year);
00221 }
00222 
00223 static int scan_mdy(int sscan_rc, int *mon, int *day, int *year)
00224 {
00225        time_t t;
00226        struct tm *tmptr;
00227 
00228        switch (sscan_rc) {
00229        case 0:
00230               return (-1);
00231        case 1:
00232               return (-1);
00233        case 2:
00234               time(&t);
00235               tmptr=localtime(&t);
00236 
00237               *year=tmptr->tm_year + 1900;
00238 
00239               if (*mon < tmptr->tm_mon+1 ||
00240                   (*mon == tmptr->tm_mon+1 && *day < tmptr->tm_mday))
00241                      ++ *year;
00242               break;
00243        case 3:
00244               if (*year < 0)
00245                      return (-1);
00246               *year=fix_year(*year);
00247               break;
00248        }
00249        if (*mon <= 0 || *day <= 0)
00250               return (-1);
00251        return (0);
00252 }
00253 
00254 time_t pcp_parse_datetime(int *argn,
00255                        int argc,
00256                        char **argv,
00257                        struct pcp_parse_datetime_info *info)
00258 {
00259        int mon=-1, day=-1, year=0;
00260        int hour=0, min=0, sec=-1;
00261        time_t t;
00262        struct tm *tmptr, tmsave;
00263        const char *today="today";
00264        const char *tomorrow="tomorrow";
00265        int nn;
00266        int i;
00267 
00268        time(&t);
00269 
00270        if (info && info->today_name)
00271               today=info->today_name;
00272 
00273        if (info && info->tomorrow_name)
00274               tomorrow=info->tomorrow_name;
00275 
00276        while (year == 0 || sec < 0 || day < 0)
00277        {
00278               const char *p;
00279               const char *q;
00280 
00281               if (*argn >= argc)
00282                      break;
00283 
00284               p=argv[*argn];
00285 
00286               if (strcasecmp(p, today) == 0)
00287               {
00288                      if (year || day >= 0)
00289                             return (0);   /* Already specified */
00290 
00291                      tmptr=localtime(&t);
00292 
00293                      mon=tmptr->tm_mon+1;
00294                      day=tmptr->tm_mday;
00295                      year=tmptr->tm_year + 1900;
00296                      ++ *argn;
00297                      continue;
00298               }
00299 
00300               if (strcasecmp(p, tomorrow) == 0)
00301               {
00302                      time_t t2;
00303 
00304                      if (year || day >= 0)
00305                             return (0);   /* Already specified */
00306 
00307                      tmptr=localtime(&t);
00308 
00309                      tmptr->tm_hour=12;
00310                      tmptr->tm_min=0;
00311                      tmptr->tm_sec=0;
00312 
00313                      t2=mktime(tmptr);
00314                      if (t2 == (time_t)-1)
00315                             return (0);
00316 
00317                      t2 += 24 * 60 * 60;
00318 
00319                      tmptr=localtime(&t2);
00320 
00321                      mon=tmptr->tm_mon+1;
00322                      day=tmptr->tm_mday;
00323                      year=tmptr->tm_year + 1900;
00324                      ++ *argn;
00325                      continue;
00326               }
00327 
00328               if ((i=pcp_wday(p)) >= 0)
00329               {
00330                      time_t t2;
00331 
00332                      if (year || day >= 0)
00333                             return (0);   /* Already specified */
00334 
00335                      t2=t;
00336 
00337                      do
00338                      {
00339                             tmptr=localtime(&t2);
00340 
00341                             tmptr->tm_hour=12;
00342                             tmptr->tm_min=0;
00343                             tmptr->tm_sec=0;
00344 
00345                             t2=mktime(tmptr);
00346                             if (t2 == (time_t)-1)
00347                                    return (0);
00348 
00349                             t2 += 24 * 60 * 60;
00350 
00351                             tmptr=localtime(&t2);
00352                      } while (tmptr->tm_wday != i);
00353 
00354                      mon=tmptr->tm_mon+1;
00355                      day=tmptr->tm_mday;
00356                      year=tmptr->tm_year + 1900;
00357                      ++ *argn;
00358                      continue;
00359               }
00360 
00361               if ((i=pcp_month(p)) >= 0)
00362               {
00363                      if (mon >= 0)
00364                             return (0);
00365                      mon=i+1;
00366 
00367                      if (++*argn >= argc)
00368                             return (0);
00369                      day=atoi(argv[*argn]);
00370                      if (day <= 0)
00371                             return (0);
00372                      ++*argn;
00373                      continue;
00374               }
00375 
00376               if (strchr(p, '/'))
00377               {
00378                      if (mon > 0 || day > 0 || year > 0)
00379                             return (0);
00380 
00381                      if (scan_mdy(sscanf(p, "%d/%d/%d", &mon, &day, &year),
00382                                  &mon, &day, &year))
00383                             return (0);
00384                      ++*argn;
00385                      continue;
00386               }
00387 
00388               if (strchr(p, '.'))
00389               {
00390                      if (mon > 0 || day > 0 || year > 0)
00391                             return (0);
00392 
00393                      if (scan_mdy(sscanf(p, "%d.%d.%d", &year, &mon, &day),
00394                                  &mon, &day, &year))
00395                             return (0);
00396                      ++*argn;
00397                      continue;
00398               }
00399 
00400               if (is_digit(p) && year == 0 &&
00401                   mon > 0 && day > 0 && sec < 0)
00402               {
00403                      year=atoi(p);
00404                      year=fix_year(year);
00405                      ++*argn;
00406                      continue;
00407               }
00408 
00409               nn=atoi(p);
00410 
00411               if (is_digit(p))
00412               {
00413                      ++*argn;
00414 
00415                      if (*argn >= argc)
00416                             return (0);
00417 
00418                      p=argv[*argn];
00419 
00420                      for (i=0; i<12; i++)
00421                             if (strcasecmp(p, pcp_monthname(i)) == 0 ||
00422                                 strcasecmp(p, pcp_monthname_long(i)) == 0)
00423                                    break;
00424 
00425                      if (i < 12)
00426                      {
00427                             if (nn == 0)
00428                                    return (0);
00429 
00430                             if (mon >= 0)
00431                                    return (0);
00432                             mon=i+1;
00433                             day=nn;
00434                             ++*argn;
00435                             continue;
00436                      }
00437 
00438                      if (strcasecmp(p, pcp_am()) == 0)
00439                      {
00440                             if (hour >= 0)
00441                                    return (0);
00442                             hour=nn;
00443                             min=0;
00444                             sec=0;
00445                             ++*argn;
00446                             if (hour == 12)
00447                                    hour=0;
00448 
00449                             continue;
00450                      }
00451 
00452                      if (strcasecmp(p, pcp_pm()) == 0)
00453                      {
00454                             if (hour >= 0)
00455                                    return (0);
00456                             hour=nn+12;
00457                             min=0;
00458                             sec=0;
00459                             ++*argn;
00460                             if (hour == 24)
00461                                    hour=12;
00462                             continue;
00463                      }
00464 
00465                      if (hour >= 0)
00466                             return (0);
00467                      hour=nn;
00468                      min=0;
00469                      sec=0;
00470                      continue;
00471               }
00472 
00473               if (!isdigit((int)(unsigned char)*p))
00474                      break;
00475 
00476               if (mon > 0 && day > 0 && year == 0)
00477               {
00478                      year=get_year(mon, day);
00479               }
00480 
00481 
00482               switch (sscanf(p, "%d:%d:%d", &hour, &min, &sec)) {
00483               case 0:
00484                      return (0);
00485               case 1:
00486                      if (sec >= 0)
00487                             return (0);
00488 
00489                      min=sec=0;
00490                      break;
00491               case 2:
00492                      if (sec >= 0)
00493                             return (0);
00494 
00495                      sec=0;
00496                      break;
00497               default:
00498                      if (sec >= 0)
00499                             return (0);
00500                      break;
00501               }
00502 
00503               for (q=p; *q; q++)
00504               {
00505                      if (!isdigit((int)(unsigned char)*q) && *q != ':')
00506                             break;
00507               }
00508 
00509               ++*argn;
00510 
00511               if (*q == 0)
00512               {
00513                      q= *argn < argc ? argv[*argn]:"";
00514                      if (strcasecmp(p, pcp_am()) == 0)
00515                      {
00516                             if (hour == 12)
00517                                    hour=0;
00518                             ++*argn;
00519                             continue;
00520                      }
00521                      if (strcasecmp(p, pcp_pm()) == 0)
00522                      {
00523                             hour += 12;
00524                             if (hour == 24)
00525                                    hour=12;
00526                             ++*argn;
00527                             continue;
00528                      }
00529                      continue;
00530               }
00531 
00532               if (strcasecmp(q, pcp_am()) == 0)
00533               {
00534                      if (hour == 12)
00535                             hour=0;
00536                      continue;
00537               }
00538               if (strcasecmp(q, pcp_pm()) == 0)
00539               {
00540                      hour += 12;
00541                      if (hour == 24)
00542                             hour=12;
00543                      continue;
00544               }
00545               return (0);
00546        }
00547 
00548        if (sec < 0)
00549        {
00550               hour=min=sec=0;
00551        }
00552 
00553        if (day <= 0 && mon < 0 && year <= 0)
00554        {
00555               tmptr=localtime(&t);
00556 
00557               mon=tmptr->tm_mon+1;
00558               day=tmptr->tm_mday;
00559               year=tmptr->tm_year + 1900;
00560        }
00561        if (day < 0)
00562               return (0);
00563 
00564        tmptr=localtime(&t);
00565        tmptr->tm_mon=mon-1;
00566        tmptr->tm_mday=day;
00567        tmptr->tm_year=year - 1900;
00568        tmptr->tm_hour=hour;
00569        tmptr->tm_min=min;
00570        tmptr->tm_sec=sec;
00571 
00572        tmsave= *tmptr;
00573 
00574        t=mktime(tmptr);
00575        if (t == (time_t)-1)
00576               return (0);
00577 
00578        /* Could be different due to altzone change, so we do it again */
00579 
00580        tmptr=localtime(&t);
00581        tmptr->tm_mon=tmsave.tm_mon;
00582        tmptr->tm_mday=tmsave.tm_mday;
00583        tmptr->tm_year=tmsave.tm_year;
00584        tmptr->tm_hour=tmsave.tm_hour;
00585        tmptr->tm_min=tmsave.tm_min;
00586        tmptr->tm_sec=tmsave.tm_sec;
00587        t=mktime(tmptr);
00588        if (t == (time_t)-1)
00589               return (0);
00590        return (t);
00591 }
00592 
00593 static time_t add_week(time_t n, unsigned cnt)
00594 {
00595        struct tm *tmptr=localtime(&n);
00596        struct tm tmsave= *tmptr;
00597 
00598        if (!tmptr)
00599               return (0);
00600 
00601        tmptr->tm_hour=13;
00602        tmptr->tm_min=0;
00603        tmptr->tm_sec=0;
00604 
00605        n=mktime(tmptr);
00606 
00607        if (n == (time_t)-1)
00608               return (0);
00609 
00610        n += cnt * 7 * 24 * 60 * 60;
00611 
00612        tmptr=localtime(&n);
00613 
00614        tmptr->tm_hour=tmsave.tm_hour;
00615        tmptr->tm_min=tmsave.tm_min;
00616        tmptr->tm_sec=tmsave.tm_sec;
00617 
00618        n=mktime(tmptr);
00619        if (n == (time_t)-1)
00620               return (0);
00621        return (n);
00622 }
00623 
00624 static time_t add_month(time_t n, unsigned cnt)
00625 {
00626        time_t a, b;
00627 
00628        struct tm *tmptr=localtime(&n);
00629        int mday, d, m, y, hh, mm, ss;
00630        int is_eom=0;
00631        int i;
00632 
00633        if (!tmptr)
00634               return (0);
00635 
00636        mday=d=tmptr->tm_mday;
00637        m=tmptr->tm_mon+1;
00638        y=tmptr->tm_year + 1900;
00639        hh=tmptr->tm_hour;
00640        mm=tmptr->tm_min;
00641        ss=tmptr->tm_sec;
00642 
00643        if (pcp_parse_ymd(y, m, d, &a, &b))
00644               return (0);
00645 
00646        tmptr=localtime(&b);
00647        if (!tmptr)
00648               return (0);
00649 
00650        if (tmptr->tm_mday == 1)    /* The original date is end of month */
00651               is_eom=1;
00652 
00653        /* Go to the next month, then backtrack */
00654 
00655        ++cnt;
00656 
00657        y += (cnt / 12);
00658        m += cnt % 12;
00659 
00660        if (m > 12)
00661        {
00662               m -= 12;
00663               ++y;
00664        }
00665 
00666        tmptr->tm_mday=1;
00667        tmptr->tm_mon=m-1;
00668        tmptr->tm_year= y - 1900;
00669        tmptr->tm_hour=13;
00670        tmptr->tm_min=0;
00671        tmptr->tm_sec=0;
00672 
00673        if ((a=mktime(tmptr)) == (time_t)-1)
00674               return (0);
00675 
00676        a -= 24 * 60 * 60;
00677 
00678        for (i=0; i<32; i++) /* Stopgap */
00679        {
00680               tmptr=localtime(&a);
00681               if (!tmptr)
00682                      return (0);
00683 
00684               if (is_eom || tmptr->tm_mday <= mday)
00685                      break;
00686 
00687               tmptr->tm_hour=13;
00688               tmptr->tm_min=0;
00689               tmptr->tm_sec=0;
00690               if ((a=mktime(tmptr)) == (time_t)-1)
00691                      return (0);
00692 
00693               a -= 24 * 60 * 60;
00694        }
00695 
00696        tmptr->tm_hour=hh;
00697        tmptr->tm_min=mm;
00698        tmptr->tm_sec=ss;
00699 
00700        if ((a=mktime(tmptr)) == (time_t)-1
00701            || (tmptr=localtime(&a)) == NULL)
00702               return (0);
00703 
00704        tmptr->tm_hour=hh;
00705        tmptr->tm_min=mm;
00706        tmptr->tm_sec=ss;
00707 
00708        if ((a=mktime(tmptr)) == (time_t)-1)
00709               return (0);
00710 
00711        return (a);
00712 }
00713 
00714 int pcp_parse_datetime_until(time_t start, time_t end,
00715                           int *argn,
00716                           int argc,
00717                           char **argv,
00718 
00719                           int recurring_type,
00720 
00721                           int (*save_date_time)(time_t, time_t, void *),
00722                           void *voidfunc)
00723 {
00724        int month=0, day=0, year=0;
00725        const char *p;
00726        struct tm *tmptr;
00727 
00728        time_t new_start, new_end;
00729        unsigned cnt;
00730 
00731        while (month == 0 || day == 0)
00732        {
00733               if (*argn >= argc)
00734                      return (-1);
00735 
00736               p=argv[ (*argn)++ ];
00737 
00738               if (month == 0 && day == 0 && year == 0)
00739               {
00740                      if (strchr(p, '/'))
00741                      {
00742                             if (scan_mdy(sscanf(p, "%d/%d/%d",
00743                                               &month, &day, &year),
00744                                         &month, &day, &year))
00745                                    return (-1);
00746                             continue;
00747                      }
00748 
00749                      if (strchr(p, '.'))
00750                      {
00751                             if (scan_mdy(sscanf(p, "%d.%d.%d",
00752                                               &year, &month, &day),
00753                                         &month, &day, &year))
00754                                    return (-1);
00755                             continue;
00756                      }
00757               }
00758 
00759               if (is_digit(p))
00760               {
00761                      if (day > 0)
00762                             return (-1);
00763                      day=atoi(p);
00764                      if (day <= 0)
00765                             return (-1);
00766                      continue;
00767               }
00768 
00769               if (month > 0)
00770                      return (-1);
00771 
00772               month=pcp_month(p);
00773 
00774               if (month < 0)
00775                      return (-1);
00776               ++month;
00777        }
00778 
00779        if ( year == 0 && *argn < argc && is_digit(argv[*argn]))
00780        {
00781               year=atoi(argv[*argn++]);
00782 
00783               year=fix_year(year);
00784        }
00785        else
00786        {
00787               if (year == 0)
00788                      year=get_year(month, day);
00789        }
00790 
00791        for (cnt=0; cnt < 100; cnt++)
00792        {
00793               int y, m;
00794 
00795               switch (recurring_type) {
00796               case PCP_RECURRING_MONTHLY:
00797                      new_start=add_month(start, cnt);
00798                      new_end=add_month(end, cnt);
00799                      break;
00800               case PCP_RECURRING_ANNUALLY:
00801                      new_start=add_month(start, cnt*12);
00802                      new_end=add_month(end, cnt*12);
00803                      break;
00804               default:
00805                      new_start=add_week(start, cnt);
00806                      new_end=add_week(end, cnt);
00807               }
00808 
00809               if (!new_start || !new_end)
00810                      continue;
00811 
00812               tmptr=localtime(&new_start);
00813 
00814               y=tmptr->tm_year + 1900;
00815               m=tmptr->tm_mon + 1;
00816 
00817               if (y > year)
00818                      break;
00819               if (y == year)
00820               {
00821                      if (m > month)
00822                             break;
00823                      if (m == month)
00824                      {
00825                             if (tmptr->tm_mday > day)
00826                                    break;
00827                      }
00828               }
00829 
00830               y= (*save_date_time)(new_start, new_end, voidfunc);
00831               if (y)
00832                      return (y);
00833        }
00834        return (0);
00835 }
00836 
00837 int pcp_fmttime(char *ubuf, size_t cnt, time_t t, int flags)
00838 {
00839        struct tm *tmptr=localtime(&t);
00840        char datebuf[100];
00841        char timebuf[100];
00842        char buf[201];
00843 
00844        char *p;
00845 
00846        if (!tmptr)
00847               return (-1);
00848 
00849        if (strftime(datebuf, sizeof(datebuf), "%x", tmptr) <= 0)
00850               datebuf[0]=0;
00851 
00852        if (strftime(timebuf, sizeof(timebuf), "%X", tmptr) <= 0)
00853               timebuf[0]=0;
00854 
00855        if (tmptr->tm_hour == 0 && tmptr->tm_min == 0 && tmptr->tm_sec == 0
00856            && (flags & FMTTIME_TIMEDROP))
00857               timebuf[0]=0;
00858 
00859        /* Try to drop minutes from timebuf */
00860 
00861        for (p=timebuf; *p; p++)
00862        {
00863               if (!isdigit((int)(unsigned char)*p) &&
00864                   p[1] == '0' && p[2] == '0')
00865               {
00866                      char *q;
00867 
00868                      for (q=p+3; *q; q++)
00869                             if (isdigit((int)(unsigned char)*q))
00870                                 break;
00871 
00872                      if (!*q)
00873                      {
00874                             for (q=p+3; (*p=*q) != 0; p++, q++)
00875                                    ;
00876                             break;
00877                      }
00878               }
00879        }
00880 
00881        buf[0]=0;
00882        if (!flags || (flags & FMTTIME_DATE))
00883        {
00884               strcpy(buf, datebuf);
00885        }
00886 
00887        if (!flags || (flags & FMTTIME_TIME))
00888        {
00889               if (timebuf[0])
00890               {
00891                      if (buf[0])
00892                             strcat(buf, " ");
00893                      strcat(buf, timebuf);
00894               }
00895        }
00896 
00897        if (strlen(buf)+1 > cnt)
00898        {
00899               errno=ENOSPC;
00900               return (-1);
00901        }
00902 
00903        strcpy(ubuf, buf);
00904        return (0);
00905 }
00906 
00907 int pcp_fmttimerange(char *ubuf, size_t cnt, time_t from, time_t to)
00908 {
00909        char date1[100];
00910        char time1[100];
00911        char date2[100];
00912        char time2[100];
00913        char fmtbuf[500];
00914 
00915        if (pcp_fmttime(date1, sizeof(date1), from, FMTTIME_DATE) < 0)
00916               date1[0]=0;
00917 
00918        if (pcp_fmttime(time1, sizeof(time1), from,
00919                      FMTTIME_TIME | FMTTIME_TIMEDROP) < 0)
00920               time1[0]=0;
00921 
00922        if (pcp_fmttime(date2, sizeof(date2), to, FMTTIME_DATE) < 0)
00923               date2[0]=0;
00924        if (pcp_fmttime(time2, sizeof(time2), to,
00925                      FMTTIME_TIME | FMTTIME_TIMEDROP) < 0)
00926               time2[0]=0;
00927 
00928        if (time1[0] == 0 && time2[0] == 0)
00929        {
00930               sprintf(fmtbuf, strcmp(date1, date2)
00931                      ? "%s-%s":"%s", date1, date2);
00932        }
00933        else
00934        {
00935               if (!time1[0] && pcp_fmttime(time1, sizeof(time1), from,
00936                                         FMTTIME_TIME) < 0)
00937                      time1[0]=0;
00938 
00939               if (!time2[0] && pcp_fmttime(time2, sizeof(time2), to,
00940                                         FMTTIME_TIME) < 0)
00941                      time2[0]=0;
00942 
00943               if (strcmp(date1, date2) == 0)
00944                      sprintf(fmtbuf, "%s %s-%s", date1, time1, time2);
00945               else
00946                      sprintf(fmtbuf, "%s %s-%s %s", date1, time1,
00947                             date2, time2);
00948        }
00949 
00950        if (strlen(fmtbuf)+1 > cnt)
00951        {
00952               errno=ENOSPC;
00953               return (-1);
00954        }
00955 
00956        strcpy(ubuf, fmtbuf);
00957        return (0);
00958 }