Re: [PATCH] Fix date() bug
| From: | Ilia Cheishvili | Date: | Sat, 27 Sep 2008 08:26:58 +0000 |
| Subject: | Re: [PATCH] Fix date() bug | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-40732@lists.php.net to get a copy of this message | ||
Ah, I see. I like that even better :)
And I attached yet another patch that incorporates your idea.
Ilia
On Sat, Sep 27, 2008 at 2:20 AM, Alexey Zakhlestin <indeyets@gmail.com>wrote:
> On Sat, Sep 27, 2008 at 12:04 PM, Ilia Cheishvili
> <ilia.cheishvili@gmail.com> wrote:
> > It definitely would be, and that's actually the way I would have
> preferred
> > to do it. I didn't want to impact too much code, if that makes sense in
> > this case, but I'm glad that someone agrees :)
> > I have attached a patch to do exactly this.
>
> I was thinking other in another direction..
>
> case 'u':
> #ifdef HAVE_GETTIMEOFDAY
> gettimeofday(&tp, &tz);
> length = slprintf(buffer, 32, "%06d", (int) tp.tv_usec);
> #else
> length = slprintf(buffer, 32, "%06d", (int) floor(t->f * 1000000));
> #endif
> break;
>
>
> >
> > On Sat, Sep 27, 2008 at 1:40 AM, Alexey Zakhlestin <indeyets@gmail.com>
> > wrote:
> >>
> >> On Sat, Sep 27, 2008 at 11:04 AM, Ilia Cheishvili
> >> <ilia.cheishvili@gmail.com> wrote:
> >> > Hi all,
> >> > This patch addresses the issue with the date() function. When passing
> >> > in a
> >> > 'u', the date() function simply outputs six zeros. To fix this, I
> added
> >> > a
> >> > gettimeofday() call that figures out what to display for microseconds.
> >> > I am
> >> > including the headers and using the function with pre-processor
> >> > safeguards
> >> > as well.
> >> > Take a look :)
> >>
> >> Wouldn't it be better, to make gettimeofday() call only in case of 'u'?
> >>
> >>
> >> --
> >> Alexey Zakhlestin
> >> http://blog.milkfarmsoft.com/
> >
> >
>
>
>
> --
> Alexey Zakhlestin
> http://blog.milkfarmsoft.com/
>
Index: php_date.h =================================================================== RCS file: /repository/php-src/ext/date/php_date.h,v retrieving revision 1.17.2.11.2.3.2.9 diff -u -b -r1.17.2.11.2.3.2.9 php_date.h --- php_date.h 18 Jul 2008 14:33:53 -0000 1.17.2.11.2.3.2.9 +++ php_date.h 27 Sep 2008 08:25:02 -0000 @@ -128,4 +128,6 @@ PHPAPI void php_date_set_tzdb(timelib_tzdb *tzdb); PHPAPI timelib_tzinfo *get_timezone_info(TSRMLS_D); +#define MICROSECONDS_PER_SECOND 1000000 + #endif /* PHP_DATE_H */ Index: php_date.c =================================================================== RCS file: /repository/php-src/ext/date/php_date.c,v retrieving revision 1.43.2.45.2.51.2.54 diff -u -b -r1.43.2.45.2.51.2.54 php_date.c --- php_date.c 8 Aug 2008 22:07:07 -0000 1.43.2.45.2.51.2.54 +++ php_date.c 27 Sep 2008 08:25:02 -0000 @@ -30,6 +30,14 @@ #include "zend_interfaces.h" #include "lib/timelib.h" #include <time.h> +#ifdef PHP_WIN32 +#include "win32/time.h" +#elif defined(NETWARE) +#include <sys/timeval.h> +#include <sys/time.h> +#else +#include <sys/time.h> +#endif /* {{{ arginfo */ static @@ -1075,6 +1083,8 @@ timelib_time_offset *offset = NULL; timelib_sll isoweek, isoyear; int rfc_colon; + struct timeval tp = {0}; + struct timezone tz = {0}; if (!format_len) { return estrdup(""); @@ -1150,7 +1160,14 @@ case 'H': length = slprintf(buffer, 32, "%02d", (int) t->h); break; case 'i': length = slprintf(buffer, 32, "%02d", (int) t->i); break; case 's': length = slprintf(buffer, 32, "%02d", (int) t->s); break; - case 'u': length = slprintf(buffer, 32, "%06d", (int) floor(t->f * 1000000)); break; + case 'u': +#ifdef HAVE_GETTIMEOFDAY + gettimeofday(&tp, &tz); + length = slprintf(buffer, 32, "%06d", (int) tp.tv_usec); +#else + length = slprintf(buffer, 32, "%06d", (int) floor(t->f * MICROSECONDS_PER_SECOND)); +#endif + break; /* timezone */ case 'I': length = slprintf(buffer, 32, "%d", localtime ? offset->is_dst : 0); break;
Index: php_date.h =================================================================== RCS file: /repository/php-src/ext/date/php_date.h,v retrieving revision 1.17.2.11.2.3.2.9 diff -u -b -r1.17.2.11.2.3.2.9 php_date.h --- php_date.h 18 Jul 2008 14:33:53 -0000 1.17.2.11.2.3.2.9 +++ php_date.h 27 Sep 2008 08:25:02 -0000 @@ -128,4 +128,6 @@ PHPAPI void php_date_set_tzdb(timelib_tzdb *tzdb); PHPAPI timelib_tzinfo *get_timezone_info(TSRMLS_D); +#define MICROSECONDS_PER_SECOND 1000000 + #endif /* PHP_DATE_H */ Index: php_date.c =================================================================== RCS file: /repository/php-src/ext/date/php_date.c,v retrieving revision 1.43.2.45.2.51.2.54 diff -u -b -r1.43.2.45.2.51.2.54 php_date.c --- php_date.c 8 Aug 2008 22:07:07 -0000 1.43.2.45.2.51.2.54 +++ php_date.c 27 Sep 2008 08:25:02 -0000 @@ -30,6 +30,14 @@ #include "zend_interfaces.h" #include "lib/timelib.h" #include <time.h> +#ifdef PHP_WIN32 +#include "win32/time.h" +#elif defined(NETWARE) +#include <sys/timeval.h> +#include <sys/time.h> +#else +#include <sys/time.h> +#endif /* {{{ arginfo */ static @@ -1075,6 +1083,8 @@ timelib_time_offset *offset = NULL; timelib_sll isoweek, isoyear; int rfc_colon; + struct timeval tp = {0}; + struct timezone tz = {0}; if (!format_len) { return estrdup(""); @@ -1150,7 +1160,14 @@ case 'H': length = slprintf(buffer, 32, "%02d", (int) t->h); break; case 'i': length = slprintf(buffer, 32, "%02d", (int) t->i); break; case 's': length = slprintf(buffer, 32, "%02d", (int) t->s); break; - case 'u': length = slprintf(buffer, 32, "%06d", (int) floor(t->f * 1000000)); break; + case 'u': +#ifdef HAVE_GETTIMEOFDAY + gettimeofday(&tp, &tz); + length = slprintf(buffer, 32, "%06d", (int) tp.tv_usec); +#else + length = slprintf(buffer, 32, "%06d", (int) floor(t->f * MICROSECONDS_PER_SECOND)); +#endif + break; /* timezone */ case 'I': length = slprintf(buffer, 32, "%d", localtime ? offset->is_dst : 0); break;