0

So I am porting code from PHP to Java. Part of the code has a time sensitive hashing algorithm, where the current Unix time stamp acts as a seed. I am running into the problem where my ported code is giving me different answers in either language. I suspect it could be a slight difference in implementation or something.

I am wondering if the problem is something rather obscure such as this. Nevertheless, any help would be greatly appreciated.

Here is my code in Java.

private static int generateB() {
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.US);

    Date start;
    Date now;
    try {
        start = sdf.parse("01/01/1970");
        now = new Date();
    } catch (ParseException e) {
        return 402890;
    }
    long diff = now.getTime() - start.getTime();
    long hours = diff / (60 * 60 * 1000) % 24;
    long days = diff / (24 * 60 * 60 * 1000);
    return (int) (hours + days * 24);
}

and here is the PHP code:

 /**
 * Generate "b" parameter
 * The number of hours elapsed, since 1st of January 1970
 *
 * @return int
 */
private function generateB()
{
    $start = new \DateTime('1970-01-01');
    $now = new \DateTime('now');
    $diff = $now->diff($start);
    return $diff->h + ($diff->days * 24);
}

Yet they return different results, off by 3 hours to be exact.

At the time of this post. The PHP returns 403472 while Java returns 403475. Also if I use System.currentTimeMillis() I get 403480.

So my question is why is there any difference at all? I mean subtracting 3 could solve my problem but I am just curious for future reference why the difference exists at all. Note: I am running the PHP via PHP Sandbox for testing purposes.

Skylion
  • 2,696
  • 26
  • 50
  • 1
    What is your timezone? Java timestamps are always in UTC time if I recall. – A.Grandt Jan 11 '16 at 16:49
  • In the Java code you are explicitly defining a timezone by the looks of things ( I don't know much Java ) but in the php code the `DateTime` call has not been given a timezone. I believe that the epoch time is GMT. – Professor Abronsius Jan 11 '16 at 16:50
  • Is it definitely an exact number of hours? Have you checked the number of milliseconds to confirm? If it is, my guess would be some difference in timezones. – DaveyDaveDave Jan 11 '16 at 16:52
  • 2
    Definitely sounds like a TZ issue. For PHP: echo date("Z"); //TZ offset in seconds echo date("T"); // TZ name, e.g. "CST" echo date("e"); // "America/Chicago". – Kevin_Kinsey Jan 11 '16 at 17:00
  • For Java, I would recommend using `System.currentTimeMillis()` since that will return the milliseconds since `1970-01-01 00:00:00Z` regardless of your current timezone (assuming your computer is properly configured etc). Don't bother with parsing `01/01/1970` as that will give you midnight in your timezone. *ETA*: Also, don't bother breaking out hours and days. The total number of hours is just `diff / (60 * 60 * 1000)`. No need to do mod 24 or figure days separately. – dcsohl Jan 11 '16 at 17:12
  • Any particular reason why you're using `01/01/1970` instead of `new Date(0L)`? – chrylis -cautiouslyoptimistic- Jan 11 '16 at 17:18
  • Because I had no idea it existed. – Skylion Jan 11 '16 at 17:34

1 Answers1

0

Judging by your quote

At the time of this post. The PHP returns 403472 while Java returns 403475. Also if I use System.currentTimeMillis() I get 403480.

that all looks alright - there's 3 seconds between your PHP and Java, and 5 seconds on top of that. I guess that's probably the amount of time it took for you to switch process and run the tests?

php > echo date('Y-m-d H:i:s', 403472);
1970-01-05 17:04:32
php > echo date('Y-m-d H:i:s', 403475);
1970-01-05 17:04:35
php > echo date('Y-m-d H:i:s', 403480);
1970-01-05 17:04:40
Farkie
  • 3,307
  • 2
  • 22
  • 33
  • 2
    Look at the code again. OP is trying to calculate hours since the epoch, so it's not three seconds; it's three ***hours***. – dcsohl Jan 11 '16 at 17:11