version 1.19, 2013/11/25 18:02:50 |
version 1.20, 2014/01/13 23:18:57 |
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
* |
* |
* at [NOW] PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS |
* at [NOW] PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS|MONTHS|YEARS |
* /NUMBER [DOT NUMBER] [AM|PM]\ /[MONTH NUMBER [NUMBER]] \ |
* /NUMBER [DOT NUMBER] [AM|PM]\ /[MONTH NUMBER [NUMBER]] \ |
* |NOON | |[TOMORROW] | |
* |NOON | |[TOMORROW] | |
* |MIDNIGHT | |[DAY OF WEEK] | |
* |MIDNIGHT | |[DAY OF WEEK] | |
* \TEATIME / |NUMBER [SLASH NUMBER [SLASH NUMBER]]| |
* \TEATIME / |NUMBER [SLASH NUMBER [SLASH NUMBER]]| |
* \PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS/ |
* \PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS|MONTHS|YEARS/ |
*/ |
*/ |
|
|
#include <sys/types.h> |
#include <sys/types.h> |
|
|
enum { /* symbols */ |
enum { /* symbols */ |
MIDNIGHT, NOON, TEATIME, |
MIDNIGHT, NOON, TEATIME, |
PM, AM, TOMORROW, TODAY, NOW, |
PM, AM, TOMORROW, TODAY, NOW, |
MINUTES, HOURS, DAYS, WEEKS, |
MINUTES, HOURS, DAYS, WEEKS, MONTHS, YEARS, |
NUMBER, PLUS, DOT, SLASH, ID, JUNK, |
NUMBER, NEXT, PLUS, DOT, SLASH, ID, JUNK, |
JAN, FEB, MAR, APR, MAY, JUN, |
JAN, FEB, MAR, APR, MAY, JUN, |
JUL, AUG, SEP, OCT, NOV, DEC, |
JUL, AUG, SEP, OCT, NOV, DEC, |
SUN, MON, TUE, WED, THU, FRI, SAT |
SUN, MON, TUE, WED, THU, FRI, SAT |
|
|
{ "tomorrow", TOMORROW, 0 }, /* execute 24 hours from time */ |
{ "tomorrow", TOMORROW, 0 }, /* execute 24 hours from time */ |
{ "today", TODAY, 0 }, /* execute today - don't advance time */ |
{ "today", TODAY, 0 }, /* execute today - don't advance time */ |
{ "now", NOW, 0 }, /* opt prefix for PLUS */ |
{ "now", NOW, 0 }, /* opt prefix for PLUS */ |
|
{ "next", NEXT, 0 }, /* opt prefix for + 1 */ |
|
|
{ "minute", MINUTES, 0 }, /* minutes multiplier */ |
{ "minute", MINUTES, 0 }, /* minutes multiplier */ |
{ "min", MINUTES, 0 }, |
{ "min", MINUTES, 0 }, |
|
|
{ "week", WEEKS, 0 }, /* week ... */ |
{ "week", WEEKS, 0 }, /* week ... */ |
{ "w", WEEKS, 0 }, |
{ "w", WEEKS, 0 }, |
{ "weeks", WEEKS, 1 }, /* (pluralized) */ |
{ "weeks", WEEKS, 1 }, /* (pluralized) */ |
|
{ "month", MONTHS, 0 }, /* month ... */ |
|
{ "mo", MONTHS, 0 }, |
|
{ "mth", MONTHS, 0 }, |
|
{ "months", MONTHS, 1 }, /* (pluralized) */ |
|
{ "year", YEARS, 0 }, /* year ... */ |
|
{ "y", YEARS, 0 }, |
|
{ "years", YEARS, 1 }, /* (pluralized) */ |
{ "jan", JAN, 0 }, |
{ "jan", JAN, 0 }, |
{ "feb", FEB, 0 }, |
{ "feb", FEB, 0 }, |
{ "mar", MAR, 0 }, |
{ "mar", MAR, 0 }, |
|
|
/* |
/* |
* plus() parses a now + time |
* plus() parses a now + time |
* |
* |
* at [NOW] PLUS NUMBER [MINUTES|HOURS|DAYS|WEEKS] |
* at [NOW] PLUS NUMBER [MINUTES|HOURS|DAYS|WEEKS|MONTHS|YEARS] |
* |
* |
*/ |
*/ |
static int |
static int |
plus(struct tm *tm) |
plus(struct tm *tm) |
{ |
{ |
int delay; |
int increment; |
int expectplur; |
int expectplur; |
|
|
if (expect(NUMBER) != 0) |
if (sc_tokid == NEXT) { |
return (-1); |
increment = 1; |
|
expectplur = 0; |
|
} else { |
|
if (expect(NUMBER) != 0) |
|
return (-1); |
|
increment = atoi(sc_token); |
|
expectplur = (increment != 1) ? 1 : 0; |
|
} |
|
|
delay = atoi(sc_token); |
|
expectplur = (delay != 1) ? 1 : 0; |
|
|
|
switch (token()) { |
switch (token()) { |
|
case YEARS: |
|
tm->tm_year += increment; |
|
return (0); |
|
case MONTHS: |
|
tm->tm_mon += increment; |
|
while (tm->tm_mon >= 12) { |
|
tm->tm_year++; |
|
tm->tm_mon -= 12; |
|
} |
|
return (0); |
case WEEKS: |
case WEEKS: |
delay *= 7; |
increment *= 7; |
/* FALLTHROUGH */ |
/* FALLTHROUGH */ |
case DAYS: |
case DAYS: |
delay *= 24; |
increment *= 24; |
/* FALLTHROUGH */ |
/* FALLTHROUGH */ |
case HOURS: |
case HOURS: |
delay *= 60; |
increment *= 60; |
/* FALLTHROUGH */ |
/* FALLTHROUGH */ |
case MINUTES: |
case MINUTES: |
if (expectplur != sc_tokplur) |
if (expectplur != sc_tokplur) |
fprintf(stderr, "%s: pluralization is wrong\n", |
fprintf(stderr, "%s: pluralization is wrong\n", |
ProgramName); |
ProgramName); |
dateadd(delay, tm); |
dateadd(increment, tm); |
return (0); |
return (0); |
} |
} |
|
|
|
|
* if we've gone past that time - but if we're specifying a time plus |
* if we've gone past that time - but if we're specifying a time plus |
* a relative offset, it's okay to bump things |
* a relative offset, it's okay to bump things |
*/ |
*/ |
if ((sc_tokid == EOF || sc_tokid == PLUS) && tm->tm_hour > hour) { |
if ((sc_tokid == EOF || sc_tokid == PLUS || sc_tokid == NEXT) && |
|
tm->tm_hour > hour) { |
tm->tm_mday++; |
tm->tm_mday++; |
tm->tm_wday++; |
tm->tm_wday++; |
} |
} |
|
|
* |[TOMORROW] | |
* |[TOMORROW] | |
* |[DAY OF WEEK] | |
* |[DAY OF WEEK] | |
* |NUMBER [SLASH NUMBER [SLASH NUMBER]]| |
* |NUMBER [SLASH NUMBER [SLASH NUMBER]]| |
* \PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS/ |
* \PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS|MONTHS|YEARS/ |
*/ |
*/ |
static int |
static int |
month(struct tm *tm) |
month(struct tm *tm) |
|
|
size_t tlen; |
size_t tlen; |
|
|
switch (sc_tokid) { |
switch (sc_tokid) { |
|
case NEXT: |
case PLUS: |
case PLUS: |
if (plus(tm) != 0) |
if (plus(tm) != 0) |
return (-1); |
return (-1); |
|
|
runtime = nowtime; |
runtime = nowtime; |
break; |
break; |
} |
} |
else if (sc_tokid != PLUS) |
else if (sc_tokid != PLUS && sc_tokid != NEXT) |
plonk(sc_tokid); |
plonk(sc_tokid); |
|
case NEXT: |
case PLUS: |
case PLUS: |
if (plus(&runtime) != 0) |
if (plus(&runtime) != 0) |
return (-1); |
return (-1); |