Factor out parse_datetime()
Create a new function that takes a time stamp and updates the date or time components of that time stamp according to a given date/time string. Use that function for updating the start time of an item. Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
This commit is contained in:
parent
1f39b5c668
commit
343d68596d
@ -1136,6 +1136,7 @@ int check_time(unsigned, unsigned);
|
|||||||
int parse_time(const char *, unsigned *, unsigned *);
|
int parse_time(const char *, unsigned *, unsigned *);
|
||||||
int parse_duration(const char *, unsigned *);
|
int parse_duration(const char *, unsigned *);
|
||||||
int parse_date_duration(const char *, unsigned *);
|
int parse_date_duration(const char *, unsigned *);
|
||||||
|
int parse_datetime(const char *, long *);
|
||||||
void file_close(FILE *, const char *);
|
void file_close(FILE *, const char *);
|
||||||
void psleep(unsigned);
|
void psleep(unsigned);
|
||||||
int fork_exec(int *, int *, const char *, const char *const *);
|
int fork_exec(int *, int *, const char *, const char *const *);
|
||||||
|
30
src/ui-day.c
30
src/ui-day.c
@ -67,36 +67,16 @@ static int day_edit_time(int time)
|
|||||||
const char *enter_str = _("Press [Enter] to continue");
|
const char *enter_str = _("Press [Enter] to continue");
|
||||||
const char *fmt_msg =
|
const char *fmt_msg =
|
||||||
_("You entered an invalid time, should be [hh:mm] or [hhmm]");
|
_("You entered an invalid time, should be [hh:mm] or [hhmm]");
|
||||||
unsigned int hour, minute;
|
long ts = time;
|
||||||
int year, month, day;
|
|
||||||
struct date new_date;
|
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
status_mesg(msg_time, "");
|
status_mesg(msg_time, "");
|
||||||
if (updatestring(win[STA].p, &input, 0, 1) != GETSTRING_VALID)
|
if (updatestring(win[STA].p, &input, 0, 1) != GETSTRING_VALID)
|
||||||
return 0;
|
return 0;
|
||||||
char *inputcpy = mem_strdup(input);
|
if (parse_datetime(input, &ts))
|
||||||
char *p = strtok(inputcpy, " ");
|
return ts;
|
||||||
while (p) {
|
status_mesg(fmt_msg, enter_str);
|
||||||
if (parse_date(p, conf.input_datefmt, &year, &month,
|
wgetch(win[KEY].p);
|
||||||
&day, ui_calendar_get_slctd_day())) {
|
|
||||||
new_date.dd = day;
|
|
||||||
new_date.mm = month;
|
|
||||||
new_date.yyyy = year;
|
|
||||||
time = date2sec(new_date, 0, 0) +
|
|
||||||
get_item_time(time);
|
|
||||||
} else if (parse_time(p, &hour, &minute) == 1) {
|
|
||||||
time = update_time_in_date(time, hour, minute);
|
|
||||||
} else {
|
|
||||||
status_mesg(fmt_msg, enter_str);
|
|
||||||
wgetch(win[KEY].p);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
p = strtok(NULL, " ");
|
|
||||||
}
|
|
||||||
mem_free(inputcpy);
|
|
||||||
if (!p)
|
|
||||||
return time;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
39
src/utils.c
39
src/utils.c
@ -1103,6 +1103,45 @@ int parse_duration(const char *string, unsigned *duration)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Converts a string containing a date or a time into a time stamp.
|
||||||
|
*
|
||||||
|
* Takes a date/time string and a time stamp. If the string only contains a
|
||||||
|
* date, the date of the time stamp is updated while the time remains
|
||||||
|
* untouched. If the string only contains a time, the time of the time stamp is
|
||||||
|
* updated and the date remains the same. If the string contains both a date
|
||||||
|
* and a time, the time stamp is updated to match the given string.
|
||||||
|
*
|
||||||
|
* Returns 1 on success and 0 on failure.
|
||||||
|
*/
|
||||||
|
int parse_datetime(const char *string, long *ts)
|
||||||
|
{
|
||||||
|
char *t = mem_strdup(string);
|
||||||
|
char *p = strtok(t, " ");
|
||||||
|
|
||||||
|
unsigned int hour, minute;
|
||||||
|
int year, month, day;
|
||||||
|
struct date new_date;
|
||||||
|
|
||||||
|
while (p) {
|
||||||
|
if (parse_date(p, conf.input_datefmt, &year, &month, &day,
|
||||||
|
ui_calendar_get_slctd_day())) {
|
||||||
|
new_date.dd = day;
|
||||||
|
new_date.mm = month;
|
||||||
|
new_date.yyyy = year;
|
||||||
|
*ts = date2sec(new_date, 0, 0) + get_item_time(*ts);
|
||||||
|
} else if (parse_time(p, &hour, &minute) == 1) {
|
||||||
|
*ts = update_time_in_date(*ts, hour, minute);
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
p = strtok(NULL, " ");
|
||||||
|
}
|
||||||
|
|
||||||
|
mem_free(t);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
void file_close(FILE * f, const char *pos)
|
void file_close(FILE * f, const char *pos)
|
||||||
{
|
{
|
||||||
EXIT_IF((fclose(f)) != 0, _("Error when closing file at %s"), pos);
|
EXIT_IF((fclose(f)) != 0, _("Error when closing file at %s"), pos);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user