commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r809150 - in /commons/sandbox/runtime/trunk/src/main/native/os: unix/time.c win32/time.c
Date Sat, 29 Aug 2009 16:00:30 GMT
Author: mturk
Date: Sat Aug 29 16:00:30 2009
New Revision: 809150

URL: http://svn.apache.org/viewvc?rev=809150&view=rev
Log:
Fix dos to unix time conversion

Modified:
    commons/sandbox/runtime/trunk/src/main/native/os/unix/time.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/time.c

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/time.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/time.c?rev=809150&r1=809149&r2=809150&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/time.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/time.c Sat Aug 29 16:00:30 2009
@@ -38,10 +38,10 @@
         year--;
 
     /* Find number of days since 1st March 1900 (in the Gregorian calendar). */
-    days = year * 365 + year / 4 - year / 100 + (year / 100 + 3) / 4;
+    days  = year * 365 + year / 4 - year / 100 + (year / 100 + 3) / 4;
     days += dayoffset[tm->tm_mon] + tm->tm_mday - 1;
     days -= 25508;              /* 1 jan 1970 is 25508 days since 1 mar 1900 */
-    days = ((days * 24 + tm->tm_hour) * 60 + tm->tm_min) * 60 + tm->tm_sec;
+    days  = ((days * 24 + tm->tm_hour) * 60 + tm->tm_min) * 60 + tm->tm_sec;
 
     if (days < 0) {
         return 0;
@@ -60,14 +60,13 @@
 ACR_DECLARE(acr_time_t) ACR_Dos2AcrTime(acr_uint32_t t)
 {
     struct tm    tm;
-    acr_uint32_t ud = (t >> 16);
 
-    tm.tm_mday = ud & 0x1F;
-    tm.tm_mon  = (acr_uint32_t)(((ud & 0x01E0) / 0x0020) - 1);
-    tm.tm_year = (acr_uint32_t)(((ud & 0xFE00) / 0x0200) + 80);
-    tm.tm_hour = (acr_uint32_t)((t & 0xF800)   / 0x0800);
-    tm.tm_min  = (acr_uint32_t)((t & 0x07E0)   / 0x0020);
-    tm.tm_sec  = (acr_uint32_t)((t & 0x001F) << 1);
+    tm.tm_year = ((t & 0xFE000000) >> 25) + 80;
+    tm.tm_mon  = ((t & 0x01E00000) >> 21) - 1;
+    tm.tm_mday = ((t & 0x001F0000) >> 16);
+    tm.tm_hour = ((t & 0x0000F800) >> 11);
+    tm.tm_min  = ((t & 0x000007E0) >>  5);
+    tm.tm_sec  = ((t & 0x0000001F) <<  1);
 
     return tm2time(&tm);
 }
@@ -76,18 +75,26 @@
 {
     struct tm    tm;
     acr_uint32_t rv;
-    time_t       tt = t / ACR_USEC_PER_SEC;
+    time_t       tt = (time_t)ACR_ALIGN((t / ACR_USEC_PER_SEC), 2);
 
 #if 1
     gmtime_r(&tt, &tm);
 #else
     tm = *gmtime(&tt);
 #endif
-    if (tm.tm_year > 1980)
-        tm.tm_year -= 1980;
-    else if (tm.tm_year > 80)
+    if (tm.tm_year > 1900)
+        tm.tm_year -= 1900;
+    if (tm.tm_year < 80) {
+        /* January 1st 1980 */
+        return 0x00210000;
+    }
+    else
         tm.tm_year -= 80;
-    rv = (((tm.tm_mday) + (32 * (tm.tm_mon+1)) + (512 * tm.tm_year)) << 16) |
-         ((tm.tm_sec / 2) + (32 * tm.tm_min) + (2048 * tm.tm_hour));
+    rv = ((acr_uint32_t)(tm.tm_year) << 25) |
+         ((acr_uint32_t)(tm.tm_mon + 1) << 21) |
+         ((acr_uint32_t)(tm.tm_mday) << 16) |
+         ((acr_uint32_t)(tm.tm_hour) << 11) |
+         ((acr_uint32_t)(tm.tm_min) << 5) |
+         ((acr_uint32_t)(tm.tm_sec) >> 1);
     return rv;
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/time.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/time.c?rev=809150&r1=809149&r2=809150&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/time.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/time.c Sat Aug 29 16:00:30 2009
@@ -23,60 +23,6 @@
 #include "acr_descriptor.h"
 #include "acr_time.h"
 
-/* Leap year is any year divisible by four, but not by 100 unless also
- * divisible by 400
- */
-#define IsLeapYear(y) ((!(y % 4)) ? (((!(y % 400)) && (y % 100)) ? 1 : 0) : 0)
-
-static acr_time_t tm2time(struct tm *tm)
-{
-    acr_time_t year = tm->tm_year;
-    acr_time_t days;
-    static const int dayoffset[12] =
-    { 306, 337, 0, 31, 61, 92, 122, 153, 184, 214, 245, 275 };
-
-    /* shift new year to 1st March in order to make leap year calc easy */
-
-    if (tm->tm_mon < 2)
-        year--;
-
-    /* Find number of days since 1st March 1900 (in the Gregorian calendar). */
-    days = year * 365 + year / 4 - year / 100 + (year / 100 + 3) / 4;
-    days += dayoffset[tm->tm_mon] + tm->tm_mday - 1;
-    days -= 25508;              /* 1 jan 1970 is 25508 days since 1 mar 1900 */
-    days = ((days * 24 + tm->tm_hour) * 60 + tm->tm_min) * 60 + tm->tm_sec;
-
-    if (days < 0) {
-        return 0;
-    }
-    return days * ACR_USEC_PER_SEC;
-}
-
-static void SystemTimeToTmTime(struct tm *tm, SYSTEMTIME *st)
-{
-    static const int dayoffset[12] =
-    { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
-
-    /* Note; the caller is responsible for filling in detailed tm_usec,
-     * tm_gmtoff and tm_isdst data when applicable.
-     */
-    tm->tm_sec  = st->wSecond;
-    tm->tm_min  = st->wMinute;
-    tm->tm_hour = st->wHour;
-    tm->tm_mday = st->wDay;
-    tm->tm_mon  = st->wMonth - 1;
-    tm->tm_year = st->wYear - 1900;
-    tm->tm_wday = st->wDayOfWeek;
-    tm->tm_yday = dayoffset[tm->tm_mon] + (st->wDay - 1);
-    tm->tm_isdst = 0;
-
-    /* If this is a leap year, and we're past the 28th of Feb. (the
-     * 58th day after Jan. 1), we'll increment our tm_yday by one.
-     */
-    if (IsLeapYear(st->wYear) && (tm->tm_yday > 58))
-        tm->tm_yday++;
-}
-
 ACR_DECLARE(acr_time_t) ACR_TimeNow(void)
 {
     LONGLONG   aprtime = 0;
@@ -88,22 +34,6 @@
     return aprtime;
 }
 
-#if 0
-ACR_DECLARE(acr_time_t) ACR_Dos2AcrTime(acr_uint32_t t)
-{
-    struct tm    tm;
-    acr_uint32_t ud = (t >> 16);
-
-    tm.tm_mday = ud & 0x1F;
-    tm.tm_mon  = (acr_uint32_t)(((ud & 0x01E0) / 0x0020) - 1);
-    tm.tm_year = (acr_uint32_t)(((ud & 0xFE00) / 0x0200) + 80);
-    tm.tm_hour = (acr_uint32_t)((t & 0xF800)   / 0x0800);
-    tm.tm_min  = (acr_uint32_t)((t & 0x07E0)   / 0x0020);
-    tm.tm_sec  = (acr_uint32_t)((t & 0x001F) << 1);
-
-    return tm2time(&tm);
-}
-#else
 ACR_DECLARE(acr_time_t) ACR_Dos2AcrTime(acr_uint32_t t)
 {
     FILETIME ft;
@@ -120,43 +50,17 @@
 }
 #endif
 
-#if 0
-ACR_DECLARE(acr_uint32_t) ACR_Acr2DosTime(acr_time_t t)
-{
-    FILETIME     ft;
-    SYSTEMTIME   st;
-    struct tm    tm;
-    acr_uint32_t rv;
-
-    /* Round up to seconds */
-    t = t / ACR_USEC_PER_SEC;
-    UsecTimeToFileTime(&ft, t * ACR_USEC_PER_SEC);
-    FileTimeToSystemTime(&ft, &st);
-    /* The Platform SDK documents that SYSTEMTIME/FILETIME are
-     * generally UTC, so no timezone info needed
-     */
-    SystemTimeToTmTime(&tm, &st);
-    if (tm.tm_year > 1980)
-        tm.tm_year -= 1980;
-    else if (tm.tm_year > 80)
-        tm.tm_year -= 80;
-    rv = (((tm.tm_mday) + (32 * (tm.tm_mon+1)) + (512 * tm.tm_year)) << 16) |
-         ((tm.tm_sec / 2) + (32 * tm.tm_min) + (2048 * tm.tm_hour));
-    return rv;
-}
-#else
 ACR_DECLARE(acr_uint32_t) ACR_Acr2DosTime(acr_time_t t)
 {
     FILETIME     ft;
     WORD         dd;
     WORD         dt;
 
-    /* Round up to seconds */
-    t = t / ACR_USEC_PER_SEC;
+    /* Round up to two seconds */
+    t = ACR_ALIGN((t / ACR_USEC_PER_SEC), 2);
     UsecTimeToFileTime(&ft, t * ACR_USEC_PER_SEC);
     if (FileTimeToDosDateTime(&ft, &dd, &dt))
         return ((acr_uint32_t)dd << 16) | (acr_uint32_t)dt;
     else
         return 0;
 }
-#endif
\ No newline at end of file



Mime
View raw message