时间问题处理

This commit is contained in:
lu7213 committed 2026-11-02 02:20:47 -05:00
1 parent 41190a25ae
commit d910c1afe8
66 files changed
+604 -393

No files matched your search

@@ -15,7 +15,6 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.*;
@@ -85,7 +84,7 @@ public class RentalHistoryCleanupTest {
private void block(String network, long height, LocalDateTime chainTime, LocalDateTime stored, int complete) {
store.execute("INSERT INTO rent_history_block (network,block_height,block_id,parent_id,block_time,"
+ "events_complete,created_at) VALUES (?,?,?,?,?,?,?)", network, height, hash(height),
hash(height - 1), chainTime.toInstant(ZoneOffset.UTC).toEpochMilli(), complete, stored);
hash(height - 1), time(chainTime).toEpochMilli(), complete, stored);
}
private void anchor(long height) {
@@ -412,7 +412,7 @@ public class RentalHistoryTest {
public void networkLeasePreventsSecondCollectorRpcCalls() {
collect(100);
store.execute("UPDATE rent_history_cursor SET lease_owner='other',"
+ "lease_until=TIMESTAMPADD(SECOND,60,UTC_TIMESTAMP(3)) WHERE network='NILE'");
+ "lease_until=TIMESTAMPADD(SECOND,60,CURRENT_TIMESTAMP(3)) WHERE network='NILE'");
int before = calls;
currentHead = 101;
service.collect("NILE");
@@ -0,0 +1,37 @@
package com.ruoyi.system.rental;
import org.junit.Test;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.TimeZone;
import static org.junit.Assert.*;
import static com.ruoyi.system.rental.RentalValues.*;
/** 非UTC环境验证DATETIME墙上时间与接口时间点,测试后恢复全局时区。 */
public class RentalTimeZoneTest {
@Test
public void databaseAndResponsePreserveInstantInDifferentServerZones() throws Exception {
TimeZone original = TimeZone.getDefault();
Instant event = Instant.parse("2026-11-02T06:00:00.123Z");
try {
for (String selected : new String[] {"Asia/Shanghai", "UTC", "America/New_York"}) {
TimeZone.setDefault(TimeZone.getTimeZone(selected));
LocalDateTime wallTime = local(event);
assertEquals(event, time(wallTime));
assertEquals(event, time(Timestamp.valueOf(wallTime)));
java.lang.reflect.Method convert = RentalResponse.class.getDeclaredMethod("convert", Object.class);
convert.setAccessible(true);
assertEquals(event.toString(), convert.invoke(new RentalResponse(), Timestamp.valueOf(wallTime)));
assertTrue(Math.abs(time(now()).toEpochMilli() - System.currentTimeMillis()) < 2000);
assertTrue(Math.abs(time(after(60)).toEpochMilli() - System.currentTimeMillis() - 60000) < 2000);
if ("Asia/Shanghai".equals(selected)) {
assertEquals(14, wallTime.getHour());
}
}
} finally {
TimeZone.setDefault(original);
}
}
}
@@ -272,7 +272,7 @@ public class RentalWithdrawTest {
private Map<String, Object> event(Map<String, Object> order, String hash, int index) {
return map("network", "NILE", "transaction_id", hash, "log_index", index, "token_contract", SENDER,
"from_address", SENDER, "to_address", RECEIVER, "amount_units", 30000000, "amount_usdt", money("30"),
"block_number", 10, "block_hash", HASH, "block_time", LocalDateTime.ofInstant(time(order.get("approved_at")).plusSeconds(1), java.time.ZoneOffset.UTC));
"block_number", 10, "block_hash", HASH, "block_time", LocalDateTime.ofInstant(time(order.get("approved_at")).plusSeconds(1), RentalValues.zone()));
}
private static void await(CountDownLatch latch) { try { latch.await(); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new IllegalStateException(ex); } }
private static void rejected(Runnable action) { try { action.run(); fail("必须拒绝"); } catch (IllegalArgumentException expected) { /* 预期校验 */ } }