Commits

Sebastian Freundt committed 88d27ea Merge

Merge branch 'feat/default' into next

* feat/default:
minor, support --default=DT in dconv tool, using dt_set_default() and the new massaging routine
minor, provide api function dt_set_default()
minor, provide dflt_tm() getter/setter and use that in the massaging routine

  • Participants
  • Parent commits a92b789, 5039429

Comments (0)

Files changed (4)

 	return tm;
 }
 
+static struct tm
+dflt_tm(const struct dt_dt_s *set)
+{
+/* getter/setter and singleton for SET == NULL */
+	static struct tm tm;
+
+	if (LIKELY(set == NULL && tm.tm_year != 0U)) {
+		/* show what we've got */
+		;
+	} else if (set == NULL) {
+		/* take over the value of now */
+		struct timeval tv = now_tv();
+		tm = now_tm();
+		tm.tm_hour = (tv.tv_sec % 86400U) / 60U / 60U;
+		tm.tm_min = (tv.tv_sec % 3600U) / 60U;
+		tm.tm_sec = (tv.tv_sec % 60U);
+	} else {
+		switch (set->typ) {
+		case DT_YMD:
+			tm.tm_year = set->d.ymd.y;
+			tm.tm_mon = set->d.ymd.m;
+			tm.tm_mday = set->d.ymd.d;
+			tm.tm_hour = set->t.hms.h;
+			tm.tm_min = set->t.hms.m;
+			tm.tm_sec = set->t.hms.s;
+			break;
+		default:
+			/* good question */
+			return (struct tm){};
+		}
+	}
+	return tm;
+}
+
 static struct strpdt_s
 massage_strpdt(struct strpdt_s d)
 {
  * the pieces of code that use the guesser for different reasons */
 	if (UNLIKELY(d.sd.y == 0U)) {
 		static const struct strpd_s d0 = {};
-		struct tm now = now_tm();
+		struct tm now = dflt_tm(NULL);
 
 		if (UNLIKELY(memcmp(&d.sd, &d0, sizeof(d0)) == 0U)) {
 			goto msgg_time;
 	msgg_time:
 		/* same for time values, but obtain those through now_tv() */
 		if (UNLIKELY(!d.st.flags.h_set)) {
-			struct timeval tv = now_tv();
-
-			d.st.h = (tv.tv_sec % 86400U) / 60U / 60U;
+			d.st.h = now.tm_hour;
 			if (LIKELY(d.st.flags.m_set)) {
 				goto out;
 			}
-			d.st.m = (tv.tv_sec % 3600U) / 60U;
+			d.st.m = now.tm_min;
 			if (LIKELY(d.st.flags.s_set)) {
 				goto out;
 			}
-			d.st.s = (tv.tv_sec % 60U);
+			d.st.s = now.tm_sec;
 		}
 	}
 out:
 # pragma GCC diagnostic warning "-Wcast-qual"
 #endif	/* __INTEL_COMPILER */
 
+DEFUN void
+dt_set_default(struct dt_dt_s dt)
+{
+	(void)dflt_tm(&dt);
+	return;
+}
+
 #endif	/* INCLUDED_date_core_c_ */
 /* dt-core.c ends here */
  * Convert a dt_dt_s to an epoch difference, based on the GPS epoch. */
 extern dt_ssexy_t dt_to_gps_epoch(struct dt_dt_s);
 
+/**
+ * Set specific fallback date/time to use when input is underspecified.
+ * Internally, when no default is set and input is underspecified  the
+ * value of `dt_datetime()' (i.e. now) is used to fill fields up. */
+DECLF void dt_set_default(struct dt_dt_s);
+
 
 /* some useful gimmicks, sort of */
 static inline __attribute__((pure, const)) struct dt_dt_s

src/dconv-clo.ggo

 QUIET
 FORMAT
 INPUT_FORMAT
+
+option "default" -
+	"For underspecified input use DT as a fallback to fill in \
+missing fields.  Must be a date/time in ISO8601 format.  If omitted \
+the default value is the current date/time."
+	string typestr="DT" optional
+
 BACKSLASH_ESCAPES
 SED_MODE
 
 FROM_ZONE
 TO_ZONE
-
 	if (argi->zone_given) {
 		z = zif_open(argi->zone_arg);
 	}
+	if (argi->default_given) {
+		struct dt_dt_s dflt = dt_strpdt(argi->default_arg, NULL, NULL);
+		dt_set_default(dflt);
+	}
 
 	if (argi->inputs_num) {
 		for (size_t i = 0; i < argi->inputs_num; i++) {