1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
|
/*** dseq.c -- like seq(1) but for dates
*
* Copyright (C) 2009 - 2011 Sebastian Freundt
*
* Author: Sebastian Freundt <freundt@ga-group.nl>
*
* This file is part of dateutils.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the author nor the names of any contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
***/
#if defined HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <time.h>
#include <string.h>
#include "dt-core.h"
#include "dt-io.h"
#include "tzraw.h"
typedef uint8_t __skipspec_t;
/* generic closure */
struct dseq_clo_s {
struct dt_dt_s fst;
struct dt_dt_s lst;
struct dt_dt_s *ite;
size_t nite;
struct dt_dt_s *altite;
__skipspec_t ss;
size_t naltite;
/* direction, >0 if increasing, <0 if decreasing, 0 if undefined */
int dir;
int flags;
#define CLO_FL_FREE_ITE (1)
};
const char *prog = "dseq";
/* skip system */
static int
skipp(__skipspec_t ss, struct dt_dt_s dt)
{
dt_dow_t dow;
/* common case first */
if (ss == 0) {
return 0;
}
dow = dt_get_wday(dt.d);
/* just check if the bit in the bitset `skip' is set */
return (ss & (1 << dow)) != 0;
}
#define SKIP_MON (1 << DT_MONDAY)
#define SKIP_TUE (1 << DT_TUESDAY)
#define SKIP_WED (1 << DT_WEDNESDAY)
#define SKIP_THU (1 << DT_THURSDAY)
#define SKIP_FRI (1 << DT_FRIDAY)
#define SKIP_SAT (1 << DT_SATURDAY)
#define SKIP_SUN (1 << DT_SUNDAY)
static inline int
__toupper(int c)
{
return c & ~0x20;
}
static dt_dow_t
__parse_wd(const char *str)
{
#define ILEA(a, b) (((a) << 8) | (b))
int s1 = __toupper(str[0]);
int s2 = __toupper(str[1]);
switch (ILEA(s1, s2)) {
case ILEA('M', 'O'):
case ILEA('M', 0):
/* monday */
return DT_MONDAY;
case ILEA('T', 'U'):
/* tuesday */
return DT_TUESDAY;
case ILEA('W', 'E'):
case ILEA('W', 0):
/* wednesday */
return DT_WEDNESDAY;
case ILEA('T', 'H'):
/* thursday */
return DT_THURSDAY;
case ILEA('F', 'R'):
case ILEA('F', 0):
/* friday */
return DT_FRIDAY;
case ILEA('S', 'A'):
case ILEA('A', 0):
/* saturday */
return DT_SATURDAY;
case ILEA('S', 'U'):
case ILEA('S', 0):
/* sunday */
return DT_SUNDAY;
default:
return DT_MIRACLEDAY;
}
}
static __skipspec_t
__skip_dow(__skipspec_t ss, unsigned int wd)
{
if (wd > GREG_DAYS_P_WEEK) {
wd -= GREG_DAYS_P_WEEK;
}
switch (wd) {
case DT_MONDAY:
/* monday */
ss |= SKIP_MON;
break;
case DT_TUESDAY:
/* tuesday */
ss |= SKIP_TUE;
break;
case DT_WEDNESDAY:
/* wednesday */
ss |= SKIP_WED;
break;
case DT_THURSDAY:
/* thursday */
ss |= SKIP_THU;
break;
case DT_FRIDAY:
/* friday */
ss |= SKIP_FRI;
break;
case DT_SATURDAY:
/* saturday */
ss |= SKIP_SAT;
break;
case DT_SUNDAY:
/* sunday */
ss |= SKIP_SUN;
break;
default:
case DT_MIRACLEDAY:
break;
}
return ss;
}
static __skipspec_t
__skip_str(__skipspec_t ss, const char *str)
{
dt_dow_t tmp;
if ((tmp = __parse_wd(str)) != DT_MIRACLEDAY) {
ss = __skip_dow(ss, tmp);
} else {
int s1 = __toupper(str[0]);
int s2 = __toupper(str[1]);
if (ILEA(s1, s2) == ILEA('S', 'S')) {
/* weekend */
ss |= SKIP_SAT;
ss |= SKIP_SUN;
}
}
return ss;
}
static __skipspec_t
__skip_1spec(__skipspec_t ss, char *spec)
{
char *tmp;
dt_dow_t from, till;
if ((tmp = strchr(spec, '-')) == NULL) {
return __skip_str(ss, spec);
}
/* otherwise it's a range */
*tmp = '\0';
from = __parse_wd(spec);
till = __parse_wd(tmp + 1);
for (int d = from, e = till >= from ? till : till + 7; d <= e; d++) {
ss = __skip_dow(ss, d);
}
return ss;
}
static __skipspec_t
set_skip(__skipspec_t ss, char *spec)
{
char *tmp, *tm2;
if ((tmp = strchr(spec, ',')) == NULL) {
return __skip_1spec(ss, spec);
}
/* const violation */
*tmp++ = '\0';
ss = __skip_1spec(ss, spec);
while ((tmp = strchr(tm2 = tmp, ','))) {
*tmp++ = '\0';
ss = __skip_1spec(ss, tm2);
}
return __skip_1spec(ss, tm2);
}
static struct dt_dt_s
date_add(struct dt_dt_s d, struct dt_dt_s dur[], size_t ndur)
{
for (size_t i = 0; i < ndur; i++) {
d = dt_dtadd(d, dur[i]);
}
return d;
}
static void
date_neg_dur(struct dt_dt_s dur[], size_t ndur)
{
for (size_t i = 0; i < ndur; i++) {
dur[i] = dt_neg_dtdur(dur[i]);
}
return;
}
static bool
__daisy_feasible_p(struct dt_dt_s dur[], size_t ndur)
{
if (ndur != 1) {
return false;
} else if (dur->typ == (dt_dttyp_t)DT_YMD) {
if (dur->d.ymd.y || dur->d.ymd.m) {
return false;
}
} else if (dur->typ == (dt_dttyp_t)DT_MD) {
if (dur->d.md.m) {
return false;
}
} else if (dur->typ == (dt_dttyp_t)DT_BIZDA && (dur->d.bizda.bd)) {
return false;
}
return true;
}
static bool
__dur_naught_p(struct dt_dt_s dur)
{
return dur.d.u == 0 && dur.t.sdur == 0;
}
static bool
__durstack_naught_p(struct dt_dt_s dur[], size_t ndur)
{
if (ndur == 0) {
return true;
} else if (ndur == 1) {
return __dur_naught_p(dur[0]);
}
for (size_t i = 0; i < ndur; i++) {
if (!__dur_naught_p(dur[i])) {
return false;
}
}
return true;
}
static bool
__in_range_p(struct dt_dt_s now, struct dseq_clo_s *clo)
{
if (!dt_sandwich_only_t_p(now)) {
if (clo->dir > 0) {
return dt_dt_in_range_p(now, clo->fst, clo->lst);
} else if (clo->dir < 0) {
return dt_dt_in_range_p(now, clo->lst, clo->fst);
}
}
/* otherwise perform a simple range check */
if (clo->dir > 0) {
if (now.t.u >= clo->fst.t.u && now.t.u <= clo->lst.t.u) {
return true;
} else if (clo->fst.t.u >= clo->lst.t.u) {
return now.t.u <= clo->lst.t.u || now.d.daisydur == 0;
}
} else if (clo->dir < 0) {
if (now.t.u <= clo->fst.t.u && now.t.u >= clo->lst.t.u) {
return true;
} else if (clo->fst.t.u <= clo->lst.t.u) {
return now.t.u >= clo->lst.t.u || now.d.daisydur == 0;
}
}
return false;
}
static struct dt_dt_s
__seq_altnext(struct dt_dt_s now, struct dseq_clo_s *clo)
{
do {
now = date_add(now, clo->altite, clo->naltite);
} while (skipp(clo->ss, now) && __in_range_p(now, clo));
return now;
}
static struct dt_dt_s
__seq_this(struct dt_dt_s now, struct dseq_clo_s *clo)
{
/* if NOW is on a skip date, find the next date according to ALTITE, then ITE */
if (!skipp(clo->ss, now) && __in_range_p(now, clo)) {
return now;
} else if (clo->naltite > 0) {
return __seq_altnext(now, clo);
} else if (clo->nite) {
/* advance until it goes out of range */
for (;
skipp(clo->ss, now) && __in_range_p(now, clo);
now = date_add(now, clo->ite, clo->nite));
} else {
/* good question */
;
}
return now;
}
static struct dt_dt_s
__seq_next(struct dt_dt_s now, struct dseq_clo_s *clo)
{
/* advance NOW, then fix it */
struct dt_dt_s tmp = date_add(now, clo->ite, clo->nite);
return __seq_this(tmp, clo);
}
static int
__get_dir(struct dt_dt_s d, struct dseq_clo_s *clo)
{
if (!dt_sandwich_only_t_p(d)) {
/* trial addition to to see where it goes */
struct dt_dt_s tmp = __seq_next(d, clo);
return dt_dtcmp(tmp, d);
}
if (clo->ite->t.sdur && !clo->ite->t.neg) {
return 1;
} else if (clo->ite->t.sdur && clo->ite->t.neg) {
return -1;
}
return 0;
}
static struct dt_dt_s
__fixup_fst(struct dseq_clo_s *clo)
{
struct dt_dt_s tmp;
struct dt_dt_s old;
/* assume clo->dir has been computed already */
old = tmp = clo->lst;
date_neg_dur(clo->ite, clo->nite);
while (__in_range_p(tmp, clo)) {
old = tmp;
tmp = __seq_next(tmp, clo);
}
/* final checks */
old = __seq_this(old, clo);
date_neg_dur(clo->ite, clo->nite);
/* fixup again with negated dur */
old = __seq_this(old, clo);
return old;
}
static struct dt_t_s
tseq_guess_ite(struct dt_t_s beg, struct dt_t_s end)
{
struct dt_t_s res;
if (beg.hms.h != end.hms.h &&
beg.hms.m == 0 && end.hms.m == 0&&
beg.hms.s == 0 && end.hms.s == 0) {
if (beg.u < end.u) {
res.sdur = SECS_PER_HOUR;
} else {
res.sdur = -SECS_PER_HOUR;
}
} else if (beg.hms.m != end.hms.m &&
beg.hms.s == 0 && end.hms.s == 0) {
if (beg.u < end.u) {
res.sdur = SECS_PER_MIN;
} else {
res.sdur = -SECS_PER_MIN;
}
} else {
if (beg.u < end.u) {
res.sdur = 1L;
} else {
res.sdur = -1L;
}
}
res.dur = 1;
return res;
}
#include "dseq.yucc"
int
main(int argc, char *argv[])
{
static struct dt_dt_s ite_p1;
yuck_t argi[1U];
struct dt_dt_s tmp;
char **ifmt;
size_t nifmt;
char *ofmt;
dt_dttyp_t tgttyp;
int rc = 0;
struct dseq_clo_s clo = {
.ite = &ite_p1,
.nite = 1,
.altite = NULL,
.naltite = 0,
.ss = 0,
.dir = 0,
.flags = 0,
};
if (yuck_parse(argi, argc, argv)) {
rc = 1;
goto out;
}
/* assign ofmt/ifmt */
ofmt = argi->format_arg;
if (argi->backslash_escapes_flag) {
dt_io_unescape(ofmt);
}
nifmt = argi->input_format_nargs;
ifmt = argi->input_format_args;
for (size_t i = 0; i < argi->skip_nargs; i++) {
clo.ss = set_skip(clo.ss, argi->skip_args[i]);
}
if (argi->alt_inc_arg) {
struct __strpdtdur_st_s st = __strpdtdur_st_initialiser();
do {
if (dt_io_strpdtdur(&st, argi->alt_inc_arg) < 0) {
if (!argi->quiet_flag) {
error("Error: \
cannot parse duration string `%s'", argi->alt_inc_arg);
}
rc = 1;
goto out;
}
} while (__strpdtdur_more_p(&st));
/* assign values */
clo.altite = st.durs;
clo.naltite = st.ndurs;
}
switch (argi->nargs) {
struct dt_dt_s fst, lst;
default:
yuck_auto_help(argi);
rc = 1;
goto out;
case 2:
lst = dt_io_strpdt(argi->args[1U], ifmt, nifmt, NULL);
if (dt_unk_p(lst)) {
if (!argi->quiet_flag) {
dt_io_warn_strpdt(argi->args[1U]);
}
rc = 1;
goto out;
} else if (UNLIKELY(lst.fix) && !argi->quiet_flag) {
rc = 2;
}
/* fallthrough */
case 1:
fst = dt_io_strpdt(argi->args[0U], ifmt, nifmt, NULL);
if (dt_unk_p(fst)) {
if (!argi->quiet_flag) {
dt_io_warn_strpdt(argi->args[0U]);
}
rc = 1;
goto out;
} else if (UNLIKELY(fst.fix) && !argi->quiet_flag) {
rc = 2;
}
/* check the input arguments and do the sane thing now
* if it's all dates, use daisy iterator
* if it's all times, use sdur iterator
* if one of them is a dt, promote the other */
if (dt_sandwich_only_d_p(fst)) {
/* emulates old dseq(1) */
if (argi->nargs == 1U) {
lst.d = dt_date(DT_YMD);
dt_make_d_only(&lst, DT_YMD);
}
dt_make_d_only(clo.ite, DT_DAISY);
clo.ite->d.daisy = 1;
} else if (dt_sandwich_only_t_p(fst)) {
/* emulates old tseq(1) */
if (argi->nargs == 1U) {
lst.t = dt_time();
dt_make_t_only(&lst, DT_HMS);
}
/* let the guesser do the work */
clo.ite->t.sdur = 0;
} else if (dt_sandwich_p(fst)) {
if (argi->nargs == 1U) {
lst = dt_datetime((dt_dttyp_t)DT_YMD);
dt_make_sandwich(&lst, DT_YMD, DT_HMS);
}
dt_make_sandwich(clo.ite, DT_DAISY, DT_TUNK);
clo.ite->d.daisy = 1;
} else {
error("\
don't know how to handle single argument case");
rc = 1;
goto out;
}
clo.fst = fst;
clo.lst = lst;
clo.ite->dur = 1;
break;
case 3: {
struct __strpdtdur_st_s st = __strpdtdur_st_initialiser();
/* get lower bound */
fst = dt_io_strpdt(argi->args[0U], ifmt, nifmt, NULL);
if (dt_unk_p(fst)) {
if (!argi->quiet_flag) {
dt_io_warn_strpdt(argi->args[0U]);
}
rc = 1;
goto out;
} else if (UNLIKELY(fst.fix) && !argi->quiet_flag) {
rc = 2;
}
/* get increment */
do {
if (dt_io_strpdtdur(&st, argi->args[1U]) < 0) {
error("Error: \
cannot parse duration string `%s'", argi->args[1U]);
rc = 1;
goto out;
}
} while (__strpdtdur_more_p(&st));
/* assign values */
clo.ite = st.durs;
clo.nite = st.ndurs;
clo.flags |= CLO_FL_FREE_ITE;
/* get upper bound */
lst = dt_io_strpdt(argi->args[2U], ifmt, nifmt, NULL);
if (dt_unk_p(lst)) {
if (!argi->quiet_flag) {
dt_io_warn_strpdt(argi->args[2U]);
}
rc = 1;
goto out;
} else if (UNLIKELY(lst.fix) && !argi->quiet_flag) {
rc = 2;
}
clo.fst = fst;
clo.lst = lst;
break;
}
}
/* promote the args maybe */
if ((dt_sandwich_only_d_p(clo.fst) && dt_sandwich_only_t_p(clo.lst)) ||
(dt_sandwich_only_t_p(clo.fst) && dt_sandwich_only_d_p(clo.lst))) {
error("\
cannot mix dates and times as arguments");
rc = 1;
goto out;
} else if (dt_sandwich_only_d_p(clo.fst) && dt_sandwich_p(clo.lst)) {
/* promote clo.fst */
clo.fst.t = clo.lst.t;
dt_make_sandwich(&clo.fst, clo.fst.d.typ, clo.lst.t.typ);
} else if (dt_sandwich_p(clo.fst) && dt_sandwich_only_d_p(clo.lst)) {
/* promote clo.lst */
clo.lst.t = clo.fst.t;
dt_make_sandwich(&clo.lst, clo.lst.d.typ, clo.fst.t.typ);
} else if (dt_sandwich_only_t_p(clo.fst) && dt_sandwich_p(clo.lst)) {
/* promote clo.fst */
clo.fst.d = clo.lst.d;
dt_make_sandwich(&clo.fst, clo.fst.d.typ, clo.lst.t.typ);
} else if (dt_sandwich_p(clo.fst) && dt_sandwich_only_t_p(clo.lst)) {
/* promote clo.lst */
clo.lst.d = clo.fst.d;
dt_make_sandwich(&clo.lst, clo.lst.d.typ, clo.fst.t.typ);
}
#define _DAISY ((dt_dttyp_t)DT_DAISY)
tgttyp = clo.fst.typ;
if ((dt_sandwich_p(clo.fst) || dt_sandwich_only_d_p(clo.fst)) &&
clo.fst.d.typ == DT_YMD && clo.fst.d.ymd.m == 0) {
/* iterate year-wise */
dt_make_d_only(clo.ite, DT_YMD);
clo.ite->d.ymd.y = 1;
clo.ite->d.ymd.m = 0;
clo.ite->d.ymd.d = 0;
} else if ((dt_sandwich_p(clo.fst) || dt_sandwich_only_d_p(clo.fst)) &&
clo.fst.d.typ == DT_YMD && clo.fst.d.ymd.d == 0) {
/* iterate month-wise */
dt_make_d_only(clo.ite, DT_YMD);
clo.ite->d.ymd.y = 0;
clo.ite->d.ymd.m = 1;
clo.ite->d.ymd.d = 0;
} else if (dt_sandwich_only_d_p(clo.fst) &&
__daisy_feasible_p(clo.ite, clo.nite) &&
clo.fst.d.typ == DT_YMD &&
/* convert to daisies */
((clo.fst = dt_dtconv(_DAISY, clo.fst)).d.typ != DT_DAISY ||
(clo.lst = dt_dtconv(_DAISY, clo.lst)).d.typ != DT_DAISY)) {
if (!argi->quiet_flag) {
error("\
cannot convert calendric system internally");
}
rc = 1;
goto out;
} else if (dt_sandwich_only_t_p(clo.fst) && clo.ite->t.sdur == 0) {
clo.ite->t = tseq_guess_ite(clo.fst.t, clo.lst.t);
}
if (__durstack_naught_p(clo.ite, clo.nite) ||
(clo.dir = __get_dir(clo.fst, &clo)) == 0) {
if (!argi->quiet_flag) {
error("\
increment must not be naught");
}
rc = 1;
goto out;
} else if (argi->compute_from_last_flag) {
tmp = __fixup_fst(&clo);
} else {
tmp = __seq_this(clo.fst, &clo);
}
for (; __in_range_p(tmp, &clo); tmp = __seq_next(tmp, &clo)) {
struct dt_dt_s tgt = tmp;
if (UNLIKELY(ofmt == NULL)) {
tgt = dt_dtconv(tgttyp, tmp);
}
dt_io_write(tgt, ofmt, NULL, '\n');
}
out:
/* free strpdur resources */
if (clo.ite && clo.flags & CLO_FL_FREE_ITE) {
free(clo.ite);
}
if (clo.altite != NULL) {
free(clo.altite);
}
yuck_free(argi);
return rc;
}
/* dseq.c ends here */
|