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

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

[avr-libc-commit] [2362] Just noticed the 'week of' functions were total


From: Mike Rice
Subject: [avr-libc-commit] [2362] Just noticed the 'week of' functions were totally wrong, except for Sunday based weeks.
Date: Fri, 26 Apr 2013 22:22:47 +0000

Revision: 2362
          http://svn.sv.gnu.org/viewvc/?view=rev&root=avr-libc&revision=2362
Author:   swfltek
Date:     2013-04-26 22:22:46 +0000 (Fri, 26 Apr 2013)
Log Message:
-----------
Just noticed the 'week of' functions were totally wrong, except for Sunday 
based weeks. I could use more eyes on this, as I am preparing to do battle 
(again)with strftime() to make it fully C90 compliant. That will leave 
'difftime()' as the only departure from the standard, i think!

Modified Paths:
--------------
    trunk/avr-libc/libc/time/week_of_month.c
    trunk/avr-libc/libc/time/week_of_year.c

Modified: trunk/avr-libc/libc/time/week_of_month.c
===================================================================
--- trunk/avr-libc/libc/time/week_of_month.c    2013-04-25 11:29:43 UTC (rev 
2361)
+++ trunk/avr-libc/libc/time/week_of_month.c    2013-04-26 22:22:46 UTC (rev 
2362)
@@ -39,20 +39,24 @@
 #include <time.h>
 
 uint8_t
-week_of_month(const struct tm * calendar, uint8_t base)
+week_of_month(const struct tm * timestruct, uint8_t base)
 {
-       int             w;
+       int             first, n;
 
        /* zero base the day of month */
-       w = calendar->tm_mday - 1;
+       n = timestruct->tm_mday - 1;
 
-       /* find most recent base day */
-       w = w - calendar->tm_wday + base;
+       /* find the first base day of the month (start of week 1) */
+       first = 7 + n - timestruct->tm_wday + base;
+       first %= 7;
 
+       /* find days since the first week began */
+       n = n - first;
+
        /* if negative, we are in week 0 */
-       if (w < 0)
+       if (n < 0)
                return 0;
 
-       return w / 7 + 1;
+       return n / 7 + 1;
 
 }

Modified: trunk/avr-libc/libc/time/week_of_year.c
===================================================================
--- trunk/avr-libc/libc/time/week_of_year.c     2013-04-25 11:29:43 UTC (rev 
2361)
+++ trunk/avr-libc/libc/time/week_of_year.c     2013-04-26 22:22:46 UTC (rev 
2362)
@@ -39,16 +39,20 @@
 #include <time.h>
 
 uint8_t
-week_of_year(const struct tm * timeptr, uint8_t base)
+week_of_year(const struct tm * timestruct, uint8_t base)
 {
-       int             w;
+       int             first, n;
 
-       /* find most recent base day */
-       w = timeptr->tm_yday - timeptr->tm_wday + base;
+       /* find the first base day of the year (start of week 1) */
+       first = 7 + timestruct->tm_yday - timestruct->tm_wday + base;
+    first %= 7;
 
+       /* find days since that first base day*/
+       n = timestruct->tm_yday - first;
+
        /* if negative, we are in week 0 */
-       if (w < 0)
+       if (n < 0)
                return 0;
 
-       return w / 7 + 1;
+       return n / 7 + 1;
 }




reply via email to

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