[haiku-commits] r41146 - in haiku/trunk/src: system/libroot/os tests/system/libroot/os

  • From: clemens.zeidler@xxxxxxxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Thu, 31 Mar 2011 06:55:08 +0200 (CEST)

Author: czeidler
Date: 2011-03-31 06:55:08 +0200 (Thu, 31 Mar 2011)
New Revision: 41146
Changeset: https://dev.haiku-os.org/changeset/41146
Ticket: https://dev.haiku-os.org/ticket/7257

Modified:
   haiku/trunk/src/system/libroot/os/parsedate.cpp
   haiku/trunk/src/tests/system/libroot/os/ParseDateTest.cpp
Log:
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.



Modified: haiku/trunk/src/system/libroot/os/parsedate.cpp
===================================================================
--- haiku/trunk/src/system/libroot/os/parsedate.cpp     2011-03-31 04:07:35 UTC 
(rev 41145)
+++ haiku/trunk/src/system/libroot/os/parsedate.cpp     2011-03-31 04:55:08 UTC 
(rev 41146)
@@ -766,11 +766,12 @@
        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 @@
                                                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':
                                        {

Modified: haiku/trunk/src/tests/system/libroot/os/ParseDateTest.cpp
===================================================================
--- haiku/trunk/src/tests/system/libroot/os/ParseDateTest.cpp   2011-03-31 
04:07:35 UTC (rev 41145)
+++ haiku/trunk/src/tests/system/libroot/os/ParseDateTest.cpp   2011-03-31 
04:55:08 UTC (rev 41146)
@@ -86,6 +86,7 @@
                {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}
        };
 


Other related posts:

  • » [haiku-commits] r41146 - in haiku/trunk/src: system/libroot/os tests/system/libroot/os - clemens . zeidler