我有一个导致整数溢出的错误,导致错误(负)时间戳被写入数据库。代码已经修复了,但我也想修复错误的数据。
我想,我可以只取错误的结果并添加 Integer.MAX_VALUE,但这似乎不起作用,它给我留下了很高的价值。我offset
在下面的代码片段中有值,但没有存储输入值。
以下代码重现了该错误:
@Test
public void testArexxConversion()
{
// The input values represent seconds since midnight, Jan 1, 2000 UTC
final int sample = 361450072; // A sample input value drawn from production
// I use the offset from the UNIX epoch to convert the vakue to UNIX seconds
final int offset = 946684800; // midnight, Jan 01 2000 UTC in UNIX seconds
// This was the buggy line in my code, the assertion will fail
long result = (sample + offset) * 1000;
// Prints 'Result is negative: -1830153280'
Assert.assertTrue(result > 0, String.format("Result is negative: %d", result));
// This is for comparison
Date dt = new Date(offset * 1000);
Assert.assertEquals(dt.getTime() + sample * 1000, result);
}