summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2009-08-08 16:54:36 +0000
committerpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2009-08-08 16:54:36 +0000
commitaa7b20473b7e31fb08d53aeb197a861c1c036536 (patch)
tree3f41ed61d30bc983e96396e1e2d664a58b21e2bb
parent709de07c24959d66bff6a68bce320bc6a5f1244c (diff)
downloadnuttx-aa7b20473b7e31fb08d53aeb197a861c1c036536.tar.gz
nuttx-aa7b20473b7e31fb08d53aeb197a861c1c036536.tar.bz2
nuttx-aa7b20473b7e31fb08d53aeb197a861c1c036536.zip
Fix various errors in time manipulation routines
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@2015 42af7a65-404d-4744-a932-0658087f49c3
-rw-r--r--nuttx/lib/lib_daysbeforemonth.c2
-rw-r--r--nuttx/lib/lib_gmtimer.c32
-rw-r--r--nuttx/lib/lib_strftime.c2
3 files changed, 20 insertions, 16 deletions
diff --git a/nuttx/lib/lib_daysbeforemonth.c b/nuttx/lib/lib_daysbeforemonth.c
index e9c4d0026..718668bd1 100644
--- a/nuttx/lib/lib_daysbeforemonth.c
+++ b/nuttx/lib/lib_daysbeforemonth.c
@@ -1,7 +1,7 @@
/****************************************************************************
* lib/lib_daysbeforemonth.c
*
- * Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved.
+ * Copyright (C) 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
diff --git a/nuttx/lib/lib_gmtimer.c b/nuttx/lib/lib_gmtimer.c
index 03e1a2058..e7514fd1a 100644
--- a/nuttx/lib/lib_gmtimer.c
+++ b/nuttx/lib/lib_gmtimer.c
@@ -177,9 +177,9 @@ static void clock_utc2calendar(time_t days, int *year, int *month, int *day)
* following:
*/
- value = days / (4*365 + 1); /* Number of 4-years periods */
- days -= value * (4*365 + 1); /* Remaining days */
- value = 70 + (value << 2); /* 1970 plus the 4 year groups */
+ value = days / (4*365 + 1); /* Number of 4-years periods since the epoch*/
+ days -= value * (4*365 + 1); /* Remaining days */
+ value <<= 2; /* Years since the epoch */
/* Then we will brute force the next 0-3 years */
@@ -212,7 +212,7 @@ static void clock_utc2calendar(time_t days, int *year, int *month, int *day)
/* At this point, value has the year and days has number days into this year */
- *year = value;
+ *year = 1970 + value;
/* Handle the month (zero based) */
@@ -225,18 +225,20 @@ static void clock_utc2calendar(time_t days, int *year, int *month, int *day)
value = (min + max) >> 1;
- /* Get the number of days that occurred before the beginning month
+ /* Get the number of days that occurred before the beginning of the month
* following the midpoint.
*/
tmp = clock_daysbeforemonth(value + 1, leapyear);
- /* Does that equal or exceed the number of days we have remaining? */
+ /* Does the number of days before this month that equal or exceed the
+ * number of days we have remaining?
+ */
if (tmp >= days)
{
- /* Yes.. then the month we want is somewhere between 'min' and the.
- * midpoint, 'value'. Check if it is the midpoint.
+ /* Yes.. then the month we want is somewhere from 'min' and to the
+ * midpoint, 'value'. Could it be the midpoint?
*/
tmp = clock_daysbeforemonth(value, leapyear);
@@ -262,15 +264,17 @@ static void clock_utc2calendar(time_t days, int *year, int *month, int *day)
}
while (min < max);
- /* Subtract the number of days in the selected month */
+ /* The selected month number is in min (and max). Subtract the number of days in the
+ * selected month
+ */
- days -= clock_daysbeforemonth(value, leapyear);
+ days -= clock_daysbeforemonth(min, leapyear);
/* At this point, value has the month into this year (zero based) and days has
* number of days into this month (zero based)
*/
- *month = value; /* Zero based */
+ *month = min + 1; /* 1-based */
*day = days + 1; /* 1-based */
}
@@ -329,9 +333,9 @@ struct tm *gmtime_r(const time_t *clock, struct tm *result)
/* Then return the struct tm contents */
- result->tm_year = (int)year - 1900;
- result->tm_mon = (int)month - 1;
- result->tm_mday = (int)day;
+ result->tm_year = (int)year - 1900; /* Relative to 1900 */
+ result->tm_mon = (int)month - 1; /* zero-based */
+ result->tm_mday = (int)day; /* one-based */
result->tm_hour = (int)hour;
result->tm_min = (int)min;
result->tm_sec = (int)sec;
diff --git a/nuttx/lib/lib_strftime.c b/nuttx/lib/lib_strftime.c
index 008586973..7eed7d5a6 100644
--- a/nuttx/lib/lib_strftime.c
+++ b/nuttx/lib/lib_strftime.c
@@ -214,7 +214,7 @@ size_t strftime(char *s, size_t max, const char *format, const struct tm *tm)
case 'C':
{
- len = snprintf(dest, chleft, "%02d", tm->tm_year);
+ len = snprintf(dest, chleft, "%02d", tm->tm_year % 100);
}
break;