php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44474 GregorianToJD wrong return value
Submitted: 2008-03-18 22:21 UTC Modified: 2009-05-03 01:00 UTC
Votes:3
Avg. Score:4.0 ± 0.8
Reproduced:0 of 0 (0.0%)
From: reidw at rawsound dot com Assigned:
Status: No Feedback Package: Date/time related
PHP Version: 5.2.6 OS: Darwin 9.2
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2008-03-18 22:21 UTC] reidw at rawsound dot com
Description:
------------
The function gregroiantojd() returns the wrong value. This was also 
tested with PHP 5.2.2 on Linux FC3 and PHP 5.1.2 on Windows Server 2003 
with the same results. Results are off by one day for positive years, 
and further off for zero and negative years. By definition, when using 
negative years rather than a suffix the pattern should be: ..., -2, -1, 
0, 1, 2, ... . Year 2 corresponds to 2AD, year 1 corresponds to 1AD, 
year 0 corresponds to 1BC, and year -1 corresponds to 2BC, etc. 
Information can be found from the links in the PHP manual. Code for 
comparison is taken from those links and given below.

Reproduce code:
---------------
built-in:
echo gregoriantojd(3,16,1)."\n";
echo gregoriantojd(3,16,0)."\n";
echo gregoriantojd(3,16,-1);


reproduced from PHP manual links:
echo GtoJD(3,16,1)."\n";
echo GtoJD(3,16,0)."\n";
echo GtoJD(3,16,-1);
function GtoJD($m,$d,$y)
{
  return (int) (((int) ( 1461 * ( $y + 4800 + ($m-14) / 12 ) ) / 4) +
    ((int) ( 367 * ( $m - 2 - 12 * ( ($m-14) / 12 ) ) ) / 12) -
    ((int) ( 3 * (int)( ( $y + 4900 + ($m-14) / 12 ) / 100 ) ) / 4) +
    $d - 32075);
}



Expected result:
----------------
The expected results are (from the reproduced code):
1721501
1721137
1720771





Actual result:
--------------
Actual results (from the built-in gregoriantojd()):
1721500
0
1721135





Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-03-18 22:49 UTC] reidw at rawsound dot com
Further research shows that the code for GtoJD to convert a Gregorian 
date to julian days may be off.

But, the function gregoriantojd still gives gross errors for zero and 
negative years.
 [2008-03-19 00:56 UTC] reidw at rawsound dot com
Further examination yields that gregoriantojd is requiring the minus 
sign to be used for years as a direct replacement for "BC". Thus, 
2AD==2, 1AD==1, 1BC==-1, 2BC==-2, etc. Fine. This is not indicated in 
the manual. Also, the manual states that the earliest year is 4714BC. 
This is an error

When the defined date of Jan. 1, 4713 BC (which is supposed to be "day 
zero" of Julian Days, by definition) is entered into each function, -
4713-01-01 for gregoriantojd and -4712-01-01 for GtoJD, BOTH yield '38' 
rather than the expected 'zero'.

Plugging in today's date yields:
2454544 for gregoriantojd;
2454545 for GtoJD.
 [2008-03-28 21:34 UTC] reidw at rawsound dot com
Ah, part of this is my misunderstanding. The definition of -4712-01-01 
needing to produce day zero is referring to Jan. 1, -4712 of the 
Julian Calendar. This would be November 23, -4713 in the proleptic 
Gregorian Calendar using astronomical numbering for the years. And 
taking into account the way gregoriantojd handles negative years, my 
complaint about -4714 being an error was wrong.

Still, this leaves 2 problems:

How does gregoriantojd handle rounding? This brings doubt to this 
function and also questions the equation used for my GtoJD. Also, 
being that Julian Days go from noon to noon and Gregorian days from 
midnight to midnight, does this account for the rounding discrepancy?

How should years before 1AD be handled? The referenced literature 
suggests using astronomical numbering since the minus sign is used for 
years before 1AD. But gregoriantojd substitutes BC (or BCE) usage with 
a minus sign, as shown in my "19 Mar" post, with no year zero.

Thanks for your time.
 [2009-04-25 14:08 UTC] jani@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5.2-latest.tar.gz
 
For Windows:

  http://windows.php.net/snapshots/


 [2009-05-03 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sat Dec 04 14:03:34 2021 UTC