Skip to content

time() and friends have Y2038 problem on 64 Windows #17856

Open

Description

@cmb69

Description

I travelled 15 years into the future, and run the following code:

<?php
var_dump(time());

Resulted in this output:

int(-2081770632)

But I expected this output instead:

int(2213196664)

The problem is

tv->tv_sec = (long)(ff / 1000000Ui64);

Casting to long is appropriate since the declaration of struct timeval in WinSock2.h is:

struct timeval {
        long    tv_sec;         /* seconds */
        long    tv_usec;        /* and microseconds */
};

However, long is a 32bit value on LLP64 architectures.

One may argue that time travel is not supportable, but maybe we should heed the POSIX 7 advise:

Applications should use the clock_gettime() function instead of the obsolescent gettimeofday() function.

gettimeofday() has been removed from POSIX 8 altogether.

PHP Version

any

Operating System

Windows

Activity

  1. ndossche commented on Feb 18, 2025

    @ndossche
    Member

    A bit related to #14141

  2. cmb69 commented on Feb 18, 2025

    @cmb69
    MemberAuthor

    Wow, serious mess here. While we're checking for the availability of gettimeofday() during configuration, setting HAVE_GETTIMEOFDAY if so, in a couple of places we just call the function even though it might not be available. Sometimes we check whether the gettimeofday() call failed, and sometimes we just assume it cannot fail. As fallback we're using time(), but still prefer gettimeofday() even when we don't use the microseconds.

    And those calls to gettimeofday() are spread all over the place.

  3. cmb69 commented on Feb 18, 2025

    @cmb69
    MemberAuthor

    Oh, forgeot: instead of using clock_gettime() we can likely use timespec_get() which is a C11 function.

  4. marc-mabe commented on Jul 17, 2025

    @marc-mabe
    Contributor

    @cmb69 I was trying to refactor the internally used time API and come up with a simple wrapper.
    It's basically a function providing the current time and used either timespec_get or clock_gettime or gettimeofdayor just time depending on what is available.

    Do you think this would be the right way of doing it?

    The branch is here https://github.com/marc-mabe/php-src/tree/current_time_wrapper

    PS: I'm completely unsure about the code structure right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions