avr-libc-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-libc-commit] [2321] Code rearrangements and comment edits to make a


From: Mike Rice
Subject: [avr-libc-commit] [2321] Code rearrangements and comment edits to make algorithms more clear.
Date: Tue, 02 Apr 2013 00:12:49 +0000

Revision: 2321
          http://svn.sv.gnu.org/viewvc/?view=rev&root=avr-libc&revision=2321
Author:   swfltek
Date:     2013-04-02 00:12:48 +0000 (Tue, 02 Apr 2013)
Log Message:
-----------
Code rearrangements and comment edits to make algorithms more clear.

Modified Paths:
--------------
    trunk/avr-libc/libc/time/asc_store.c
    trunk/avr-libc/libc/time/gmtime_r.c
    trunk/avr-libc/libc/time/mk_gmtime.c
    trunk/avr-libc/libc/time/tm_store.c
    trunk/avr-libc/libc/time/utc_offset.c
    trunk/avr-libc/libc/time/week_of_month.c
    trunk/avr-libc/libc/time/week_of_year.c

Property Changed:
----------------
    trunk/avr-libc/libc/time/Files.am
    trunk/avr-libc/libc/time/Makefile.am
    trunk/avr-libc/libc/time/Rules.am
    trunk/avr-libc/libc/time/system_time.c


Property changes on: trunk/avr-libc/libc/time/Files.am
___________________________________________________________________
Added: svn:keywords
   + ID


Property changes on: trunk/avr-libc/libc/time/Makefile.am
___________________________________________________________________
Added: svn:keywords
   + ID


Property changes on: trunk/avr-libc/libc/time/Rules.am
___________________________________________________________________
Added: svn:keywords
   + ID

Modified: trunk/avr-libc/libc/time/asc_store.c
===================================================================
--- trunk/avr-libc/libc/time/asc_store.c        2013-03-31 22:22:09 UTC (rev 
2320)
+++ trunk/avr-libc/libc/time/asc_store.c        2013-04-02 00:12:48 UTC (rev 
2321)
@@ -1,10 +1,10 @@
 /*
  * (c)2012 Michael Duane Rice All rights reserved.
- * 
+ *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
  * met:
- * 
+ *
  * Redistributions of source code must retain the above copyright notice, this
  * list of conditions and the following disclaimer. Redistributions in binary
  * form must reproduce the above copyright notice, this list of conditions
@@ -12,7 +12,7 @@
  * provided with the distribution. Neither the name of the copyright holders
  * nor the names of contributors may be used to endorse or promote products
  * derived from this software without specific prior written permission.
- * 
+ *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE

Modified: trunk/avr-libc/libc/time/gmtime_r.c
===================================================================
--- trunk/avr-libc/libc/time/gmtime_r.c 2013-03-31 22:22:09 UTC (rev 2320)
+++ trunk/avr-libc/libc/time/gmtime_r.c 2013-04-02 00:12:48 UTC (rev 2321)
@@ -29,7 +29,7 @@
 /* $Id$ */
 
 /*
-       Re entrant version of gmtime(), this function converts 'binary' time 
into 'calendar' time.
+    Re entrant version of gmtime(), this function 'breaks down' a Y2K time 
stamp .
 */
 
 #include <time.h>
@@ -39,98 +39,88 @@
 void
 gmtime_r(const time_t * timer, struct tm * timeptr)
 {
-       int32_t         t;
+       int32_t         fract;
        ldiv_t          lresult;
        div_t           result;
-       uint16_t        days, w;
-       int16_t         n, i, d, ly, years;
+       uint16_t        days, n, leapyear, years;
 
-       /* This first division must be done the long way */
+       /* break down timer into whole and fractional parts of 1 day */
        days = *timer / 86400UL;
-       t = *timer % 86400UL;
+       fract = *timer % 86400UL;
 
-       /* Epoch was a Saturday */
-       w = days + 6;
-       n = w % 7;
+       /* Determine day of week ( the epoch was a Saturday ) */
+       n = days + SATURDAY;
+       n %= 7;
        timeptr->tm_wday = n;
 
-       /* extract second, minute, and hour */
-       lresult = ldiv(t, 60L);
-       timeptr->tm_sec = lresult.rem;
-
-       result = div(lresult.quot, 60);
-       timeptr->tm_min = result.rem;
-       timeptr->tm_hour = result.quot;
-
-       /* map our place in the 100 and 4 year leap cycles. */
+       /* map our place into the 100 year and 4 year leap cycles. */
        lresult = ldiv((long) days, 36525L);
        years = 100 * lresult.quot;
-
        lresult = ldiv(lresult.rem, 1461L);
        years += 4 * lresult.quot;
-       d = lresult.rem;
+       days = lresult.rem;
+       if (years > 100)
+               days++;
 
        /*
-        * 'd' is number of days into current 4 year cycle, 0 to 1460. years
-        * is evenly divisible by 4, and WILL be a leap year, unless it
-        * equals 100
-        */
-
-       ly = 1;
+         * 'years' is now at a 4 year boundary
+         * 'days' is an index into the current 1461 day leap cycle
+         * If 'years' != 100, this cycle begins with a 366 day year.
+         */
+       leapyear = 1;
        if (years == 100)
-               ly = 0;
+               leapyear = 0;
 
-       n = 364 + ly;
+       n = 364 + leapyear;
 
-       if (d > n) {
-               d -= ly;
-               ly = 0;
-               result = div(d, 365);
+       /*
+            Given the length of the first year of this cycle,
+            we can proceed to break down year and day of year.
+        */
+       if (days > n) {
+               days -= leapyear;
+               leapyear = 0;
+               result = div(days, 365);
                years += result.quot;
-               d = result.rem;
+               days = result.rem;
        }
        timeptr->tm_year = 100 + years;
-       timeptr->tm_yday = d;
+       timeptr->tm_yday = days;
 
-       /* special case January */
-       if (d < 31) {
-               timeptr->tm_mon = 0;
-               timeptr->tm_mday = d;
-       }
-       /* special case February */
-       else {
-               d -= 31;
-               n = 28;
-               if (ly)
-                       n = 29;
-               if (d < n) {
-                       timeptr->tm_mon = 1;
-                       timeptr->tm_mday = d;
-               } else {
+       /*
+            Given the year, day of year, and leap year indicator, we can break 
down the
+            month and day of month.
+        */
+       n = 59 + leapyear;
 
-                       d = d - n + 30;
-                       result = div(d, 61);
-                       i = result.quot;
-                       d = result.rem;
-
-                       n = 30;
-                       if (i > 2)
-                               n = 31;
-
-                       i *= 2;
-                       if (d >= n) {
-                               i++;
-                               d -= n;
-                       }
-                       timeptr->tm_mon = i + 1;
-                       timeptr->tm_mday = d;
-               }
+       if (days < n) {
+               result = div(days, 31);
+               timeptr->tm_mon = result.quot;
+               timeptr->tm_mday = result.rem;
+       } else {
+               days -= n;
+               result = div(days, 153);
+               timeptr->tm_mon = 2 + result.quot * 5;
+               result = div(result.rem, 61);
+               timeptr->tm_mon += result.quot * 2;
+               result = div(result.rem, 31);
+               timeptr->tm_mon += result.quot;
+               timeptr->tm_mday = result.rem;
        }
 
-       /* tm_mday is 1 based */
-       timeptr->tm_mday++;
+       /*
+            Break down hour, minute, and second from the fractional day
+        */
+       lresult = ldiv(fract, 60L);
+       timeptr->tm_sec = lresult.rem;
+       result = div(lresult.quot, 60);
+       timeptr->tm_min = result.rem;
+       timeptr->tm_hour = result.quot;
 
-       /* gmt is never in DST */
-       timeptr->tm_isdst = 0;
+       /*
+            Cleanup and return
+        */
+       timeptr->tm_isdst = 0;  /* gmt is never in DST */
+       timeptr->tm_mday++;     /* tm_mday is 1 based */
 
 }

Modified: trunk/avr-libc/libc/time/mk_gmtime.c
===================================================================
--- trunk/avr-libc/libc/time/mk_gmtime.c        2013-03-31 22:22:09 UTC (rev 
2320)
+++ trunk/avr-libc/libc/time/mk_gmtime.c        2013-04-02 00:12:48 UTC (rev 
2321)
@@ -29,10 +29,10 @@
 /* $Id$ */
 
 /*
-       Basically the inverse of gmtime_r(). We assume the values in timeptr 
represent
-       UTC, thus time zone and DST do not apply.
-       
-       Unlike mktime(), we do not attempt to 'normalize' timeptr... saves some 
cpu cycles.
+    Basically the inverse of gmtime_r(). We assume the values in timeptr 
represent
+    UTC, thus time zone and DST do not apply.
+
+    Unlike mktime(), we do not attempt to 'normalize' timeptr... saves some 
cpu cycles.
 */
 
 #include <time.h>
@@ -45,10 +45,10 @@
        long            tmp;
        int             n, m, d, leaps;
 
-       /* n = years since epoch */
+       /*
+            Determine elapsed whole days, since Y2K, to the beginning of the 
year
+        */
        n = timeptr->tm_year - 100;
-
-       /* compute leap days prior to timeptr */
        leaps = 0;
        if (n) {
                m = n - 1;
@@ -56,47 +56,41 @@
                leaps -= m / 100;
                leaps++;
        }
-       /* compute days elapsed since EPOCH to the beginning of the year */
        tmp = 365L * n + leaps;
 
-       m = timeptr->tm_mday - 1;
-       d = 0;
+       /*
+            Derive the day of year from month and day of month
+        */
+       d = timeptr->tm_mday - 1;       /* tm_mday is one based */
 
-       /* Special case January */
-       if (timeptr->tm_mon == 0) {
-               d += m;
-       }
-       /* Special case February */
-       else if (timeptr->tm_mon == 1) {
-               n = m + 31;
-               d += n;
+       if (timeptr->tm_mon < 2) {
+               if (timeptr->tm_mon)
+                       d += 31;
+
        } else {
-               d += 29;
-               if (is_leap_year(timeptr->tm_year + 1900))
-                       d++;
+               n = 59 + is_leap_year(timeptr->tm_year + 1900);
+               d += n;
 
-               m = timeptr->tm_mon - 1;
+               /* other months exhibit a regular pattern */
+               n = timeptr->tm_mon - 2;
+               if (n > 4)
+                       d += 153;
 
-               n = m / 2;
-               m -= n * 2;
+               n %= 5;
+               m = n / 2;
+               m *= 61;
+               d += m;
 
-               n *= 61;
-               d += n;
-
-               if (m) {
-                       d += 30;
-                       if (timeptr->tm_mon > 6)
-                               d++;
-               }
-               d += timeptr->tm_mday - 1;
+               n &= 1;
+               if (n)
+                       d += 31;
        }
 
+       /* add day of year to elapsed days, and convert to seconds */
        tmp += d;
-
-       /* convert days into seconds */
        ret = 86400LL * tmp;
 
-       /* add time of day */
+       /* compute 'fractional' day */
        tmp = 3600L * timeptr->tm_hour;
        tmp += 60L * timeptr->tm_min;
        tmp += timeptr->tm_sec;


Property changes on: trunk/avr-libc/libc/time/system_time.c
___________________________________________________________________
Added: svn:keywords
   + ID

Modified: trunk/avr-libc/libc/time/tm_store.c
===================================================================
--- trunk/avr-libc/libc/time/tm_store.c 2013-03-31 22:22:09 UTC (rev 2320)
+++ trunk/avr-libc/libc/time/tm_store.c 2013-04-02 00:12:48 UTC (rev 2321)
@@ -1,10 +1,10 @@
 /*
  * (c)2012 Michael Duane Rice All rights reserved.
- * 
+ *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
  * met:
- * 
+ *
  * Redistributions of source code must retain the above copyright notice, this
  * list of conditions and the following disclaimer. Redistributions in binary
  * form must reproduce the above copyright notice, this list of conditions
@@ -12,7 +12,7 @@
  * provided with the distribution. Neither the name of the copyright holders
  * nor the names of contributors may be used to endorse or promote products
  * derived from this software without specific prior written permission.
- * 
+ *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE

Modified: trunk/avr-libc/libc/time/utc_offset.c
===================================================================
--- trunk/avr-libc/libc/time/utc_offset.c       2013-03-31 22:22:09 UTC (rev 
2320)
+++ trunk/avr-libc/libc/time/utc_offset.c       2013-04-02 00:12:48 UTC (rev 
2321)
@@ -1,10 +1,10 @@
 /*
  * (c)2012 Michael Duane Rice All rights reserved.
- * 
+ *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
  * met:
- * 
+ *
  * Redistributions of source code must retain the above copyright notice, this
  * list of conditions and the following disclaimer. Redistributions in binary
  * form must reproduce the above copyright notice, this list of conditions
@@ -12,7 +12,7 @@
  * provided with the distribution. Neither the name of the copyright holders
  * nor the names of contributors may be used to endorse or promote products
  * derived from this software without specific prior written permission.
- * 
+ *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE

Modified: trunk/avr-libc/libc/time/week_of_month.c
===================================================================
--- trunk/avr-libc/libc/time/week_of_month.c    2013-03-31 22:22:09 UTC (rev 
2320)
+++ trunk/avr-libc/libc/time/week_of_month.c    2013-04-02 00:12:48 UTC (rev 
2321)
@@ -29,9 +29,9 @@
 /* $Id$ */
 
 /*
-       Return the week of month, where 'base' represents the day of week 
considered as
-       the beginning day. In the USA, the week is generally considered to be 
Sunday
-       (base = 0), while in Europe it is generally considered to be Monday 
(base = 1).
+       Return the week of month, where 'base' represents the starting day.
+       In the USA, the week is generally considered to start on Sunday (base = 
0),
+       while in Europe it is generally considered to be Monday (base = 1).
 
        Return value ranges from 0 to 5.
 */

Modified: trunk/avr-libc/libc/time/week_of_year.c
===================================================================
--- trunk/avr-libc/libc/time/week_of_year.c     2013-03-31 22:22:09 UTC (rev 
2320)
+++ trunk/avr-libc/libc/time/week_of_year.c     2013-04-02 00:12:48 UTC (rev 
2321)
@@ -29,11 +29,11 @@
 /* $Id$ */
 
 /*
-       Return the week of year, where 'base' represents the day of week 
considered as
-       the beginning day. In the USA, the week is generally considered to be 
Sunday
-       (base = 0), while in Europe it is generally considered to be Monday 
(base = 1).
+       Return the week of year, where 'base' represents the first day of the 
week.
+       In the USA, the week is generally considered to start on Sunday (base = 
0),
+       while in Europe it is generally considered to be Monday (base = 1).
 
-       Return value ranges from 0 to 5.
+       Return value ranges from 0 to 52.
 */
 
 #include <time.h>




reply via email to

[Prev in Thread] Current Thread [Next in Thread]