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 <afsconfig.h>
11 #include <afs/param.h>
16 #include <sys/types.h>
31 /* maximum values for relative dates */
33 #define MAX_YEAR_VALUE 0
34 #define MAX_MONTH_VALUE 11
35 #define MAX_DAY_VALUE 30
37 /* for parsing relative expiration dates */
38 static struct parseseqS
42 afs_int32 ps_maxValue;
44 { KTIMEDATE_YEAR, 'y', MAX_YEAR_VALUE, }, /* no max. value */
45 { KTIMEDATE_MONTH, 'm', MAX_MONTH_VALUE, }, /* months max. 12 */
46 { KTIMEDATE_DAY, 'd', MAX_DAY_VALUE, }, /* days max. 31 */
50 /* Encodings to and from relative dates. The caller is responsible for
51 * enforcing appropriate use of these routines
55 /* ktimeRelDate_ToInt32
56 * converts a relative ktime date into an afs_int32.
58 * afs_int32 value of encoded date.
62 ktimeRelDate_ToInt32(kdptr)
63 struct ktime_date *kdptr;
67 retval = (((kdptr->year*(MAX_MONTH_VALUE+1)) +
68 kdptr->month)*(MAX_DAY_VALUE+1)) + kdptr->day;
72 /* Int32To_ktimeRelDate
73 * Convert a relative date encoded in an afs_int32 - back into a ktime_date
77 Int32To_ktimeRelDate(int32Date, kdptr)
79 struct ktime_date *kdptr;
81 memset(kdptr, 0, sizeof(*kdptr));
83 kdptr->day = int32Date % (MAX_DAY_VALUE + 1);
84 if ( kdptr->day != 0 )
85 kdptr->mask |= KTIMEDATE_DAY;
87 int32Date = int32Date/(MAX_DAY_VALUE + 1);
89 kdptr->month = int32Date % (MAX_MONTH_VALUE + 1);
90 if ( kdptr->month != 0 )
91 kdptr->mask |= KTIMEDATE_MONTH;
93 int32Date = int32Date/(MAX_MONTH_VALUE + 1);
95 kdptr->year = int32Date;
96 if ( kdptr->year != 0 )
97 kdptr->mask |= KTIMEDATE_YEAR;
102 /* ktimeDate_FromInt32
103 * Converts a time in seconds, to a time (in ktime_date format).
104 * Result is a conventional ktime_date structure.
105 * placed in the supplied structure
107 * timeSecs - time in seconds
108 * ktimePtr - ptr to struct for the return value
111 ktimeDate_FromInt32(timeSecs, ktimePtr)
113 struct ktime_date *ktimePtr;
117 timePtr = localtime((time_t *)&timeSecs);
119 /* copy the relevant fields */
120 ktimePtr->sec = timePtr->tm_sec;
121 ktimePtr->min = timePtr->tm_min;
122 ktimePtr->hour = timePtr->tm_hour;
123 ktimePtr->day = timePtr->tm_mday;
124 ktimePtr->month = timePtr->tm_mon+1;
125 ktimePtr->year = timePtr->tm_year;
127 ktimePtr->mask = KTIMEDATE_YEAR | KTIMEDATE_MONTH | KTIMEDATE_DAY |
128 KTIMEDATE_HOUR | KTIMEDATE_MIN | KTIMEDATE_SEC;
133 #define RD_DIGIT_LIMIT 4 /* max. no. digits permitted */
136 * Parses a relative date of the form <n>y<n>m<n>d representing years
137 * months and days. <n> is limited to RD_DIGIT_LIMIT digits in length
138 * and is further restricted by the maximums specified at the head
141 * dateStr - ptr to string to parse. Leading white space ingnored.
143 * returns a ptr to a static ktime_date structure containing
144 * appropriately set fields. The mask field is unused.
145 * 0 - error in date specification
149 ParseRelDate(dateStr, relDatePtr)
151 struct ktime_date *relDatePtr;
153 struct parseseqS *psPtr;
154 afs_int32 value, digit_limit;
155 afs_int32 type_index;
157 memset(relDatePtr, 0, sizeof(*relDatePtr));
163 while ( isspace(*dateStr) ) /* skip leading whitespace */
166 if ( isdigit(*dateStr) == 0 )
169 digit_limit = RD_DIGIT_LIMIT;
171 while ( isdigit(*dateStr) )
173 value = value*10 + *dateStr - '0';
175 if ( digit_limit-- == 0 )
179 psPtr = &parseseq[type_index];
180 /* determine the units. Search for a matching type character */
181 while ( (psPtr->ps_keychar != *dateStr)
182 && (psPtr->ps_keychar != 0)
186 psPtr = &parseseq[type_index];
189 /* no matching type found */
190 if ( psPtr->ps_keychar == 0 )
193 /* check the bounds on the maximum value. Can't be negative
194 * and if a maximum value is specified, check against it
197 || ((psPtr->ps_maxValue > 0) && (value > psPtr->ps_maxValue))
201 /* save computed value in the relevant type field */
202 switch ( psPtr->ps_field )
205 relDatePtr->year = value;
206 relDatePtr->mask |= KTIMEDATE_YEAR;
209 case KTIMEDATE_MONTH:
210 if ( value > MAX_MONTH_VALUE )
212 relDatePtr->month = value;
213 relDatePtr->mask |= KTIMEDATE_MONTH;
217 if ( value > MAX_DAY_VALUE )
220 relDatePtr->mask |= KTIMEDATE_DAY;
221 relDatePtr->day = value;
227 dateStr++; /* next digit */
231 /* no more chars to process, return the result */
241 * returns a static string representing the relative date. This is in
242 * a format acceptable to the relative date parser.
244 * datePtr - relative date to be converted.
246 * ptr to static string
250 RelDatetoString(datePtr)
251 struct ktime_date *datePtr;
253 static char dateString[64];
254 char tempstring[64], *sptr;
257 sptr = &dateString[0];
259 if ( datePtr->mask & KTIMEDATE_YEAR )
261 sprintf(tempstring, "%-dy", datePtr->year);
262 strcat(sptr, tempstring);
265 if ( datePtr->mask & KTIMEDATE_MONTH )
268 sprintf(tempstring, "%-dm", datePtr->month);
269 strcat(sptr, tempstring);
272 if ( datePtr->mask & KTIMEDATE_DAY )
275 sprintf(tempstring, "%-dd", datePtr->day);
276 strcat(sptr, tempstring);
281 /* Add_RelDate_to_Time
282 * Returns current time with a relative time added. Note that the
283 * computation adds in most significant fields first, i.e. year, month
284 * day etc. Addition of least significant fields would produce different
285 * results (depending on the data).
287 * relDatePtr - a ktime_date containing a relative time specification
289 * returns specified time with relative time added.
293 Add_RelDate_to_Time(relDatePtr, atime)
294 struct ktime_date *relDatePtr;
298 static struct ktime_date absDate;
300 ktimeDate_FromInt32(atime, &absDate); /* convert to ktime */
303 if ( relDatePtr->mask & KTIMEDATE_YEAR )
304 absDate.year += relDatePtr->year;
307 if ( relDatePtr->mask & KTIMEDATE_MONTH )
308 absDate.month += relDatePtr->month;
310 if ( absDate.month > 12 )
312 moreYears = absDate.month / 12;
313 absDate.month = absDate.month % 12;
314 absDate.year += moreYears;
317 /* day computations depend upon month size, so do these in seconds */
318 atime = ktime_InterpretDate(&absDate);
320 if ( relDatePtr->mask & KTIMEDATE_DAY )
321 atime = atime + relDatePtr->day*24*60*60;
323 if ( relDatePtr->mask & KTIMEDATE_HOUR )
324 atime = atime + relDatePtr->hour*60*60;
326 if ( relDatePtr->mask & KTIMEDATE_MIN )
327 atime = atime + relDatePtr->min*60;
329 if ( relDatePtr->mask & KTIMEDATE_SEC )
330 atime = atime + relDatePtr->sec;