calendar

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

commit
5d9f3c1f6ac55eda0c9e02c7fe49a7f9e5250ed1
parent
178c08c1c21eaf53a120eb056fb7952b7e5228f0
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2018-11-19 22:27
c: refactor

Diffstat

M calendar.c 72 +++++++++++++++++++++++++++++++++++--------------------------

1 files changed, 41 insertions, 31 deletions


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

@@ -64,6 +64,37 @@ struct line {
   64    64 	char *desc;
   65    65 };
   66    66 
   -1    67 struct tm mkdate(int year, int month, int day) {
   -1    68 	struct tm tm = {
   -1    69 		.tm_year=year - 1900,
   -1    70 		.tm_mon=month - 1,
   -1    71 		.tm_mday=day,
   -1    72 	};
   -1    73 	mktime(&tm);
   -1    74 	return tm;
   -1    75 }
   -1    76 
   -1    77 struct tpl mktpl() {
   -1    78 	struct tpl tpl = {
   -1    79 		.year=0,
   -1    80 		.month=0,
   -1    81 		.day=0,
   -1    82 		.repeat=0,
   -1    83 		.weekday=0,
   -1    84 		.nth_of_month=0,
   -1    85 		.from_easter=0,
   -1    86 		.from_paskha=0,
   -1    87 		.easter=false,
   -1    88 		.paskha=false,
   -1    89 	};
   -1    90 	return tpl;
   -1    91 }
   -1    92 
   -1    93 struct tm *today() {
   -1    94 	time_t t = time(NULL);
   -1    95 	return localtime(&t);
   -1    96 }
   -1    97 
   67    98 struct tm easter(int year, bool paskha) {
   68    99 	/* https://de.wikipedia.org/wiki/Gau%C3%9Fsche_Osterformel#Eine_erg.C3.A4nzte_Osterformel */
   69   100 	int k = year / 100;
@@ -85,24 +116,19 @@ struct tm easter(int year, bool paskha) {
   85   116 		os += k - year / 400 - 2;
   86   117 	}
   87   118 
   88    -1 	struct tm t = {
   89    -1 		.tm_year=year - 1900,
   90    -1 		.tm_mon=2,
   91    -1 		.tm_mday=os,
   92    -1 	};
   93    -1 	mktime(&t);
   -1   119 	struct tm tm = mkdate(year, 3, os);
   94   120 
   95    -1 	return t;
   -1   121 	return tm;
   96   122 }
   97   123 
   98   124 struct tm add_days(struct tm date, int days) {
   99    -1 	struct tm d = {
   -1   125 	struct tm tm = {
  100   126 		.tm_year=date.tm_year,
  101   127 		.tm_mon=date.tm_mon,
  102   128 		.tm_mday=date.tm_mday + days,
  103   129 	};
  104    -1 	mktime(&d);
  105    -1 	return d;
   -1   130 	mktime(&tm);
   -1   131 	return tm;
  106   132 }
  107   133 
  108   134 bool date_comp(struct tm a, struct tm b) {
@@ -115,11 +141,7 @@ bool date_comp(struct tm a, struct tm b) {
  115   141 bool is_match(struct tpl tpl, struct tm date) {
  116   142 	if (tpl.repeat) {
  117   143 		if (tpl.day && tpl.month && tpl.year) {
  118    -1 			struct tm reference = {
  119    -1 				.tm_year=tpl.year - 1900,
  120    -1 				.tm_mon=tpl.month - 1,
  121    -1 				.tm_mday=tpl.day,
  122    -1 			};
   -1   144 			struct tm reference = mkdate(tpl.year, tpl.month, tpl.day);
  123   145 			time_t delta = difftime(mktime(&reference), mktime(&date));
  124   146 			int delta_days = delta / 60 / 60 / 24;
  125   147 			if (delta_days % (7 * tpl.repeat) != 0) {
@@ -175,19 +197,8 @@ bool is_match(struct tpl tpl, struct tm date) {
  175   197 }
  176   198 
  177   199 struct tpl parse_date(char *s) {
  178    -1 	time_t t = time(NULL);
  179    -1 	struct tm *TODAY = localtime(&t);
  180    -1 
  181    -1 	struct tpl tpl = {
  182    -1 		.repeat=0,
  183    -1 		.day=0,
  184    -1 		.month=0,
  185    -1 		.year=0,
  186    -1 		.weekday=0,
  187    -1 		.nth_of_month=0,
  188    -1 		.from_easter=0,
  189    -1 		.from_paskha=0,
  190    -1 	};
   -1   200 	struct tm *TODAY = today();
   -1   201 	struct tpl tpl = mktpl();
  191   202 
  192   203 	int n = 0;
  193   204 	if (strchr(s, '+')) {
@@ -387,12 +398,11 @@ int main(int argc, char *argv[]) {
  387   398 
  388   399 	struct line *first = get_lines("~/.calendar/calendar");
  389   400 
  390    -1 	time_t t = time(NULL);
  391    -1 	struct tm *today = localtime(&t);
   -1   401 	struct tm *TODAY = today();
  392   402 
  393   403 	struct tm day;
  394   404 	for (int i = 0; i < days; i++) {
  395    -1 		day = add_days(*today, i);
   -1   405 		day = add_days(*TODAY, i);
  396   406 		print_matches(day, first);
  397   407 	}
  398   408