+ if (do_gmt == -1) {
+ /* The diet libc doesn't respect the TZ environment variable */
+ tz = ss_safe_getenv("TZ");
+ if (!tz)
+ tz = "";
+ do_gmt = !strcmp(tz, "GMT") || !strcmp(tz, "GMT0");
+ }
+
+ return asctime((do_gmt) ? gmtime(&t) : localtime(&t));
+}
+
+/*
+ * Parse a string as a time. Return ((time_t)-1) if the string
+ * doesn't appear to be a sane time.
+ */
+extern __s64 string_to_time(const char *arg)
+{
+ struct tm ts;
+ __s64 ret;
+ char *tmp;
+
+ if (strcmp(arg, "now") == 0) {
+ return time(0);
+ }
+ if (arg[0] == '@') {
+ /* interpret it as an integer */
+ arg++;
+ fallback:
+ ret = strtoll(arg, &tmp, 0);
+ if (*tmp)
+ return -1;
+ return ret;
+ }
+ memset(&ts, 0, sizeof(ts));
+#ifdef HAVE_STRPTIME
+ tmp = strptime(arg, "%Y%m%d%H%M%S", &ts);
+ if (tmp == NULL)
+ tmp = strptime(arg, "%Y%m%d%H%M", &ts);
+ if (tmp == NULL)
+ tmp = strptime(arg, "%Y%m%d", &ts);
+ if (tmp == NULL)
+ goto fallback;
+#else
+ sscanf(arg, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
+ &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
+ ts.tm_year -= 1900;
+ ts.tm_mon -= 1;
+ if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
+ ts.tm_mday <= 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
+ ts.tm_min > 59 || ts.tm_sec > 61)
+ goto fallback;
+#endif
+ ts.tm_isdst = -1;
+ /* strptime() may only update the specified fields, which does not
+ * necessarily include ts.tm_yday (%j). Calculate this if unset:
+ *
+ * Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
+ * 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
+ *
+ * Start with 31 days per month. Even months have only 30 days, but
+ * reverse in August, subtract one day for those months. February has
+ * only 28 days, not 30, subtract two days. Add day of month, minus
+ * one, since day is not finished yet. Leap years handled afterward. */
+ if (ts.tm_yday == 0)
+ ts.tm_yday = (ts.tm_mon * 31) -
+ ((ts.tm_mon - (ts.tm_mon > 7)) / 2) -
+ 2 * (ts.tm_mon > 1) + ts.tm_mday - 1;
+ ret = ts.tm_sec + ts.tm_min*60 + ts.tm_hour*3600 + ts.tm_yday*86400 +
+ ((__s64) ts.tm_year-70)*31536000 +
+ (((__s64) ts.tm_year-69)/4)*86400 -
+ (((__s64) ts.tm_year-1)/100)*86400 +
+ (((__s64) ts.tm_year+299)/400)*86400;
+ return ret;