diff --git a/source/base/src/main/java/com/jd/blockchain/consts/DataCodes.java b/source/base/src/main/java/com/jd/blockchain/consts/DataCodes.java index e2f8d65b..4085f8cb 100644 --- a/source/base/src/main/java/com/jd/blockchain/consts/DataCodes.java +++ b/source/base/src/main/java/com/jd/blockchain/consts/DataCodes.java @@ -152,4 +152,5 @@ public interface DataCodes { public static final int CONSENSUS_MSGQUEUE_BLOCK_SETTINGS = CONSENSUS_MSGQUEUE | 0x05; + } diff --git a/source/base/src/main/java/com/jd/blockchain/consts/Global.java b/source/base/src/main/java/com/jd/blockchain/consts/Global.java new file mode 100644 index 00000000..b508e833 --- /dev/null +++ b/source/base/src/main/java/com/jd/blockchain/consts/Global.java @@ -0,0 +1,19 @@ +package com.jd.blockchain.consts; + +import java.util.TimeZone; + +public class Global { + + public static final String DEFAULT_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss.SSSZ"; + + public static final String DEFAULT_TIME_ZONE = "GMT+08:00"; + + static { + initialize(); + } + + public static void initialize() { + TimeZone.setDefault(TimeZone.getTimeZone(DEFAULT_TIME_ZONE)); + } + +} diff --git a/source/peer/src/main/java/com/jd/blockchain/peer/PeerServerBooter.java b/source/peer/src/main/java/com/jd/blockchain/peer/PeerServerBooter.java index aba98521..056753fe 100644 --- a/source/peer/src/main/java/com/jd/blockchain/peer/PeerServerBooter.java +++ b/source/peer/src/main/java/com/jd/blockchain/peer/PeerServerBooter.java @@ -1,5 +1,6 @@ package com.jd.blockchain.peer; +import com.jd.blockchain.consts.Global; import com.jd.blockchain.storage.service.DbConnectionFactory; import com.jd.blockchain.tools.initializer.LedgerBindingConfig; import com.jd.blockchain.utils.ArgumentSet; @@ -41,6 +42,11 @@ public class PeerServerBooter { private static final String DEBUG_OPT = "-debug"; public static String ledgerBindConfigFile; + + static { + // 加载 Global ,初始化全局设置; + Global.initialize(); + } public static void main(String[] args) { PeerServerBooter peerServerBooter = new PeerServerBooter(); diff --git a/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/LedgerInitProperties.java b/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/LedgerInitProperties.java index 9030eb77..7b48cec6 100644 --- a/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/LedgerInitProperties.java +++ b/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/LedgerInitProperties.java @@ -9,8 +9,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Properties; -import org.springframework.util.ResourceUtils; - +import com.jd.blockchain.consts.Global; import com.jd.blockchain.crypto.AddressEncoding; import com.jd.blockchain.crypto.PubKey; import com.jd.blockchain.ledger.ParticipantNode; @@ -31,7 +30,7 @@ public class LedgerInitProperties { // 声明的账本建立时间; public static final String CREATED_TIME = "created-time"; // 创建时间的格式; - public static final String CREATED_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss.SSSZ"; + public static final String CREATED_TIME_FORMAT = Global.DEFAULT_TIME_FORMAT; // 共识参与方的个数,后续以 part.id 分别标识每一个参与方的配置; public static final String PART_COUNT = "cons_parti.count"; @@ -162,7 +161,7 @@ public class LedgerInitProperties { public static LedgerInitProperties resolve(Properties props) { return resolve(null, props); } - + public static LedgerInitProperties resolve(String dir, Properties props) { String hexLedgerSeed = PropertiesUtils.getRequiredProperty(props, LEDGER_SEED).replace("-", ""); byte[] ledgerSeed = HexUtils.decode(hexLedgerSeed); diff --git a/source/tools/tools-initializer/src/test/java/test/com/jd/blockchain/tools/initializer/LedgerInitPropertiesTest.java b/source/tools/tools-initializer/src/test/java/test/com/jd/blockchain/tools/initializer/LedgerInitPropertiesTest.java index 703b86b4..3e0a004e 100644 --- a/source/tools/tools-initializer/src/test/java/test/com/jd/blockchain/tools/initializer/LedgerInitPropertiesTest.java +++ b/source/tools/tools-initializer/src/test/java/test/com/jd/blockchain/tools/initializer/LedgerInitPropertiesTest.java @@ -1,17 +1,18 @@ package test.com.jd.blockchain.tools.initializer; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; import java.io.IOException; import java.io.InputStream; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.TimeZone; -import com.jd.blockchain.crypto.AddressEncoding; import org.junit.Test; import org.springframework.core.io.ClassPathResource; +import com.jd.blockchain.crypto.AddressEncoding; import com.jd.blockchain.crypto.PubKey; import com.jd.blockchain.tools.initializer.LedgerInitProperties; import com.jd.blockchain.tools.initializer.LedgerInitProperties.ConsensusParticipantConfig; @@ -22,12 +23,21 @@ public class LedgerInitPropertiesTest { private static String expectedCreatedTimeStr = "2019-08-01 14:26:58.069+0800"; + private static String expectedCreatedTimeStr1 = "2019-08-01 13:26:58.069+0700"; + @Test public void testTimeFormat() throws ParseException { SimpleDateFormat timeFormat = new SimpleDateFormat(LedgerInitProperties.CREATED_TIME_FORMAT); +// timeFormat.setTimeZone(TimeZone.getTimeZone("GMT+08:00")); + TimeZone.setDefault(TimeZone.getTimeZone("GMT+08:00")); + Date time = timeFormat.parse(expectedCreatedTimeStr); String actualTimeStr = timeFormat.format(time); assertEquals(expectedCreatedTimeStr, actualTimeStr); + + Date time1 = timeFormat.parse(expectedCreatedTimeStr1); + String actualTimeStr1 = timeFormat.format(time1); + assertEquals(expectedCreatedTimeStr, actualTimeStr1); } @Test @@ -43,6 +53,7 @@ public class LedgerInitPropertiesTest { assertEquals(expectedLedgerSeed, actualLedgerSeed); SimpleDateFormat timeFormat = new SimpleDateFormat(LedgerInitProperties.CREATED_TIME_FORMAT); + timeFormat.setTimeZone(TimeZone.getTimeZone("GMT+08:00")); long expectedTs = timeFormat.parse(expectedCreatedTimeStr).getTime(); assertEquals(expectedTs, initProps.getCreatedTime()); diff --git a/source/tools/tools-initializer/src/test/resources/ledger.init b/source/tools/tools-initializer/src/test/resources/ledger.init index ad2b5f1f..2d574f93 100644 --- a/source/tools/tools-initializer/src/test/resources/ledger.init +++ b/source/tools/tools-initializer/src/test/resources/ledger.init @@ -3,7 +3,7 @@ ledger.seed=932dfe23-fe23232f-283f32fa-dd32aa76-8322ca2f-56236cda-7136b322-cb323ffe #账本的描述名称;此属性不参与共识,仅仅在当前参与方的本地节点用于描述用途; -ledger.name= +ledger.name=test #声明的账本创建时间;格式为 “yyyy-MM-dd HH:mm:ss.SSSZ”,表示”年-月-日 时:分:秒:毫秒时区“;例如:“2019-08-01 14:26:58.069+0800”,其中,+0800 表示时区是东8区 created-time=2019-08-01 14:26:58.069+0800