You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apr.apache.org by cm...@collab.net on 2001/02/01 23:42:17 UTC

[patch] Time fixes for Win32

Some fixes in the Win32 time support.

* (IsLeapYear): New macro for quickly figgerin' out if a given year is a 
  leap year.

* (SystemTimeToAprExpTime): Perform the calculation of tm_yday.  Also, 
  negate the sign of the tm_gmtoff field to be consistent with Unix
  platforms and APR header file comments.



Index: time/win32/time.c
===================================================================
RCS file: /home/cvspublic/apr/time/win32/time.c,v
retrieving revision 1.16
diff -u -r1.16 time.c
--- time/win32/time.c	2001/01/18 20:07:38	1.16
+++ time/win32/time.c	2001/02/01 22:34:18
@@ -77,6 +77,7 @@
     *result -= APR_DELTA_EPOCH_IN_USEC;  /* Convert from Windows epoch to Unix epoch */
     return;
 }
+
 void AprTimeToFileTime(LPFILETIME pft, apr_time_t t)
 {
     LONGLONG ll;
@@ -87,10 +88,17 @@
     return;
 }
 
+/* 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)
+
 void SystemTimeToAprExpTime(apr_exploded_time_t *xt, SYSTEMTIME *tm)
 {
     TIME_ZONE_INFORMATION tz;
     DWORD rc;
+    static const int dayoffset[12] =
+    {0, 31, 59, 90, 120, 151, 182, 212, 243, 273, 304, 334};
 
     xt->tm_usec = tm->wMilliseconds * 1000;
     xt->tm_sec  = tm->wSecond;
@@ -100,7 +108,13 @@
     xt->tm_mon  = tm->wMonth - 1;
     xt->tm_year = tm->wYear - 1900;
     xt->tm_wday = tm->wDayOfWeek;
-    xt->tm_yday = 0; /* ToDo: need to compute this */
+    xt->tm_yday = dayoffset[xt->tm_mon] + (tm->wDay - 1);
+
+    /* 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(tm->wYear) && (xt->tm_yday > 58))
+        xt->tm_yday++;
 
     rc = GetTimeZoneInformation(&tz);
     switch (rc) {
@@ -110,11 +124,11 @@
         /* Bias = UTC - local time in minutes 
          * tm_gmtoff is seconds east of UTC
          */
-        xt->tm_gmtoff = tz.Bias * 60;
+        xt->tm_gmtoff = tz.Bias * -60;
         break;
     case TIME_ZONE_ID_DAYLIGHT:
         xt->tm_isdst = 1;
-        xt->tm_gmtoff = tz.Bias * 60;
+        xt->tm_gmtoff = tz.Bias * -60;
         break;
     default:
         xt->tm_isdst = 0;

Re: [patch] Time fixes for Win32

Posted by Karl Fogel <kf...@galois.collab.net>.
Ben Collins-Sussman <su...@newton.collab.net> writes:
> I think the the rule is:  every fourth year is a leap year, BUT, every
> century is not;  BUT;  every fourth century *is* a leap year.   We've
> got two levels of override here.

Yep, that's the rule as I've been taught it too.

Re: [patch] Time fixes for Win32

Posted by Ben Collins-Sussman <su...@newton.collab.net>.
Cliff Woolley <cl...@yahoo.com> writes:

> --- cmpilato@collab.net wrote:
> > +/* 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)
> > +
> 
> If y is divisible evenly by 400, it's automatically divisible by 100... you don't need to
> check both.  =-)

It's also divisible by 4, but that's not the point. :)

I think the the rule is:  every fourth year is a leap year, BUT, every
century is not;  BUT;  every fourth century *is* a leap year.   We've
got two levels of override here.

Re: [patch] Time fixes for Win32

Posted by Cliff Woolley <cl...@yahoo.com>.
--- Cliff Woolley <cl...@yahoo.com> wrote:

> If y is divisible evenly by 400, it's automatically divisible by 100... you don't need
> to check both.  =-)

That's what I get for talking before I think... I guess you actually DO have to check,
since you have to be sure that if it's not divisible by 400 that it's also not divisible
by 100.  Nevermind... my bad.  :)

--Cliff

__________________________________________________
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/

Re: [patch] Time fixes for Win32

Posted by Cliff Woolley <cl...@yahoo.com>.
--- cmpilato@collab.net wrote:
> +/* 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)
> +

If y is divisible evenly by 400, it's automatically divisible by 100... you don't need to
check both.  =-)

--Cliff

__________________________________________________
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/