From 08444962ae632f9bc31636dd8f5eb0a6aab291fd Mon Sep 17 00:00:00 2001 From: Clemens Zeidler Date: Thu, 31 Mar 2011 04:55:08 +0000 Subject: [PATCH] Interpret a year like 10 as 2010 and not as 1910 if the now time is near 2010. Add test case for that. Should fix #7257. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@41146 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/system/libroot/os/parsedate.cpp | 26 +++++++++++++++++-- src/tests/system/libroot/os/ParseDateTest.cpp | 1 + 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/system/libroot/os/parsedate.cpp b/src/system/libroot/os/parsedate.cpp index 71f8aa6407..ef3c8703ff 100644 --- a/src/system/libroot/os/parsedate.cpp +++ b/src/system/libroot/os/parsedate.cpp @@ -766,11 +766,12 @@ computeDate(const char* format, bool* optional, parsed_element* elements, if (now == -1) now = time(NULL); + int nowYear = -1; if (dateMask.IsComplete()) memset(&tm, 0, sizeof(tm)); else { localtime_r(&now, &tm); - + nowYear = tm.tm_year; if (dateMask.HasTime()) { tm.tm_min = 0; tm.tm_sec = 0; @@ -820,10 +821,31 @@ computeDate(const char* format, bool* optional, parsed_element* elements, break; case 'y': case 'Y': + { + if (nowYear < 0) { + struct tm tmNow; + localtime_r(&now, &tmNow); + nowYear = tmNow.tm_year; + } + int nowYearInCentury = nowYear % 100; + int nowCentury = 1900 + nowYear - nowYearInCentury; + tm.tm_year = element->value; - if (tm.tm_year > 1900) + if (tm.tm_year < 1900) { + // just a relative year like 11 (2011) + + // interpret something like 50 as 1950 but + // something like 11 as 2011 (assuming now is 2011) + if (nowYearInCentury + 10 < tm.tm_year % 100) + tm.tm_year -= 100; + + tm.tm_year += nowCentury - 1900; + } + else { tm.tm_year -= 1900; + } break; + } case 'z': // time zone case 'Z': { diff --git a/src/tests/system/libroot/os/ParseDateTest.cpp b/src/tests/system/libroot/os/ParseDateTest.cpp index 723508c425..7b083aaa4d 100644 --- a/src/tests/system/libroot/os/ParseDateTest.cpp +++ b/src/tests/system/libroot/os/ParseDateTest.cpp @@ -86,6 +86,7 @@ main(int argc, char** argv) {739702803, "Mon, June 10th, 1993 10:00:03 am +0100", ABSOLUTE, true, false}, {739731603, "Mon, June 10th, 1993 10:00:03 am -0700", ABSOLUTE, true, false}, {739654203, "Mon, June 10th, 1993 06:00:03 am ACDT", ABSOLUTE, true, false}, + {1291204800, "01 Dec 10 12:00", ABSOLUTE, false, true}, {-1, NULL, 0, false} };