Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 31 additions & 42 deletions ext/date/lib/unixtime2tm.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,80 +32,69 @@ static int month_tab[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 3
/* Converts a Unix timestamp value into broken down time, in GMT */
void timelib_unixtime2gmt(timelib_time* tm, timelib_sll ts)
{
timelib_sll days, remainder, tmp_days;
timelib_sll days = 0, remainder = 0;
timelib_sll cur_year = 1970;
timelib_sll i;
timelib_sll hours, minutes, seconds;
int *months;

days = ts / SECS_PER_DAY;
remainder = ts - (days * SECS_PER_DAY);
remainder = ts;
days = remainder / SECS_PER_DAY;
remainder %= SECS_PER_DAY;

if (ts < 0 && remainder == 0) {
days++;
remainder -= SECS_PER_DAY;
}
TIMELIB_DEBUG(printf("days=%lld, rem=%lld\n", days, remainder););

if (ts >= 0) {
tmp_days = days + 1;
if (days >= DAYS_PER_LYEAR_PERIOD || days <= -DAYS_PER_LYEAR_PERIOD) {
cur_year += days / DAYS_PER_LYEAR_PERIOD * YEARS_PER_LYEAR_PERIOD;
days %= DAYS_PER_LYEAR_PERIOD;
}

if (tmp_days > DAYS_PER_LYEAR_PERIOD || tmp_days <= -DAYS_PER_LYEAR_PERIOD) {
cur_year += YEARS_PER_LYEAR_PERIOD * (tmp_days / DAYS_PER_LYEAR_PERIOD);
tmp_days -= DAYS_PER_LYEAR_PERIOD * (tmp_days / DAYS_PER_LYEAR_PERIOD);
}
TIMELIB_DEBUG(printf("days=%lld, year=%lld\n", days, cur_year););

while (tmp_days >= DAYS_PER_LYEAR) {
if (ts >= 0) {
days++;

while (days >= DAYS_PER_LYEAR) {
cur_year++;
if (timelib_is_leap(cur_year)) {
tmp_days -= DAYS_PER_LYEAR;
days -= DAYS_PER_LYEAR;
} else {
tmp_days -= DAYS_PER_YEAR;
days -= DAYS_PER_YEAR;
}
}
} else {
tmp_days = days;

/* Guess why this might be for, it has to do with a pope ;-). It's also
* only valid for Great Brittain and it's colonies. It needs fixing for
* other locales. *sigh*, why is this crap so complex! */
/*
if (ts <= TIMELIB_LL_CONST(-6857352000)) {
tmp_days -= 11;
}
*/
}
else {
remainder += SECS_PER_DAY;

while (tmp_days <= 0) {
if (tmp_days < -1460970) {
cur_year -= 4000;
TIMELIB_DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););
tmp_days += 1460970;
while (days <= 0) {
cur_year--;
if (timelib_is_leap(cur_year)) {
days += DAYS_PER_LYEAR;
} else {
cur_year--;
TIMELIB_DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););
if (timelib_is_leap(cur_year)) {
tmp_days += DAYS_PER_LYEAR;
} else {
tmp_days += DAYS_PER_YEAR;
}
days += DAYS_PER_YEAR;
}
}
remainder += SECS_PER_DAY;
}
TIMELIB_DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););

TIMELIB_DEBUG(printf("days=%lld, year=%lld\n", days, cur_year););

months = timelib_is_leap(cur_year) ? month_tab_leap : month_tab;
if (timelib_is_leap(cur_year) && cur_year < 1970) {
tmp_days--;
days--;
}

i = 11;
while (i > 0) {
TIMELIB_DEBUG(printf("month=%lld (%d)\n", i, months[i]););
if (tmp_days > months[i]) {
if (days > months[i]) {
break;
}
i--;
}
TIMELIB_DEBUG(printf("A: ts=%lld, year=%lld, month=%lld, day=%lld,", ts, cur_year, i + 1, tmp_days - months[i]););
TIMELIB_DEBUG(printf("A: ts=%lld, year=%lld, month=%lld, day=%lld,", ts, cur_year, i + 1, days - months[i]););
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above


/* That was the date, now we do the tiiiime */
hours = remainder / 3600;
Expand All @@ -115,7 +104,7 @@ void timelib_unixtime2gmt(timelib_time* tm, timelib_sll ts)

tm->y = cur_year;
tm->m = i + 1;
tm->d = tmp_days - months[i];
tm->d = days - months[i];
tm->h = hours;
tm->i = minutes;
tm->s = seconds;
Expand Down
40 changes: 40 additions & 0 deletions ext/date/tests/bug75088_date.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
Bug #75088 getTransitions is slow with large values
--FILE--
<?php

$timestamps = [
-9223372036854775808,
-2208988800,
-2203891200,
0,
946684800,
951782400,
1078012800,
3191032750586206520,
5278333347477533876,
6202182859306740885,
7060696112971042445,
9223372036854775807
];

foreach($timestamps as $timestamp) {
printf("%d: %s\n", $timestamp, date('Y-m-d H:i:s', $timestamp));
}

?>
==DONE==
--EXPECT--
-9223372036854775808: -292277022657-01-27 08:29:52
-2208988800: 1900-01-01 00:00:00
-2203891200: 1900-03-01 00:00:00
0: 1970-01-01 00:00:00
946684800: 2000-01-01 00:00:00
951782400: 2000-02-29 00:00:00
1078012800: 2004-02-29 00:00:00
3191032750586206520: 101119804369-12-31 02:15:20
5278333347477533876: 167263727169-12-31 08:17:56
6202182859306740885: 196539352769-12-31 18:14:45
7060696112971042445: 223744554769-12-31 17:34:05
9223372036854775807: 292277026596-12-04 15:30:07
==DONE==
Loading