00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef lint
00021 static const char rcsid[] = "$Id: timeutil.c,v 1.5 2004-10-17 05:14:06 stdarg Exp $";
00022 #endif
00023
00024 #include <eggdrop/eggdrop.h>
00025
00026
00027
00028
00029 char *duration_to_string(time_t difftime)
00030 {
00031 static char durstring[64];
00032 time_t days, hours, mins;
00033 int len;
00034
00035 *durstring = '\0';
00036 days = difftime/86400;
00037 difftime -= days*86400;
00038 hours = difftime/3600;
00039 difftime -= hours*3600;
00040 mins = difftime/60;
00041
00042 if (days)
00043 snprintf(durstring, sizeof(durstring), "%ld %s", days, days==1?"day":"days");
00044 if (hours) {
00045 len = strlen(durstring);
00046 snprintf(&durstring[len], sizeof(durstring) - len, "%s%ld %s",
00047 !len?"":mins?", ":"and ", hours, hours==1?"hour":"hours");
00048 }
00049 if (mins) {
00050 len = strlen(durstring);
00051 snprintf(&durstring[len], sizeof(durstring) - len, "%s%ld %s",
00052 len?" and ":"", mins, mins==1?"minute":"minutes");
00053 }
00054
00055 return strlen(durstring)?durstring:"less than minute";
00056 }
00057
00058
00059 time_t parse_expire_string(char *s)
00060 {
00061 time_t tmp, exptime = 0;
00062 char *endptr = NULL;
00063
00064
00065
00066
00067
00068 s++;
00069 while (*s) {
00070 tmp = strtoul(s, &endptr, 10);
00071 switch(tolower(*endptr)) {
00072 case 'd':
00073 if (tmp > 365)
00074 tmp = 365;
00075 exptime += 86400 * tmp;
00076 break;
00077 case 'h':
00078 if (tmp > 8760)
00079 tmp = 8760;
00080 exptime += 3600 * tmp;
00081 break;
00082 case 'm':
00083 if (tmp > 525600)
00084 tmp = 525600;
00085 exptime += 60 * tmp;
00086 break;
00087 default:
00088 return 0;
00089 }
00090 s = endptr+1;
00091 }
00092 return exptime;
00093 }