php / php/php-src

time() and friends have Y2038 problem on 64 Windows

Open
#17,856 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Extension: date Status: Needs Triage
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

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

https://github.com/php/php-src/blob/3d9a0a0c17c1660116c0699e6be734817d616292/win32/time.c#L46

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in win32/time.c around the linked line where time() obtains and casts tv_sec, then review the Windows LLP64 limitation described in the report and the related time APIs. Confirm the behavior on 64-bit Windows and define completion as returning a correct positive timestamp beyond 2038 without the current 32-bit overflow.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, php
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.