/* This only rolls emails from the 'email' field, not emails from any
* field of type FIELD_EMAILS.
* TODO: expand to ask for which field to roll if several are present? */
-static void
+void
roll_emails(int item, enum rotate_dir dir)
{
abook_list *emails = csv_to_abook_list(db_fget(item, EMAIL));
int day, month, year;
char buf[64];
- /* put raw representation of date in buf */
find_field_number(cur->field->key, &nb);
- if((str = db_fget_byid(item, nb)) != NULL)
- strncpy(buf, str, sizeof(buf));
-
- if(str && parse_date_string(buf, &day, &month, &year)) {
+ str = db_fget_byid(item, nb);
+
+ if(parse_date_string(str, &day, &month, &year)) {
/* put locale representation of date in buf */
locale_date(buf, sizeof(buf), year, month, day);
mvwaddnstr(editw, y, TAB_COLON_POS + 2, buf,
}
/*
- * str is a buffer of max length str_len, which, after calling, will
- * contain a representation of the given [y, m, d] date using the
- * current locale (as defined by LC_TIME).
- *
- * Default is an ISO 8601 representation.
- *
- * %-sequences available to translators: %y, %Y, %m, %M, %d, %D represent
- * year, month, and day (the uppercase version telling to fill with leading
- * zeros if necessary)
+ * available %-sequences:
+ * - %y, %Y, %m, %M, %d, %D represent year, month, and day
+ * (the uppercase version telling to fill with leading zeros
+ * if necessary)
+ * - %I for ISO 8601 representation
*/
-static void
-locale_date(char *str, size_t str_len, int year, int month, int day)
+static size_t
+format_date(char *str, size_t str_len, char *fmt, int year, int month, int day)
{
- char *s = str, *fmt;
+ char *s = str;
size_t len;
-#if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE)
- fmt = year ? dcgettext(PACKAGE, "%Y-%M-%D", LC_TIME) :
- dcgettext(PACKAGE, "--%M-%D", LC_TIME);
-#else
- if(year)
- snprintf(str, str_len, "%04d-%02d-%02d", year, month, day);
- else
- snprintf(str, str_len, "--%02d-%02d", month, day);
- return;
-#endif
-
while(*fmt && (s - str + 1 < str_len)) {
if(*fmt != '%') {
*s++ = *fmt++;
case 'M': s += snprintf(s, len, "%02d", month); break;
case 'd': s += snprintf(s, len, "%d", day); break;
case 'D': s += snprintf(s, len, "%02d", day); break;
- case '%': /* fall through */
- default:
- *s++ = '%';
- *s++ = *fmt;
- break;
+ case 'I': s += format_date(s, len,
+ year ? "%Y-%M-%D" : "--%M-%D",
+ year, month, day);
+ break;
+ case '%': *s++ = '%'; break;
+ default: *s++ = '%'; *s++ = *fmt; break;
}
fmt++;
}
- *++s = 0;
+ *s = 0;
+ return s - str;
+}
+
+/*
+ * str is a buffer of max length str_len, which, after calling, will
+ * contain a representation of the given [y, m, d] date using the
+ * current locale (as defined by LC_TIME).
+ *
+ * In the absence of any localization, use an ISO 8601 representation.
+ */
+static void
+locale_date(char *str, size_t str_len, int year, int month, int day)
+{
+ char *fmt;
+
+#if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE)
+ fmt = year ? dcgettext(PACKAGE, "%Y-%M-%D", LC_TIME) :
+ dcgettext(PACKAGE, "--%M-%D", LC_TIME);
+#else
+ fmt = "%I";
+#endif
+ format_date(str, str_len, fmt, year, month, day);
}
static int is_valid_date(const int day, const int month, const int year)
}
int
-parse_date_string(char *s, int *day, int *month, int *year)
+parse_date_string(char *str, int *day, int *month, int *year)
{
int i = 0;
- char *p = s;
- assert(s && day && month && year);
+ char buf[12], *s, *p;
+
+ assert(day && month && year);
+
+ if(!str || !*str)
+ return FALSE;
+
+ p = s = strncpy(buf, str, sizeof(buf));
if(*s == '-' && *s++ == '-') { /* omitted year */
*year = 0;
}
p = s;
} else
- return FALSE;
+ return FALSE;
}
if (i != 2 || !*p)
static void
edit_date(int item, int nb)
{
- int i, date[3], old = FALSE;
- char buf[12], *s = db_fget_byid(item, nb);
+ int i, date[3], old;
+ char *s = db_fget_byid(item, nb);
char *field[] = { N_("Day: "), N_("Month: "), N_("Year (optional): ") };
- if(s) {
- strncpy(buf, s, sizeof(buf));
- old = parse_date_string(buf, &date[0], &date[1], &date[2]);
- }
+ old = parse_date_string(s, &date[0], &date[1], &date[2]);
for(i = 0; i < 3; i++) {
s = (old && date[i]) ? strdup_printf("%d", date[i]) : NULL;