]>
git.cameronkatri.com Git - pw-darwin.git/blob - pw/psdate.c
3 * David L. Nugent. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 static const char rcsid
[] =
41 a2i(char const ** str
)
56 numerics(char const * str
)
58 int rc
= isdigit(*str
);
61 while (isdigit(*str
) || *str
== 'x')
67 aindex(char const * arr
[], char const ** str
, int len
)
73 l
= strlen(strncpy(mystr
, *str
, len
));
74 for (i
= 0; i
< l
; i
++)
75 mystr
[i
] = (char) tolower(mystr
[i
]);
76 for (i
= 0; arr
[i
] && strcmp(mystr
, arr
[i
]) != 0; i
++);
79 else { /* Skip past it */
80 while (**str
&& isalpha(**str
))
82 /* And any following whitespace */
83 while (**str
&& (**str
== ',' || isspace(**str
)))
90 weekday(char const ** str
)
92 static char const *days
[] =
93 {"sun", "mon", "tue", "wed", "thu", "fri", "sat", NULL
};
95 return aindex(days
, str
, 3);
99 month(char const ** str
)
101 static char const *months
[] =
102 {"jan", "feb", "mar", "apr", "may", "jun", "jul",
103 "aug", "sep", "oct", "nov", "dec", NULL
};
105 return aindex(months
, str
, 3);
109 parse_time(char const * str
, int *hour
, int *min
, int *sec
)
112 if ((str
= strchr(str
, ':')) == NULL
)
117 *sec
= ((str
= strchr(str
, ':')) == NULL
) ? 0 : atoi(++str
);
123 parse_datesub(char const * str
, int *day
, int *mon
, int *year
)
127 static char const nchrs
[] = "0123456789 \t,/-.";
129 if ((i
= month(&str
)) != -1) {
131 if ((i
= a2i(&str
)) != 0)
133 } else if ((i
= a2i(&str
)) != 0) {
135 while (*str
&& strchr(nchrs
+ 10, *str
) != NULL
)
137 if ((i
= month(&str
)) != -1)
139 else if ((i
= a2i(&str
)) != 0)
144 while (*str
&& strchr(nchrs
+ 10, *str
) != NULL
)
157 * Parse time must be flexible, it handles the following formats:
158 * nnnnnnnnnnn UNIX timestamp (all numeric), 0 = now
159 * 0xnnnnnnnn UNIX timestamp in hexadecimal
160 * 0nnnnnnnnn UNIX timestamp in octal
162 * +nnnn[smhdwoy] Given time + nnnn hours, mins, days, weeks, months or years
163 * -nnnn[smhdwoy] Given time - nnnn hours, mins, days, weeks, months or years
164 * dd[ ./-]mmm[ ./-]yy Date }
165 * hh:mm:ss Time } May be combined
169 parse_date(time_t dt
, char const * str
)
179 while (*str
&& isspace(*str
))
183 val
= strtol(str
, &p
, 0);
185 } else if (*str
== '+' || *str
== '-') {
186 val
= strtol(str
, &p
, 0);
189 case 'H': /* hours */
194 case 'M': /* minutes */
198 case 'S': /* seconds */
203 dt
+= (val
* 86400L);
206 case 'W': /* weeks */
207 dt
+= (val
* 604800L);
210 case 'O': /* months */
212 T
->tm_mon
+= (int) val
;
216 case 'Y': /* years */
218 T
->tm_year
+= (int) val
;
223 if (T
->tm_mday
!= i
) {
226 dt
-= (time_t) 86400L;
228 default: /* unknown */
229 break; /* leave untouched */
235 * Skip past any weekday prefix
238 str
= strncpy(tmp
, str
, sizeof tmp
- 1);
239 tmp
[sizeof tmp
- 1] = '\0';
243 * See if we can break off any timezone
245 while ((q
= strrchr(tmp
, ' ')) != NULL
) {
246 if (strchr("(+-", q
[1]) != NULL
)
251 while (q
[j
] && isupper(q
[j
]))
261 * See if there is a time hh:mm[:ss]
263 if ((p
= strchr(tmp
, ':')) == NULL
) {
266 * No time string involved
268 T
->tm_hour
= T
->tm_min
= T
->tm_sec
= 0;
269 parse_datesub(tmp
, &T
->tm_mday
, &T
->tm_mon
, &T
->tm_year
);
271 char datestr
[64], timestr
[64];
274 * Let's chip off the time string
276 if ((q
= strpbrk(p
, " \t")) != NULL
) { /* Time first? */
279 strncpy(timestr
, str
, l
);
281 strncpy(datestr
, q
+ 1, sizeof datestr
);
282 datestr
[sizeof datestr
- 1] = '\0';
283 parse_time(timestr
, &T
->tm_hour
, &T
->tm_min
, &T
->tm_sec
);
284 parse_datesub(datestr
, &T
->tm_mday
, &T
->tm_mon
, &T
->tm_year
);
285 } else if ((q
= strrchr(tmp
, ' ')) != NULL
) { /* Time last */
288 strncpy(timestr
, q
+ 1, sizeof timestr
);
289 timestr
[sizeof timestr
- 1] = '\0';
290 strncpy(datestr
, tmp
, l
);
292 } else /* Bail out */
294 parse_time(timestr
, &T
->tm_hour
, &T
->tm_min
, &T
->tm_sec
);
295 parse_datesub(datestr
, &T
->tm_mday
, &T
->tm_mon
, &T
->tm_year
);