vary.c revision 44598
1218799Snwhitehorn/*- 2218799Snwhitehorn * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org> 3218799Snwhitehorn * All rights reserved. 4218799Snwhitehorn * 5218799Snwhitehorn * Redistribution and use in source and binary forms, with or without 6218799Snwhitehorn * modification, are permitted provided that the following conditions 7218799Snwhitehorn * are met: 8218799Snwhitehorn * 1. Redistributions of source code must retain the above copyright 9218799Snwhitehorn * notice, this list of conditions and the following disclaimer. 10218799Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright 11218799Snwhitehorn * notice, this list of conditions and the following disclaimer in the 12218799Snwhitehorn * documentation and/or other materials provided with the distribution. 13218799Snwhitehorn * 14218799Snwhitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15218799Snwhitehorn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16218799Snwhitehorn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17218799Snwhitehorn * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18218799Snwhitehorn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19218799Snwhitehorn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20218799Snwhitehorn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21218799Snwhitehorn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22218799Snwhitehorn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23218799Snwhitehorn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24218799Snwhitehorn * SUCH DAMAGE. 25218799Snwhitehorn */ 26218799Snwhitehorn 27218799Snwhitehorn#ifndef lint 28218799Snwhitehornstatic const char rcsid[] = 29218799Snwhitehorn "$Id: vary.c,v 1.4 1998/05/06 06:51:20 charnier Exp $"; 30218799Snwhitehorn#endif /* not lint */ 31218799Snwhitehorn 32218799Snwhitehorn#include <time.h> 33218799Snwhitehorn#include <string.h> 34218799Snwhitehorn#include <stdlib.h> 35218799Snwhitehorn#include "vary.h" 36218799Snwhitehorn 37218799Snwhitehornstruct trans { 38218799Snwhitehorn int val; 39218799Snwhitehorn char *str; 40218799Snwhitehorn}; 41218799Snwhitehorn 42218799Snwhitehornstatic struct trans trans_mon[] = { 43218799Snwhitehorn { 1, "january" }, { 2, "february" }, { 3, "march" }, { 4, "april" }, 44218799Snwhitehorn { 6, "june" }, { 7, "july" }, { 8, "august" }, { 9, "september" }, 45218799Snwhitehorn { 10, "october" }, { 11, "november" }, { 12, "december" }, 46218799Snwhitehorn { -1, NULL } 47218799Snwhitehorn}; 48218799Snwhitehorn 49218799Snwhitehornstatic struct trans trans_wday[] = { 50218799Snwhitehorn { 0, "sunday" }, { 1, "monday" }, { 2, "tuesday" }, { 3, "wednesday" }, 51218799Snwhitehorn { 4, "thursday" }, { 5, "friday" }, { 6, "saturday" }, 52218799Snwhitehorn { -1, NULL } 53218799Snwhitehorn}; 54218799Snwhitehorn 55218799Snwhitehornstatic char digits[] = "0123456789"; 56218799Snwhitehorn 57218799Snwhitehornstatic int 58218799Snwhitehorntrans(const struct trans t[], const char *arg) 59218799Snwhitehorn{ 60218799Snwhitehorn int f; 61218799Snwhitehorn 62218799Snwhitehorn for (f = 0; t[f].val != -1; f++) 63218799Snwhitehorn if (!strncasecmp(t[f].str, arg, 3) || 64 !strncasecmp(t[f].str, arg, strlen(t[f].str))) 65 return t[f].val; 66 67 return -1; 68} 69 70struct vary * 71vary_append(struct vary *v, char *arg) 72{ 73 struct vary *result, **nextp; 74 75 if (v) { 76 result = v; 77 while (v->next) 78 v = v->next; 79 nextp = &v->next; 80 } else 81 nextp = &result; 82 83 *nextp = (struct vary *)malloc(sizeof(struct vary)); 84 (*nextp)->arg = arg; 85 (*nextp)->next = NULL; 86 return result; 87} 88 89static int mdays[12] = { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 90 91static int 92daysinmonth(const struct tm *t) 93{ 94 int year; 95 96 year = t->tm_year + 1900; 97 98 if (t->tm_mon == 1) 99 if (!(year % 400)) 100 return 29; 101 else if (!(year % 100)) 102 return 28; 103 else if (!(year % 4)) 104 return 29; 105 else 106 return 28; 107 else if (t->tm_mon >= 0 && t->tm_mon < 12) 108 return mdays[t->tm_mon]; 109 110 return 0; 111} 112 113 114static int 115adjyear(struct tm *t, char type, int val) 116{ 117 switch (type) { 118 case '+': 119 t->tm_year += val; 120 break; 121 case '-': 122 t->tm_year -= val; 123 break; 124 default: 125 t->tm_year = val; 126 if (t->tm_year < 69) 127 t->tm_year += 100; /* as per date.c */ 128 else if (t->tm_year > 1900) 129 t->tm_year -= 1900; /* struct tm holds years since 1900 */ 130 break; 131 } 132 return mktime(t) != -1; 133} 134 135static int 136adjmon(struct tm *t, char type, int val, int istext) 137{ 138 if (val < 0) 139 return 0; 140 141 switch (type) { 142 case '+': 143 if (istext) 144 if (val <= t->tm_mon) 145 val += 11 - t->tm_mon; /* early next year */ 146 else 147 val -= t->tm_mon + 1; /* later this year */ 148 if (!adjyear(t, '+', (t->tm_mon + val) / 12)) 149 return 0; 150 val %= 12; 151 t->tm_mon += val; 152 if (t->tm_mon > 11) 153 t->tm_mon -= 12; 154 break; 155 156 case '-': 157 if (istext) 158 if (val-1 > t->tm_mon) 159 val = 13 - val + t->tm_mon; /* later last year */ 160 else 161 val = t->tm_mon - val + 1; /* early this year */ 162 if (!adjyear(t, '-', val / 12)) 163 return 0; 164 val %= 12; 165 if (val > t->tm_mon) { 166 if (!adjyear(t, '-', 1)) 167 return 0; 168 val -= 12; 169 } 170 t->tm_mon -= val; 171 break; 172 173 default: 174 if (val > 12 || val < 1) 175 return 0; 176 t->tm_mon = --val; 177 } 178 179 return mktime(t) != -1; 180} 181 182static int 183adjday(struct tm *t, char type, int val) 184{ 185 int mdays; 186 switch (type) { 187 case '+': 188 while (val) { 189 mdays = daysinmonth(t); 190 if (val > mdays - t->tm_mday) { 191 val -= mdays - t->tm_mday + 1; 192 t->tm_mday = 1; 193 if (!adjmon(t, '+', 1, 0)) 194 return 0; 195 } else { 196 t->tm_mday += val; 197 val = 0; 198 } 199 } 200 break; 201 case '-': 202 while (val) 203 if (val >= t->tm_mday) { 204 val -= t->tm_mday; 205 t->tm_mday = 1; 206 if (!adjmon(t, '-', 1, 0)) 207 return 0; 208 t->tm_mday = daysinmonth(t); 209 } else { 210 t->tm_mday -= val; 211 val = 0; 212 } 213 break; 214 default: 215 if (val > 0 && val <= daysinmonth(t)) 216 t->tm_mday = val; 217 else 218 return 0; 219 break; 220 } 221 222 return mktime(t) != -1; 223} 224 225static int 226adjwday(struct tm *t, char type, int val, int istext) 227{ 228 if (val < 0) 229 return 0; 230 231 switch (type) { 232 case '+': 233 if (istext) 234 if (val < t->tm_wday) 235 val = 7 - t->tm_wday + val; /* early next week */ 236 else 237 val -= t->tm_wday; /* later this week */ 238 else 239 val *= 7; /* "-W +5" == "5 weeks in the future" */ 240 return adjday(t, '+', val); 241 case '-': 242 if (istext) 243 if (val > t->tm_wday) 244 val = 7 - val + t->tm_wday; /* later last week */ 245 else 246 val = t->tm_wday - val; /* early this week */ 247 else 248 val *= 7; /* "-W -5" == "5 weeks ago" */ 249 return adjday(t, '-', val); 250 default: 251 if (val < t->tm_wday) 252 return adjday(t, '-', t->tm_wday - val); 253 else if (val > 6) 254 return 0; 255 else if (val > t->tm_wday) 256 return adjday(t, '+', val - t->tm_wday); 257 } 258 return 1; 259} 260 261static int 262adjhour(struct tm *t, char type, int val) 263{ 264 if (val < 0) 265 return 0; 266 267 switch (type) { 268 case '+': 269 if (!adjday(t, '+', (t->tm_hour + val) / 24)) 270 return 0; 271 val %= 24; 272 t->tm_hour += val; 273 if (t->tm_hour > 23) 274 t->tm_hour -= 24; 275 break; 276 277 case '-': 278 if (!adjday(t, '-', val / 24)) 279 return 0; 280 val %= 24; 281 if (val > t->tm_hour) { 282 if (!adjday(t, '-', 1)) 283 return 0; 284 val -= 24; 285 } 286 t->tm_hour -= val; 287 break; 288 289 default: 290 if (val > 23) 291 return 0; 292 t->tm_hour = val; 293 } 294 295 return mktime(t) != -1; 296} 297 298static int 299adjmin(struct tm *t, char type, int val) 300{ 301 if (val < 0) 302 return 0; 303 304 switch (type) { 305 case '+': 306 if (!adjhour(t, '+', (t->tm_min + val) / 60)) 307 return 0; 308 val %= 60; 309 t->tm_min += val; 310 if (t->tm_min > 59) 311 t->tm_min -= 60; 312 break; 313 314 case '-': 315 if (!adjhour(t, '-', val / 60)) 316 return 0; 317 val %= 60; 318 if (val > t->tm_min) { 319 if (!adjhour(t, '-', 1)) 320 return 0; 321 val -= 60; 322 } 323 t->tm_min -= val; 324 break; 325 326 default: 327 if (val > 59) 328 return 0; 329 t->tm_min = val; 330 } 331 332 return mktime(t) != -1; 333} 334 335static int 336adjsec(struct tm *t, char type, int val) 337{ 338 if (val < 0) 339 return 0; 340 341 switch (type) { 342 case '+': 343 if (!adjmin(t, '+', (t->tm_sec + val) / 60)) 344 return 0; 345 val %= 60; 346 t->tm_sec += val; 347 if (t->tm_sec > 59) 348 t->tm_sec -= 60; 349 break; 350 351 case '-': 352 if (!adjmin(t, '-', val / 60)) 353 return 0; 354 val %= 60; 355 if (val > t->tm_sec) { 356 if (!adjmin(t, '-', 1)) 357 return 0; 358 val -= 60; 359 } 360 t->tm_sec -= val; 361 break; 362 363 default: 364 if (val > 59) 365 return 0; 366 t->tm_sec = val; 367 } 368 369 return mktime(t) != -1; 370} 371 372const struct vary * 373vary_apply(const struct vary *v, struct tm *t) 374{ 375 char type; 376 char which; 377 char *arg; 378 int len; 379 int val; 380 381 for (; v; v = v->next) { 382 type = *v->arg; 383 arg = v->arg; 384 if (type == '+' || type == '-') 385 arg++; 386 else 387 type = '\0'; 388 len = strlen(arg); 389 if (len < 2) 390 return v; 391 392 if (strspn(arg, digits) != len-1) { 393 val = trans(trans_wday, arg); 394 if (val != -1) { 395 if (!adjwday(t, type, val, 1)) 396 return v; 397 } else { 398 val = trans(trans_mon, arg); 399 if (val != -1) { 400 if (!adjmon(t, type, val, 1)) 401 return v; 402 } else 403 return v; 404 } 405 } else { 406 val = atoi(arg); 407 which = arg[len-1]; 408 409 switch (which) { 410 case 'S': 411 if (!adjsec(t, type, val)) 412 return v; 413 break; 414 case 'M': 415 if (!adjmin(t, type, val)) 416 return v; 417 break; 418 case 'H': 419 if (!adjhour(t, type, val)) 420 return v; 421 break; 422 case 'd': 423 if (!adjday(t, type, val)) 424 return v; 425 break; 426 case 'w': 427 if (!adjwday(t, type, val, 0)) 428 return v; 429 break; 430 case 'm': 431 if (!adjmon(t, type, val, 0)) 432 return v; 433 break; 434 case 'y': 435 if (!adjyear(t, type, val)) 436 return v; 437 break; 438 default: 439 return v; 440 } 441 } 442 } 443 return 0; 444} 445 446void 447vary_destroy(struct vary *v) 448{ 449 struct vary *n; 450 451 while (v) { 452 n = v->next; 453 free(v); 454 v = n; 455 } 456} 457