aboutsummaryrefslogtreecommitdiff
path: root/toys/posix
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2015-11-10 18:21:51 -0800
committerRob Landley <rob@landley.net>2015-11-12 21:26:56 -0600
commitd885b528f2d26435ecff83da9e50e815ac73605b (patch)
tree848304ecf9db6e945195fea21c4c97df84fcf20c /toys/posix
parent26ec1c05b0eef896b59fe3c870f6885fa7edf4ae (diff)
downloadtoybox-d885b528f2d26435ecff83da9e50e815ac73605b.tar.gz
Fix year parsing in date(1).
Four-digit years were being mangled by the code for two-digit years. Move all the two-digit year code into the "we only saw two digits" case. Add some new tests and fix existing tests.
Diffstat (limited to 'toys/posix')
-rw-r--r--toys/posix/date.c10
1 files changed, 6 insertions, 4 deletions
diff --git a/toys/posix/date.c b/toys/posix/date.c
index a42de502..398b8915 100644
--- a/toys/posix/date.c
+++ b/toys/posix/date.c
@@ -131,12 +131,11 @@ static int parse_default(char *str, struct tm *tm)
// If year specified, overwrite one we fetched earlier
if (*str && *str != '.') {
- unsigned year, r1 = tm->tm_year % 100, r2 = (tm->tm_year + 50) % 100,
- century = tm->tm_year - r1;
+ unsigned year;
len = 0;
sscanf(str, "%u%n", &year, &len);
- if (len == 4) year -= 1900;
+ if (len == 4) tm->tm_year = year - 1900;
else if (len != 2) return 1;
str += len;
@@ -144,11 +143,14 @@ static int parse_default(char *str, struct tm *tm)
// A "future" date in past is a century ahead.
// A non-future date in the future is a century behind.
if (len == 2) {
+ unsigned r1 = tm->tm_year % 100, r2 = (tm->tm_year + 50) % 100,
+ century = tm->tm_year - r1;
+
if ((r1 < r2) ? (r1 < year && year < r2) : (year < r1 || year > r2)) {
if (year < r1) year += 100;
} else if (year > r1) year -= 100;
+ tm->tm_year = year + century;
}
- tm->tm_year = year + century;
}
if (*str == '.') {
len = 0;