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>
17 #include <sys/types.h>
32 /* maximum values for relative dates */
34 #define MAX_YEAR_VALUE 0
35 #define MAX_MONTH_VALUE 11
36 #define MAX_DAY_VALUE 30
38 /* for parsing relative expiration dates */
39 static struct parseseqS {
42 afs_int32 ps_maxValue;
45 KTIMEDATE_YEAR, 'y', MAX_YEAR_VALUE,}, /* no max. value */
47 KTIMEDATE_MONTH, 'm', MAX_MONTH_VALUE,}, /* months max. 12 */
49 KTIMEDATE_DAY, 'd', MAX_DAY_VALUE,}, /* days max. 31 */
54 /* Encodings to and from relative dates. The caller is responsible for
55 * enforcing appropriate use of these routines
59 /* ktimeRelDate_ToInt32
60 * converts a relative ktime date into an afs_int32.
62 * afs_int32 value of encoded date.
66 ktimeRelDate_ToInt32(struct ktime_date *kdptr)
71 (((kdptr->year * (MAX_MONTH_VALUE + 1)) +
72 kdptr->month) * (MAX_DAY_VALUE + 1)) + kdptr->day;
76 /* Int32To_ktimeRelDate
77 * Convert a relative date encoded in an afs_int32 - back into a ktime_date
82 Int32To_ktimeRelDate(afs_int32 int32Date, struct ktime_date *kdptr)
84 memset(kdptr, 0, sizeof(*kdptr));
86 kdptr->day = int32Date % (MAX_DAY_VALUE + 1);
88 kdptr->mask |= KTIMEDATE_DAY;
90 int32Date = int32Date / (MAX_DAY_VALUE + 1);
92 kdptr->month = int32Date % (MAX_MONTH_VALUE + 1);
93 if (kdptr->month != 0)
94 kdptr->mask |= KTIMEDATE_MONTH;
96 int32Date = int32Date / (MAX_MONTH_VALUE + 1);
98 kdptr->year = int32Date;
100 kdptr->mask |= KTIMEDATE_YEAR;
105 /* ktimeDate_FromInt32
106 * Converts a time in seconds, to a time (in ktime_date format).
107 * Result is a conventional ktime_date structure.
108 * placed in the supplied structure
110 * timeSecs - time in seconds
111 * ktimePtr - ptr to struct for the return value
115 ktimeDate_FromInt32(afs_int32 timeSecs, struct ktime_date *ktimePtr)
119 timePtr = localtime((time_t *) & timeSecs);
121 /* copy the relevant fields */
122 ktimePtr->sec = timePtr->tm_sec;
123 ktimePtr->min = timePtr->tm_min;
124 ktimePtr->hour = timePtr->tm_hour;
125 ktimePtr->day = timePtr->tm_mday;
126 ktimePtr->month = timePtr->tm_mon + 1;
127 ktimePtr->year = timePtr->tm_year;
130 KTIMEDATE_YEAR | KTIMEDATE_MONTH | KTIMEDATE_DAY | KTIMEDATE_HOUR |
131 KTIMEDATE_MIN | KTIMEDATE_SEC;
136 #define RD_DIGIT_LIMIT 4 /* max. no. digits permitted */
139 * Parses a relative date of the form <n>y<n>m<n>d representing years
140 * months and days. <n> is limited to RD_DIGIT_LIMIT digits in length
141 * and is further restricted by the maximums specified at the head
144 * dateStr - ptr to string to parse. Leading white space ingnored.
146 * returns a ptr to a static ktime_date structure containing
147 * appropriately set fields. The mask field is unused.
148 * 0 - error in date specification
152 ParseRelDate(char *dateStr, struct ktime_date * relDatePtr)
154 struct parseseqS *psPtr;
155 afs_int32 value, digit_limit;
156 afs_int32 type_index;
158 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)) {
172 value = value * 10 + *dateStr - '0';
174 if (digit_limit-- == 0)
178 psPtr = &parseseq[type_index];
179 /* determine the units. Search for a matching type character */
180 while ((psPtr->ps_keychar != *dateStr)
181 && (psPtr->ps_keychar != 0)
184 psPtr = &parseseq[type_index];
187 /* no matching type found */
188 if (psPtr->ps_keychar == 0)
191 /* check the bounds on the maximum value. Can't be negative
192 * and if a maximum value is specified, check against it
195 || ((psPtr->ps_maxValue > 0) && (value > psPtr->ps_maxValue))
199 /* save computed value in the relevant type field */
200 switch (psPtr->ps_field) {
202 relDatePtr->year = value;
203 relDatePtr->mask |= KTIMEDATE_YEAR;
206 case KTIMEDATE_MONTH:
207 if (value > MAX_MONTH_VALUE)
209 relDatePtr->month = value;
210 relDatePtr->mask |= KTIMEDATE_MONTH;
214 if (value > MAX_DAY_VALUE)
217 relDatePtr->mask |= KTIMEDATE_DAY;
218 relDatePtr->day = value;
224 dateStr++; /* next digit */
227 /* no more chars to process, return the result */
237 * returns a static string representing the relative date. This is in
238 * a format acceptable to the relative date parser.
240 * datePtr - relative date to be converted.
242 * ptr to static string
246 RelDatetoString(struct ktime_date *datePtr)
248 static char dateString[64];
249 char tempstring[64], *sptr;
252 sptr = &dateString[0];
254 if (datePtr->mask & KTIMEDATE_YEAR) {
255 sprintf(tempstring, "%-dy", datePtr->year);
256 strcat(sptr, tempstring);
259 if (datePtr->mask & KTIMEDATE_MONTH) {
261 sprintf(tempstring, "%-dm", datePtr->month);
262 strcat(sptr, tempstring);
265 if (datePtr->mask & KTIMEDATE_DAY) {
267 sprintf(tempstring, "%-dd", datePtr->day);
268 strcat(sptr, tempstring);
273 /* Add_RelDate_to_Time
274 * Returns current time with a relative time added. Note that the
275 * computation adds in most significant fields first, i.e. year, month
276 * day etc. Addition of least significant fields would produce different
277 * results (depending on the data).
279 * relDatePtr - a ktime_date containing a relative time specification
281 * returns specified time with relative time added.
285 Add_RelDate_to_Time(struct ktime_date * relDatePtr, afs_int32 atime)
288 static struct ktime_date absDate;
290 ktimeDate_FromInt32(atime, &absDate); /* convert to ktime */
293 if (relDatePtr->mask & KTIMEDATE_YEAR)
294 absDate.year += relDatePtr->year;
297 if (relDatePtr->mask & KTIMEDATE_MONTH)
298 absDate.month += relDatePtr->month;
300 if (absDate.month > 12) {
301 moreYears = absDate.month / 12;
302 absDate.month = absDate.month % 12;
303 absDate.year += moreYears;
306 /* day computations depend upon month size, so do these in seconds */
307 atime = ktime_InterpretDate(&absDate);
309 if (relDatePtr->mask & KTIMEDATE_DAY)
310 atime = atime + relDatePtr->day * 24 * 60 * 60;
312 if (relDatePtr->mask & KTIMEDATE_HOUR)
313 atime = atime + relDatePtr->hour * 60 * 60;
315 if (relDatePtr->mask & KTIMEDATE_MIN)
316 atime = atime + relDatePtr->min * 60;
318 if (relDatePtr->mask & KTIMEDATE_SEC)
319 atime = atime + relDatePtr->sec;