# [PATCH] Fix date() bug

- Thread: https://externals.io/message/40726
- Original archive: https://news-web.php.net/php.internals/40726
- Started by Ilia Cheishvili on 2008-09-27T07:04:34+00:00
- Messages: 9
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #40726 — Ilia Cheishvili — 2008-09-27T07:04:34+00:00

URL: https://externals.io/message/40726#40726

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 :)

Thanks!

Ilia

---

## #40727 — Alexey Zakhlestin — 2008-09-27T07:40:52+00:00

URL: https://externals.io/message/40726#40727 · In reply to: #40726

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/

---

## #40730 — Ilia Cheishvili — 2008-09-27T08:04:03+00:00

URL: https://externals.io/message/40726#40730 · In reply to: #40727

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.

Ilia

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:
> > […]
> a
> > […]
> a
> > […]
> am
> > […]
> safeguards
> > […]
>
> Wouldn't it be better, to make `gettimeofday()` call only in case of 'u'?
>
>
> --
> Alexey Zakhlestin
> http://blog.milkfarmsoft.com/

---

## #40731 — Alexey Zakhlestin — 2008-09-27T08:20:42+00:00

URL: https://externals.io/message/40726#40731 · In reply to: #40730

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:
> > […]
>
>



-- 
Alexey Zakhlestin
http://blog.milkfarmsoft.com/

---

## #40732 — Ilia Cheishvili — 2008-09-27T08:26:58+00:00

URL: https://externals.io/message/40726#40732 · In reply to: #40731

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:
> > […]
> preferred
> > […]
>
> 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;
>
>
> > […]
> added
> > […]
>
>
>
> --
> Alexey Zakhlestin
> http://blog.milkfarmsoft.com/

---

## #40734 — Alexey Zakhlestin — 2008-09-27T09:21:18+00:00

URL: https://externals.io/message/40726#40734 · In reply to: #40732

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:
> > […]
>
>



-- 
Alexey Zakhlestin
http://blog.milkfarmsoft.com/

---

## #40735 — Hannes Magnusson — 2008-09-27T09:28:35+00:00

URL: https://externals.io/message/40726#40735 · In reply to: #40726

On Sat, Sep 27, 2008 at 09:04, 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

"Note: Since this function only accepts integer timestamps the u
format character is only useful when using the `date_format()` function
with user based timestamps created with `date_create()`. " ?

-Hannes

---

## #40736 — Alexey Zakhlestin — 2008-09-27T09:43:55+00:00

URL: https://externals.io/message/40726#40736 · In reply to: #40735

On Sat, Sep 27, 2008 at 1:28 PM, Hannes Magnusson
<hannes.magnusson@gmail.com> wrote:
> On Sat, Sep 27, 2008 at 09:04, Ilia Cheishvili
> <ilia.cheishvili@gmail.com> wrote:
> > […]
>
> "Note: Since this function only accepts integer timestamps the u
> format character is only useful when using the `date_format()` function
> with user based timestamps created with `date_create()`. " ?

This is oversimplification, as for "current moment", `date()` can get
microseconds.
For user-provided timestamps it, obviously, can not — that is true.

-- 
Alexey Zakhlestin
http://blog.milkfarmsoft.com/

---

## #40739 — Ilia Cheishvili — 2008-09-27T21:05:08+00:00

URL: https://externals.io/message/40726#40739 · In reply to: #40734

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:
> > […]
>
> 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.
>
> > […]
> in
> > […]
> indeyets@gmail.com>
> > […]
> 'u'?
> > […]
>
>
>
> --
> Alexey Zakhlestin
> http://blog.milkfarmsoft.com/
