aboutsummaryrefslogtreecommitdiff
path: root/toys
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2019-02-14 06:50:35 -0600
committerRob Landley <rob@landley.net>2019-02-14 06:50:35 -0600
commit8bd644457282e1b94675f825c979256363b81ee6 (patch)
tree208a69477fcff1bbf24ba075ee5e700346660bf0 /toys
parentda11940f93ec52b00f7306195d73797921a29a95 (diff)
downloadtoybox-8bd644457282e1b94675f825c979256363b81ee6.tar.gz
Use current time for unspecified fields, set weekday to match date.
Diffstat (limited to 'toys')
-rw-r--r--toys/posix/date.c30
1 files changed, 16 insertions, 14 deletions
diff --git a/toys/posix/date.c b/toys/posix/date.c
index 40c1fed2..09a548f5 100644
--- a/toys/posix/date.c
+++ b/toys/posix/date.c
@@ -56,17 +56,17 @@ GLOBALS(
unsigned nano;
)
-static const char *formats[] = {
- // Formats with years must come first.
- "%Y-%m-%d %H:%M:%S", "%Y-%m-%d %H:%M", "%Y-%m-%d",
- "%H:%M:%S", "%H:%M", 0
-};
-
// Handle default posix date format (mmddhhmm[[cc]yy]) or @UNIX[.FRAC]
// returns 0 success, nonzero for error
static int parse_default(char *str, struct tm *tm)
{
+ time_t now;
int len = 0, i;
+ char *formats[] = {
+ // Formats with years must come first.
+ "%Y-%m-%d %H:%M:%S", "%Y-%m-%d %H:%M", "%Y-%m-%d",
+ "%H:%M:%S", "%H:%M"
+ };
// Parse @UNIXTIME[.FRACTION]
if (*str == '@') {
@@ -87,23 +87,20 @@ static int parse_default(char *str, struct tm *tm)
}
if (str[len]) return 1;
tt = ll;
- gmtime_r(&tt, tm);
+ localtime_r(&tt, tm);
return 0;
}
// Is it one of the fancy formats?
- for (i = 0; formats[i]; ++i) {
- time_t now = time(NULL);
+ for (i = 0; i<ARRAY_LEN(formats); i++) {
char *p;
- if (!strchr(formats[i], 'Y')) {
- localtime_r(&now, tm);
- tm->tm_hour = tm->tm_min = tm->tm_sec = 0;
- }
+ now = time(0);
+ localtime_r(&now, tm);
+ tm->tm_hour = tm->tm_min = tm->tm_sec = 0;
if ((p = strptime(str, formats[i], tm)) && !*p) return 0;
}
- memset(tm, 0, sizeof(struct tm));
// Posix format?
sscanf(str, "%2u%2u%2u%2u%n", &tm->tm_mon, &tm->tm_mday, &tm->tm_hour,
@@ -141,6 +138,11 @@ static int parse_default(char *str, struct tm *tm)
str += len;
} else tm->tm_sec = 0;
+ // Fix up weekday
+ now = mktime(tm);
+ localtime_r(&now, tm);
+
+ // shouldn't be any trailing garbage
return *str;
}