Use parse_{time,duration}() where appropriate

Make use of these new helpers at various places. Note that this patch
implies a few behavioural changes:

* Short forms such as "23:" and ":45" are allowed when entering times.

* Durations always need to be prefixed with a plus sign ("+"), with the
  nice side effect that you can now use "+3:30" to declare an
  appointment that lasts three hours and thirty minutes (that's much
  more convenient than "+210").

Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
This commit is contained in:
Lukas Fleischer 2011-10-05 09:28:17 +02:00
parent 0acbc06c43
commit 39dd3c251d
2 changed files with 39 additions and 43 deletions

View File

@ -155,26 +155,26 @@ apoint_add (void)
{ {
#define LTIME 6 #define LTIME 6
char *mesg_1 = char *mesg_1 =
_("Enter start time ([hh:mm] or [h:mm]), " _("Enter start time ([hh:mm]), leave blank for an all-day event : ");
"leave blank for an all-day event : ");
char *mesg_2 = char *mesg_2 =
_("Enter end time ([hh:mm] or [h:mm]) or duration (in minutes) : "); _("Enter end time ([hh:mm]) or duration ([+hh:mm]) : ");
char *mesg_3 = _("Enter description :"); char *mesg_3 = _("Enter description :");
char *format_message_1 = char *format_message_1 =
_("You entered an invalid start time, should be [h:mm] or [hh:mm]"); _("You entered an invalid start time, should be [hh:mm]");
char *format_message_2 = char *format_message_2 =
_("You entered an invalid end time, should be [h:mm] or [hh:mm] or [mm]"); _("You entered an invalid end time, should be [hh:mm], [+hh:mm] or [+mm]");
char *enter_str = _("Press [Enter] to continue"); char *enter_str = _("Press [Enter] to continue");
int Id = 1; int Id = 1;
char item_time[LTIME] = ""; char item_time[LTIME] = "";
char item_mesg[BUFSIZ] = ""; char item_mesg[BUFSIZ] = "";
long apoint_duration = 0, apoint_start; long apoint_start;
unsigned heures, minutes; unsigned heures, minutes;
unsigned apoint_duration;
unsigned end_h, end_m; unsigned end_h, end_m;
int is_appointment = 1; int is_appointment = 1;
/* Get the starting time */ /* Get the starting time */
do for (;;)
{ {
status_mesg (mesg_1, ""); status_mesg (mesg_1, "");
if (getstring (win[STA].p, item_time, LTIME, 0, 1) != GETSTRING_ESC) if (getstring (win[STA].p, item_time, LTIME, 0, 1) != GETSTRING_ESC)
@ -184,18 +184,18 @@ apoint_add (void)
is_appointment = 0; is_appointment = 0;
break; break;
} }
else if (check_time (item_time) != 1)
if (parse_time (item_time, &heures, &minutes) == 1)
break;
else
{ {
status_mesg (format_message_1, enter_str); status_mesg (format_message_1, enter_str);
(void)wgetch (win[STA].p); (void)wgetch (win[STA].p);
} }
else
(void)sscanf (item_time, "%u:%u", &heures, &minutes);
} }
else else
return; return;
} }
while (check_time (item_time) != 1);
/* /*
* Check if an event or appointment is entered, * Check if an event or appointment is entered,
@ -205,23 +205,16 @@ apoint_add (void)
if (is_appointment) if (is_appointment)
{ /* Get the appointment duration */ { /* Get the appointment duration */
item_time[0] = '\0'; item_time[0] = '\0';
do for (;;)
{ {
status_mesg (mesg_2, ""); status_mesg (mesg_2, "");
if (getstring (win[STA].p, item_time, LTIME, 0, 1) != GETSTRING_VALID) if (getstring (win[STA].p, item_time, LTIME, 0, 1) != GETSTRING_ESC)
return; //nothing entered, cancel adding of event
else if (check_time (item_time) == 0)
{ {
status_mesg (format_message_2, enter_str); if (*item_time == '+' && parse_duration (item_time + 1,
(void)wgetch (win[STA].p); &apoint_duration) == 1)
} break;
else else if (parse_time (item_time, &end_h, &end_m) == 1)
{ {
if (check_time (item_time) == 2)
apoint_duration = atoi (item_time);
else if (check_time (item_time) == 1)
{
(void)sscanf (item_time, "%u:%u", &end_h, &end_m);
if (end_h < heures || ((end_h == heures) && (end_m < minutes))) if (end_h < heures || ((end_h == heures) && (end_m < minutes)))
{ {
apoint_duration = MININSEC - minutes + end_m apoint_duration = MININSEC - minutes + end_m
@ -232,10 +225,17 @@ apoint_add (void)
apoint_duration = MININSEC - minutes apoint_duration = MININSEC - minutes
+ end_m + (end_h - (heures + 1)) * MININSEC; + end_m + (end_h - (heures + 1)) * MININSEC;
} }
break;
}
else
{
status_mesg (format_message_2, enter_str);
(void)wgetch (win[STA].p);
} }
} }
else
return;
} }
while (check_time (item_time) == 0);
} }
else /* Insert the event Id */ else /* Insert the event Id */
Id = 1; Id = 1;

View File

@ -567,26 +567,28 @@ day_chk_busy_slices (struct date day, int slicesno, int *slices)
} }
/* Request the user to enter a new time. */ /* Request the user to enter a new time. */
static char * static int
day_edit_time (long time) day_edit_time (int time, unsigned *new_hour, unsigned *new_minute)
{ {
char *timestr; char *timestr = date_sec2date_str (time, "%H:%M");
char *msg_time = _("Enter the new time ([hh:mm] or [h:mm]) : "); char *msg_time = _("Enter the new time ([hh:mm]) : ");
char *enter_str = _("Press [Enter] to continue"); char *enter_str = _("Press [Enter] to continue");
char *fmt_msg = _("You entered an invalid time, should be [h:mm] or [hh:mm]"); char *fmt_msg = _("You entered an invalid time, should be [hh:mm]");
while (1) for (;;)
{ {
status_mesg (msg_time, ""); status_mesg (msg_time, "");
timestr = date_sec2date_str (time, "%H:%M");
updatestring (win[STA].p, &timestr, 0, 1); updatestring (win[STA].p, &timestr, 0, 1);
if (check_time (timestr) != 1 || strlen (timestr) == 0) if (parse_time (timestr, new_hour, new_minute) == 1)
{
mem_free (timestr);
return 1;
}
else
{ {
status_mesg (fmt_msg, enter_str); status_mesg (fmt_msg, enter_str);
(void)wgetch (win[STA].p); (void)wgetch (win[STA].p);
} }
else
return (timestr);
} }
} }
@ -596,15 +598,12 @@ update_start_time (long *start, long *dur)
long newtime; long newtime;
unsigned hr, mn; unsigned hr, mn;
int valid_date; int valid_date;
char *timestr;
char *msg_wrong_time = _("Invalid time: start time must be before end time!"); char *msg_wrong_time = _("Invalid time: start time must be before end time!");
char *msg_enter = _("Press [Enter] to continue"); char *msg_enter = _("Press [Enter] to continue");
do do
{ {
timestr = day_edit_time (*start); day_edit_time (*start, &hr, &mn);
(void)sscanf (timestr, "%u:%u", &hr, &mn);
mem_free (timestr);
newtime = update_time_in_date (*start, hr, mn); newtime = update_time_in_date (*start, hr, mn);
if (newtime < *start + *dur) if (newtime < *start + *dur)
{ {
@ -627,11 +626,8 @@ update_duration (long *start, long *dur)
{ {
long newtime; long newtime;
unsigned hr, mn; unsigned hr, mn;
char *timestr;
timestr = day_edit_time (*start + *dur); day_edit_time (*start + *dur, &hr, &mn);
(void)sscanf (timestr, "%u:%u", &hr, &mn);
mem_free (timestr);
newtime = update_time_in_date (*start, hr, mn); newtime = update_time_in_date (*start, hr, mn);
*dur = (newtime > *start) ? newtime - *start : DAYINSEC + newtime - *start; *dur = (newtime > *start) ? newtime - *start : DAYINSEC + newtime - *start;
} }