Merge "%(start"{,str}")" and "%(end"{,str}")"

Add "default" and "epoch" extended formats for dates. "default" is the
same as "%H:%M", "epoch" prints the number of seconds since January 1,
1970.

Also, change the semantics of "%(start)" to return dates in default
format (same format that "%(startstr)" used to use before this patch).
The old "%(start)" behavior can be emulated by using "%(start:epoch)".
The same applies to "%(end)" and "%(endstr)".

Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
This commit is contained in:
Lukas Fleischer 2012-04-13 19:19:48 +02:00
parent b80c1f6364
commit 0791eaabca

View File

@ -53,10 +53,8 @@
enum format_specifier { enum format_specifier {
FS_STARTDATE, FS_STARTDATE,
FS_STARTDATESTR,
FS_DURATION, FS_DURATION,
FS_ENDDATE, FS_ENDDATE,
FS_ENDDATESTR,
FS_MESSAGE, FS_MESSAGE,
FS_NOTE, FS_NOTE,
FS_NOTEFILE, FS_NOTEFILE,
@ -1092,15 +1090,17 @@ parse_fs (const char **s, char *extformat)
switch (**s) switch (**s)
{ {
case 's': case 's':
strcpy (extformat, "epoch");
return FS_STARTDATE; return FS_STARTDATE;
case 'S': case 'S':
return FS_STARTDATESTR; return FS_STARTDATE;
case 'd': case 'd':
return FS_DURATION; return FS_DURATION;
case 'e': case 'e':
strcpy (extformat, "epoch");
return FS_ENDDATE; return FS_ENDDATE;
case 'E': case 'E':
return FS_ENDDATESTR; return FS_ENDDATE;
case 'm': case 'm':
return FS_MESSAGE; return FS_MESSAGE;
case 'n': case 'n':
@ -1138,14 +1138,10 @@ parse_fs (const char **s, char *extformat)
if (!strcmp (buf, "start")) if (!strcmp (buf, "start"))
return FS_STARTDATE; return FS_STARTDATE;
else if (!strcmp (buf, "startstr"))
return FS_STARTDATESTR;
else if (!strcmp (buf, "duration")) else if (!strcmp (buf, "duration"))
return FS_DURATION; return FS_DURATION;
else if (!strcmp (buf, "end")) else if (!strcmp (buf, "end"))
return FS_ENDDATE; return FS_ENDDATE;
else if (!strcmp (buf, "endstr"))
return FS_ENDDATESTR;
else if (!strcmp (buf, "message")) else if (!strcmp (buf, "message"))
return FS_MESSAGE; return FS_MESSAGE;
else if (!strcmp (buf, "noteid")) else if (!strcmp (buf, "noteid"))
@ -1171,15 +1167,20 @@ print_date (long date, const char *extformat)
{ {
char buf[BUFSIZ]; char buf[BUFSIZ];
if (extformat[0] != '\0') if (!strcmp (extformat, "epoch"))
printf ("%ld", date);
else
{ {
time_t t = date; time_t t = date;
struct tm *lt = localtime ((time_t *)&t); struct tm *lt = localtime ((time_t *)&t);
strftime (buf, BUFSIZ, extformat, lt);
if (extformat[0] == '\0' || !strcmp (extformat, "default"))
strftime (buf, BUFSIZ, "%H:%M", lt);
else
strftime (buf, BUFSIZ, extformat, lt);
printf ("%s", buf); printf ("%s", buf);
} }
else
printf ("%ld", date);
} }
/* Print a formatted appointment to stdout. */ /* Print a formatted appointment to stdout. */
@ -1187,11 +1188,8 @@ void
print_apoint (const char *format, long day, struct apoint *apt) print_apoint (const char *format, long day, struct apoint *apt)
{ {
const char *p; const char *p;
char str_start[HRMIN_SIZE], str_end[HRMIN_SIZE];
char extformat[FS_EXT_MAXLEN]; char extformat[FS_EXT_MAXLEN];
apoint_sec2str (apt, day, str_start, str_end);
for (p = format; *p; p++) for (p = format; *p; p++)
{ {
if (*p == '%') { if (*p == '%') {
@ -1201,18 +1199,12 @@ print_apoint (const char *format, long day, struct apoint *apt)
case FS_STARTDATE: case FS_STARTDATE:
print_date (apt->start, extformat); print_date (apt->start, extformat);
break; break;
case FS_STARTDATESTR:
printf ("%s", str_start);
break;
case FS_DURATION: case FS_DURATION:
printf ("%ld", apt->dur); printf ("%ld", apt->dur);
break; break;
case FS_ENDDATE: case FS_ENDDATE:
print_date (apt->start + apt->dur, extformat); print_date (apt->start + apt->dur, extformat);
break; break;
case FS_ENDDATESTR:
printf ("%s", str_end);
break;
case FS_MESSAGE: case FS_MESSAGE:
printf ("%s", apt->mesg); printf ("%s", apt->mesg);
break; break;