+/*
+ * 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 size_t
+format_date(char *str, size_t str_len, char *fmt, int year, int month, int day)
+{
+ char *s = str;
+ size_t len;
+
+ while(*fmt && (s - str + 1 < str_len)) {
+ if(*fmt != '%') {
+ *s++ = *fmt++;
+ continue;
+ }
+
+ len = str_len - (str - s);
+ switch(*++fmt) {
+ case 'y': s += snprintf(s, len, "%d", year); break;
+ case 'Y': s += snprintf(s, len, "%04d", year); break;
+ case 'm': s += snprintf(s, len, "%d", month); break;
+ 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 '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;
+ 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 valid = 1;
+ int month_length[13] =
+ { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
+
+ /*
+ * leap year
+ */
+ if ((!(year % 4)) && ((year % 100) || !(year % 400)))
+ month_length[2] = 29;
+
+ if (month < 1 || month > 12)
+ valid = 0;
+ else if (day < 1 || day > month_length[month])
+ valid = 0;
+ else if (year < 0) /* we don't accept negative year numbers */
+ valid = 0;
+
+ return valid;
+}
+
+int
+parse_date_string(char *str, int *day, int *month, int *year)
+{
+ int i = 0;
+ 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;
+ i++;
+ }
+
+ while(*s) {
+ if(isdigit(*s)) {
+ s++;
+ continue;
+ } else if(*s == '-') {
+ if(++i > 3)
+ return FALSE;
+ *s++ = '\0';
+ switch(i) {
+ case 1: *year = safe_atoi(p); break;
+ case 2: *month = safe_atoi(p); break;
+ }
+ p = s;
+ } else
+ return FALSE;
+ }
+
+ if (i != 2 || !*p)
+ return FALSE;
+
+ *day = atoi(p);
+
+ return is_valid_date(*day, *month, *year);
+}
+
+static void
+edit_date(int item, int nb)
+{
+ int i, date[3], old;
+ char *s = db_fget_byid(item, nb);
+ char *field[] = { N_("Day: "), N_("Month: "), N_("Year (optional): ") };
+
+ 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;
+ if(change_field(gettext(field[i]), &s, 5))
+ return; /* user aborted with ^G */
+
+ date[i] = (s && is_number(s)) ? atoi(s) : 0;
+
+ if(!s) {
+ switch(i) {
+ case 0: db_fput_byid(item, nb, NULL); /*delete*/
+ case 1: /* fall through */ return;
+ }
+ } else
+ xfree(s);
+ }
+
+ /* ISO 8601 date, of the YYYY-MM-DD or --MM-DD format */
+ if(is_valid_date(date[0], date[1], date[2])) {
+ if(date[2])
+ s = strdup_printf("%04d-%02d-%02d",
+ date[2], date[1], date[0]);
+ else
+ s = strdup_printf("--%02d-%02d", date[1], date[0]);
+
+ db_fput_byid(item, nb, xstrdup(s));
+ } else
+ statusline_msg(_("Invalid date"));
+}