Make parse_date accept several short forms.

Short forms are only accepted in interactive mode (e.g. when using the
"Go to" function, editing a recurrent item's end date, and so on).

Samples: "1/1/30" for "01/01/2030", "26" for the 26th of the currently
selected month/year or "3/1" for Mar 01 (or Jan 03, depending on the
date format) of the currently selected year.
This commit is contained in:
Lukas Fleischer 2011-01-11 22:10:48 +00:00
parent b8779e714a
commit c8af480f52
7 changed files with 87 additions and 56 deletions

View File

@ -1,3 +1,17 @@
2011-01-11 Lukas Fleischer <calcurse@cryptocrack.de>
* src/calcurse.h
* src/utils.c: Make parse_date accept several short forms, e.g.
"1/1/30" for "01/01/2030", "26" for the 26th of the currently
selected month/year or "3/1" for Mar 01 (or Jan 03, depending
on the date format) of the currently selected year.
* src/args.c
* src/calendar.c
* src/day.c
* src/recur.c: Make everything work with the new parse_date
function accepting short forms.
2010-11-04 Lukas Fleischer <calcurse@cryptocrack.de> 2010-11-04 Lukas Fleischer <calcurse@cryptocrack.de>
* src/utils.c: Added "backword-kill-word" functionality to * src/utils.c: Added "backword-kill-word" functionality to

View File

@ -1,4 +1,4 @@
/* $calcurse: args.c,v 1.63 2010/05/26 18:18:28 culot Exp $ */ /* $calcurse: args.c,v 1.64 2011/01/11 22:10:48 fleischer Exp $ */
/* /*
* Calcurse - text-based organizer * Calcurse - text-based organizer
@ -578,7 +578,7 @@ date_arg (char *ddate, int add_line, int print_note, struct conf *conf,
else else
{ /* a date was entered */ { /* a date was entered */
if (parse_date (ddate, conf->input_datefmt, (int *)&day.yyyy, if (parse_date (ddate, conf->input_datefmt, (int *)&day.yyyy,
(int *)&day.mm, (int *)&day.dd)) (int *)&day.mm, (int *)&day.dd, NULL))
{ {
app_found = app_arg (add_line, &day, 0, print_note, conf, regex); app_found = app_arg (add_line, &day, 0, print_note, conf, regex);
} }
@ -630,7 +630,7 @@ date_arg_extended (char *startday, char *range, int add_line, int print_note,
if (startday != NULL) if (startday != NULL)
{ {
if (parse_date (startday, conf->input_datefmt, (int *)&t.tm_year, if (parse_date (startday, conf->input_datefmt, (int *)&t.tm_year,
(int *)&t.tm_mon, (int *)&t.tm_mday)) (int *)&t.tm_mon, (int *)&t.tm_mday, NULL))
{ {
t.tm_year -= 1900; t.tm_year -= 1900;
t.tm_mon--; t.tm_mon--;

View File

@ -1,4 +1,4 @@
/* $Id: calcurse.h,v 1.5 2010/03/22 08:16:33 culot Exp $ */ /* $Id: calcurse.h,v 1.6 2011/01/11 22:10:48 fleischer Exp $ */
/* /*
* Calcurse - text-based organizer * Calcurse - text-based organizer
@ -873,7 +873,8 @@ long mystrtol (const char *);
void print_bool_option_incolor (WINDOW *, unsigned, int, int); void print_bool_option_incolor (WINDOW *, unsigned, int, int);
char *new_tempfile (const char *, int); char *new_tempfile (const char *, int);
void erase_note (char **, enum eraseflg); void erase_note (char **, enum eraseflg);
int parse_date (char *, enum datefmt, int *, int *, int *); int parse_date (char *, enum datefmt, int *, int *, int *,
struct date *);
char *str_toupper (char *); char *str_toupper (char *);
void file_close (FILE *, const char *); void file_close (FILE *, const char *);
void psleep (unsigned); void psleep (unsigned);

View File

@ -1,4 +1,4 @@
/* $calcurse: calendar.c,v 1.38 2010/10/23 10:19:47 culot Exp $ */ /* $calcurse: calendar.c,v 1.39 2011/01/11 22:10:48 fleischer Exp $ */
/* /*
* Calcurse - text-based organizer * Calcurse - text-based organizer
@ -646,11 +646,8 @@ calendar_change_day (int datefmt)
wrong_day = 0; wrong_day = 0;
calendar_goto_today (); calendar_goto_today ();
} }
else if (strlen (selected_day) != LDAY - 1) else if (parse_date (selected_day, datefmt, &dyear, &dmonth, &dday,
{ calendar_get_slctd_day ()))
wrong_day = 1;
}
else if (parse_date (selected_day, datefmt, &dyear, &dmonth, &dday))
{ {
wrong_day = 0; wrong_day = 0;
/* go to chosen day */ /* go to chosen day */

View File

@ -1,4 +1,4 @@
/* $calcurse: day.c,v 1.53 2010/03/20 10:54:44 culot Exp $ */ /* $calcurse: day.c,v 1.54 2011/01/11 22:10:48 fleischer Exp $ */
/* /*
* Calcurse - text-based organizer * Calcurse - text-based organizer
@ -757,7 +757,7 @@ update_rept (struct rpt **rpt, const long start, struct conf *conf)
int newmonth, newday, newyear; int newmonth, newday, newyear;
if (parse_date (timstr, conf->input_datefmt, if (parse_date (timstr, conf->input_datefmt,
&newyear, &newmonth, &newday)) &newyear, &newmonth, &newday, calendar_get_slctd_day ()))
{ {
t = start; t = start;
lt = localtime (&t); lt = localtime (&t);

View File

@ -1,4 +1,4 @@
/* $calcurse: recur.c,v 1.54 2010/03/20 10:54:47 culot Exp $ */ /* $calcurse: recur.c,v 1.55 2011/01/11 22:10:48 fleischer Exp $ */
/* /*
* Calcurse - text-based organizer * Calcurse - text-based organizer
@ -971,7 +971,7 @@ recur_repeat_item (struct conf *conf)
else else
{ {
if (parse_date (user_input, conf->input_datefmt, if (parse_date (user_input, conf->input_datefmt,
&year, &month, &day)) &year, &month, &day, calendar_get_slctd_day ()))
{ {
t = p->start; t = p->start;
lt = localtime (&t); lt = localtime (&t);

View File

@ -1,4 +1,4 @@
/* $calcurse: utils.c,v 1.86 2010/11/04 10:55:07 fleischer Exp $ */ /* $calcurse: utils.c,v 1.87 2011/01/11 22:10:48 fleischer Exp $ */
/* /*
* Calcurse - text-based organizer * Calcurse - text-based organizer
@ -47,6 +47,8 @@
#include "calcurse.h" #include "calcurse.h"
#define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
/* General routine to exit calcurse properly. */ /* General routine to exit calcurse properly. */
void void
exit_calcurse (int status) exit_calcurse (int status)
@ -883,63 +885,80 @@ erase_note (char **note, enum eraseflg flag)
/* /*
* Convert a string containing a date into three integers containing the year, * Convert a string containing a date into three integers containing the year,
* month and day. * month and day.
*
* If a pointer to a date structure containing the current date is passed as
* last parameter ("slctd_date"), the function will accept several short forms,
* e.g. "26" for the 26th of the current month/year or "3/1" for Mar 01 (or Jan
* 03, depending on the date format) of the current year. If a null pointer is
* passed, short forms won't be accepted at all.
*
* Returns 1 if sucessfully converted or 0 if the string is an invalid date. * Returns 1 if sucessfully converted or 0 if the string is an invalid date.
*/ */
int int
parse_date (char *date_string, enum datefmt datefmt, int *year, int *month, parse_date (char *date_string, enum datefmt datefmt, int *year, int *month,
int *day) int *day, struct date *slctd_date)
{ {
int in1, in2, in3; char sep = (datefmt == DATEFMT_ISO) ? '-' : '/';
int lyear, lmonth, lday; char *p;
int in[3] = {0, 0, 0}, n = 0;
int d, m, y;
if (date_string == 0) if (!date_string) return 0;
return 0;
if (datefmt == DATEFMT_ISO) /* parse string into in[], read up to three integers */
{ for (p = date_string; *p; p++) {
if (sscanf (date_string, "%d - %d - %d", &in1, &in2, &in3) < 3) if (*p == sep) {
return 0; if ((++n) > 2) return 0;
}
else if ((*p >= '0') && (*p <= '9')) {
in[n] = in[n] * 10 + (int)(*p - '0');
} }
else else
{
if (sscanf (date_string, "%d / %d / %d", &in1, &in2, &in3) < 3)
return 0; return 0;
} }
switch (datefmt)
{ if ((!slctd_date && n < 2) || in[n] == 0) return 0;
/* convert into day, month and year, depending on the date format */
switch (datefmt) {
case DATEFMT_MMDDYYYY: case DATEFMT_MMDDYYYY:
lmonth = in1; m = in[n > 0 ? 0 : 1];
lday = in2; d = in[n > 0 ? 1 : 0];
lyear = in3; y = in[2];
break; break;
case DATEFMT_DDMMYYYY: case DATEFMT_DDMMYYYY:
lday = in1; d = in[0];
lmonth = in2; m = in[1];
lyear = in3; y = in[2];
break; break;
case DATEFMT_YYYYMMDD: case DATEFMT_YYYYMMDD:
case DATEFMT_ISO: case DATEFMT_ISO:
lyear = in1; y = in[0];
lmonth = in2; m = in[n - 1];
lday = in3; d = in[n];
break; break;
default: default:
return 0; return 0;
} }
if (lyear < 1 || lyear > 9999 if (y > 0 && y < 100) {
|| lmonth < 1 || lmonth > 12 /* convert "YY" format into "YYYY" */
|| lday < 1 || lday > 31) y += slctd_date->yyyy - slctd_date->yyyy % 100;
}
else if (n < 2) {
/* set year and, optionally, month if short from is used */
y = slctd_date->yyyy;
if (n < 1) m = slctd_date->mm;
}
/* check if date is valid, take leap years into account */
if (y < 1902 || y > 2037 || m < 1 || m > 12 || d < 1 ||
d > days[m - 1] + (m == 2 && isleap (y)) ? 1 : 0)
return 0; return 0;
if (year != NULL) if (year) *year = y;
*year = lyear; if (month) *month = m;
if (day) *day = d;
if (month != NULL)
*month = lmonth;
if (day != NULL)
*day = lday;
return 1; return 1;
} }