2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afs/param.h>
12 #include <sys/types.h>
17 #if defined(AFS_SUN5_ENV) || defined(AFS_NT40_ENV)
25 /* maximum values for relative dates */
27 #define MAX_YEAR_VALUE 0
28 #define MAX_MONTH_VALUE 11
29 #define MAX_DAY_VALUE 30
31 /* for parsing relative expiration dates */
32 static struct parseseqS
36 afs_int32 ps_maxValue;
38 KTIMEDATE_YEAR, 'y', MAX_YEAR_VALUE, /* no max. value */
39 KTIMEDATE_MONTH, 'm', MAX_MONTH_VALUE, /* months max. 12 */
40 KTIMEDATE_DAY, 'd', MAX_DAY_VALUE, /* days max. 31 */
44 /* Encodings to and from relative dates. The caller is responsible for
45 * enforcing appropriate use of these routines
49 /* ktimeRelDate_ToInt32
50 * converts a relative ktime date into an afs_int32.
52 * afs_int32 value of encoded date.
56 ktimeRelDate_ToInt32(kdptr)
57 struct ktime_date *kdptr;
61 retval = (((kdptr->year*(MAX_MONTH_VALUE+1)) +
62 kdptr->month)*(MAX_DAY_VALUE+1)) + kdptr->day;
66 /* Int32To_ktimeRelDate
67 * Convert a relative date encoded in an afs_int32 - back into a ktime_date
71 Int32To_ktimeRelDate(int32Date, kdptr)
73 struct ktime_date *kdptr;
75 bzero(kdptr, sizeof(*kdptr));
77 kdptr->day = int32Date % (MAX_DAY_VALUE + 1);
78 if ( kdptr->day != 0 )
79 kdptr->mask |= KTIMEDATE_DAY;
81 int32Date = int32Date/(MAX_DAY_VALUE + 1);
83 kdptr->month = int32Date % (MAX_MONTH_VALUE + 1);
84 if ( kdptr->month != 0 )
85 kdptr->mask |= KTIMEDATE_MONTH;
87 int32Date = int32Date/(MAX_MONTH_VALUE + 1);
89 kdptr->year = int32Date;
90 if ( kdptr->year != 0 )
91 kdptr->mask |= KTIMEDATE_YEAR;
96 /* ktimeDate_FromInt32
97 * Converts a time in seconds, to a time (in ktime_date format).
98 * Result is a conventional ktime_date structure.
99 * placed in the supplied structure
101 * timeSecs - time in seconds
102 * ktimePtr - ptr to struct for the return value
105 ktimeDate_FromInt32(timeSecs, ktimePtr)
107 struct ktime_date *ktimePtr;
111 timePtr = localtime((time_t *)&timeSecs);
113 /* copy the relevant fields */
114 ktimePtr->sec = timePtr->tm_sec;
115 ktimePtr->min = timePtr->tm_min;
116 ktimePtr->hour = timePtr->tm_hour;
117 ktimePtr->day = timePtr->tm_mday;
118 ktimePtr->month = timePtr->tm_mon+1;
119 ktimePtr->year = timePtr->tm_year;
121 ktimePtr->mask = KTIMEDATE_YEAR | KTIMEDATE_MONTH | KTIMEDATE_DAY |
122 KTIMEDATE_HOUR | KTIMEDATE_MIN | KTIMEDATE_SEC;
127 #define RD_DIGIT_LIMIT 4 /* max. no. digits permitted */
130 * Parses a relative date of the form <n>y<n>m<n>d representing years
131 * months and days. <n> is limited to RD_DIGIT_LIMIT digits in length
132 * and is further restricted by the maximums specified at the head
135 * dateStr - ptr to string to parse. Leading white space ingnored.
137 * returns a ptr to a static ktime_date structure containing
138 * appropriately set fields. The mask field is unused.
139 * 0 - error in date specification
143 ParseRelDate(dateStr, relDatePtr)
145 struct ktime_date *relDatePtr;
147 struct parseseqS *psPtr;
148 afs_int32 value, digit_limit;
149 afs_int32 type_index;
151 bzero(relDatePtr, sizeof(*relDatePtr));
157 while ( isspace(*dateStr) ) /* skip leading whitespace */
160 if ( isdigit(*dateStr) == 0 )
163 digit_limit = RD_DIGIT_LIMIT;
165 while ( isdigit(*dateStr) )
167 value = value*10 + *dateStr - '0';
169 if ( digit_limit-- == 0 )
173 psPtr = &parseseq[type_index];
174 /* determine the units. Search for a matching type character */
175 while ( (psPtr->ps_keychar != *dateStr)
176 && (psPtr->ps_keychar != 0)
180 psPtr = &parseseq[type_index];
183 /* no matching type found */
184 if ( psPtr->ps_keychar == 0 )
187 /* check the bounds on the maximum value. Can't be negative
188 * and if a maximum value is specified, check against it
191 || ((psPtr->ps_maxValue > 0) && (value > psPtr->ps_maxValue))
195 /* save computed value in the relevant type field */
196 switch ( psPtr->ps_field )
199 relDatePtr->year = value;
200 relDatePtr->mask |= KTIMEDATE_YEAR;
203 case KTIMEDATE_MONTH:
204 if ( value > MAX_MONTH_VALUE )
206 relDatePtr->month = value;
207 relDatePtr->mask |= KTIMEDATE_MONTH;
211 if ( value > MAX_DAY_VALUE )
214 relDatePtr->mask |= KTIMEDATE_DAY;
215 relDatePtr->day = value;
221 dateStr++; /* next digit */
225 /* no more chars to process, return the result */
235 * returns a static string representing the relative date. This is in
236 * a format acceptable to the relative date parser.
238 * datePtr - relative date to be converted.
240 * ptr to static string
244 RelDatetoString(datePtr)
245 struct ktime_date *datePtr;
247 static char dateString[64];
248 char tempstring[64], *sptr;
251 sptr = &dateString[0];
253 if ( datePtr->mask & KTIMEDATE_YEAR )
255 sprintf(tempstring, "%-dy", datePtr->year);
256 strcat(sptr, tempstring);
259 if ( datePtr->mask & KTIMEDATE_MONTH )
262 sprintf(tempstring, "%-dm", datePtr->month);
263 strcat(sptr, tempstring);
266 if ( datePtr->mask & KTIMEDATE_DAY )
269 sprintf(tempstring, "%-dd", datePtr->day);
270 strcat(sptr, tempstring);
275 /* Add_RelDate_to_Time
276 * Returns current time with a relative time added. Note that the
277 * computation adds in most significant fields first, i.e. year, month
278 * day etc. Addition of least significant fields would produce different
279 * results (depending on the data).
281 * relDatePtr - a ktime_date containing a relative time specification
283 * returns specified time with relative time added.
287 Add_RelDate_to_Time(relDatePtr, atime)
288 struct ktime_date *relDatePtr;
292 static struct ktime_date absDate;
294 ktimeDate_FromInt32(atime, &absDate); /* convert to ktime */
297 if ( relDatePtr->mask & KTIMEDATE_YEAR )
298 absDate.year += relDatePtr->year;
301 if ( relDatePtr->mask & KTIMEDATE_MONTH )
302 absDate.month += relDatePtr->month;
304 if ( absDate.month > 12 )
306 moreYears = absDate.month / 12;
307 absDate.month = absDate.month % 12;
308 absDate.year += moreYears;
311 /* day computations depend upon month size, so do these in seconds */
312 atime = ktime_InterpretDate(&absDate);
314 if ( relDatePtr->mask & KTIMEDATE_DAY )
315 atime = atime + relDatePtr->day*24*60*60;
317 if ( relDatePtr->mask & KTIMEDATE_HOUR )
318 atime = atime + relDatePtr->hour*60*60;
320 if ( relDatePtr->mask & KTIMEDATE_MIN )
321 atime = atime + relDatePtr->min*60;
323 if ( relDatePtr->mask & KTIMEDATE_SEC )
324 atime = atime + relDatePtr->sec;