calendar

BSD calendar reimplementation
git clone https://git.ce9e.org/calendar.git

commit
348f9ed31c63978e1548a135d0d49cb2c74e8396
parent
9efd6bb17d55e427b29241118d761c0408aae629
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2024-08-17 14:38
refactor: simpler star

Diffstat

M calendar.c 28 +++++++++++++++-------------

1 files changed, 15 insertions, 13 deletions


diff --git a/calendar.c b/calendar.c

@@ -277,14 +277,15 @@ struct tpl parse_date(char *s) {
  277   277 		return tpl;
  278   278 	}
  279   279 
  280    -1 	bool star = strcmp(s + strlen(s) - strlen("*"), "*") == 0;
   -1   280 	bool star = strncmp(s + strlen(s) - 1, "*", 1) == 0;
  281   281 
  282   282 	/* weekday */
  283   283 	if (strptime(s, "%Y/%m/%a", &match)) {
   -1   284 		tpl.year = match.tm_year + 1900;
  284   285 		tpl.month = match.tm_mon + 1;
  285   286 		tpl.weekday = match.tm_wday + 1;
  286    -1 		if (!star) {
  287    -1 			tpl.year = match.tm_year + 1900;
   -1   287 		if (star) {
   -1   288 			tpl.year = 0;
  288   289 		}
  289   290 		tpl.nth_of_month = n;
  290   291 		return tpl;
@@ -301,11 +302,9 @@ struct tpl parse_date(char *s) {
  301   302 
  302   303 	/* date */
  303   304 	if (strptime(s, "%Y/%m/%d", &match)) {
   -1   305 		tpl.year = match.tm_year + 1900;
  304   306 		tpl.month = match.tm_mon + 1;
  305   307 		tpl.day = match.tm_mday;
  306    -1 		if (!star) {
  307    -1 			tpl.year = match.tm_year + 1900;
  308    -1 		}
  309   308 		if (strchr(s, ':')) {
  310   309 			strtok(s, ":");
  311   310 			char *s_n = strtok(NULL, "");
@@ -320,23 +319,26 @@ struct tpl parse_date(char *s) {
  320   319 				invalid_date(s);
  321   320 				exit(1);
  322   321 			}
   -1   322 		} else if (star) {
   -1   323 			tpl.year = 0;
  323   324 		}
  324   325 		tpl.repeat = n;
  325   326 		return tpl;
  326   327 	} else if (strptime(s, "%m/%d", &match)) {
   -1   328 		tpl.year = TODAY->tm_year + 1900;
  327   329 		tpl.month = match.tm_mon + 1;
  328   330 		tpl.day = match.tm_mday;
  329    -1 		if (!star) {
  330    -1 			tpl.year = TODAY->tm_year + 1900;
  331    -1 			if (match.tm_mon < TODAY->tm_mon) {
  332    -1 				tpl.year += 1;
  333    -1 			}
   -1   331 		if (star) {
   -1   332 			tpl.year = 0;
   -1   333 		} else if (match.tm_mon < TODAY->tm_mon) {
   -1   334 			tpl.year += 1;
  334   335 		}
  335   336 		return tpl;
  336   337 	} else if (strptime(s, "%d", &match)) {
   -1   338 		tpl.year = TODAY->tm_year + 1900;
  337   339 		tpl.day = match.tm_mday;
  338    -1 		if (!star) {
  339    -1 			tpl.year = TODAY->tm_year + 1900;
   -1   340 		if (star) {
   -1   341 			tpl.year = 0;
  340   342 		}
  341   343 		return tpl;
  342   344 	}