Re: [PATCH] Fix date() bug
| From: | Ilia Cheishvili | Date: | Sat, 27 Sep 2008 21:05:08 +0000 |
| Subject: | Re: [PATCH] Fix date() bug | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-40739@lists.php.net to get a copy of this message | ||
I did some digging around in the code and came up with the solution that you
suggested. I put the fix in unixtime2tm.c, letting php_date.c remain the
nice abstraction layer between the system-dependent code of getting the time
and actually formatting it for display. The patch is attached.
Thanks for the back and forth thus far :)
Ilia
On Sat, Sep 27, 2008 at 3:21 AM, Alexey Zakhlestin <indeyets@gmail.com>wrote:
> On Sat, Sep 27, 2008 at 12:26 PM, Ilia Cheishvili
> <ilia.cheishvili@gmail.com> wrote:
> > Ah, I see. I like that even better :)
> > And I attached yet another patch that incorporates your idea.
>
> thanks.
> I did some more code-digging, and it looks, like proper point for
> fixing is not here, anyway.
>
> It should be in timelib_unixtime2gmt() and timelib_unixtime2local()
> funcitons of ext/date/lib/unixtime2tm.c
> "->f" member of structure is not set there, which results in zero's in
> output.
>
> date() function is an abstraction and should not deal with such details
> itself.
>
> >
> > 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/
> >
> >
>
>
>
> --
> Alexey Zakhlestin
> http://blog.milkfarmsoft.com/
>
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 21:00:49 -0000 @@ -1150,7 +1150,7 @@ 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': length = slprintf(buffer, 32, "%06d", (int) t->f); break; /* timezone */ case 'I': length = slprintf(buffer, 32, "%d", localtime ? offset->is_dst : 0); break; Index: lib/unixtime2tm.c =================================================================== RCS file: /repository/php-src/ext/date/lib/unixtime2tm.c,v retrieving revision 1.12.2.4.2.3.2.2 diff -u -b -r1.12.2.4.2.3.2.2 unixtime2tm.c --- lib/unixtime2tm.c 7 Apr 2008 17:44:03 -0000 1.12.2.4.2.3.2.2 +++ lib/unixtime2tm.c 27 Sep 2008 21:00:49 -0000 @@ -32,6 +32,15 @@ #include <strings.h> #endif +#ifdef PHP_WIN32 +#include "win32/time.h" +#elif defined(NETWARE) +#include <sys/timeval.h> +#include <sys/time.h> +#else +#include <sys/time.h> +#endif + static int month_tab_leap[12] = { -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; static int month_tab[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; @@ -44,6 +53,8 @@ timelib_sll i; timelib_sll hours, minutes, seconds; int *months; + struct timeval tp = {0}; + struct timezone tz = {0}; days = ts / SECS_PER_DAY; remainder = ts - (days * SECS_PER_DAY); @@ -126,6 +137,11 @@ tm->sse_uptodate = 1; tm->tim_uptodate = 1; tm->is_localtime = 0; + +#ifdef HAVE_GETTIMEOFDAY + gettimeofday(&tp, &tz); + tm->f = tp.tv_usec; +#endif } void timelib_update_from_sse(timelib_time *tm)
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 21:00:49 -0000 @@ -1150,7 +1150,7 @@ 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': length = slprintf(buffer, 32, "%06d", (int) t->f); break; /* timezone */ case 'I': length = slprintf(buffer, 32, "%d", localtime ? offset->is_dst : 0); break; Index: lib/unixtime2tm.c =================================================================== RCS file: /repository/php-src/ext/date/lib/unixtime2tm.c,v retrieving revision 1.12.2.4.2.3.2.2 diff -u -b -r1.12.2.4.2.3.2.2 unixtime2tm.c --- lib/unixtime2tm.c 7 Apr 2008 17:44:03 -0000 1.12.2.4.2.3.2.2 +++ lib/unixtime2tm.c 27 Sep 2008 21:00:49 -0000 @@ -32,6 +32,15 @@ #include <strings.h> #endif +#ifdef PHP_WIN32 +#include "win32/time.h" +#elif defined(NETWARE) +#include <sys/timeval.h> +#include <sys/time.h> +#else +#include <sys/time.h> +#endif + static int month_tab_leap[12] = { -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; static int month_tab[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; @@ -44,6 +53,8 @@ timelib_sll i; timelib_sll hours, minutes, seconds; int *months; + struct timeval tp = {0}; + struct timezone tz = {0}; days = ts / SECS_PER_DAY; remainder = ts - (days * SECS_PER_DAY); @@ -126,6 +137,11 @@ tm->sse_uptodate = 1; tm->tim_uptodate = 1; tm->is_localtime = 0; + +#ifdef HAVE_GETTIMEOFDAY + gettimeofday(&tp, &tz); + tm->f = tp.tv_usec; +#endif } void timelib_update_from_sse(timelib_time *tm)