本文整理汇总了Java中org.apache.commons.net.ntp.TimeStamp类的典型用法代码示例。如果您正苦于以下问题:Java TimeStamp类的具体用法?Java TimeStamp怎么用?Java TimeStamp使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TimeStamp类属于org.apache.commons.net.ntp包,在下文中一共展示了TimeStamp类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getCurrentTime
import org.apache.commons.net.ntp.TimeStamp; //导入依赖的package包/类
private long getCurrentTime() {
long currentTime = 0;
NTPUDPClient client = new NTPUDPClient();
client.setDefaultTimeout(WAIT_FOR_SERVER_RESPONSE);
try {
client.open();
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, MMM dd yyyy HH:mm:ss.SSS zzz");
for (String server : NTP_SERVERS) {
try {
InetAddress ioe = InetAddress.getByName(server);
TimeInfo info = client.getTime(ioe);
TimeStamp ntpTime = TimeStamp.getNtpTime(info.getReturnTime());
return ntpTime.getTime();
} catch (Exception e2) {
System.out.println("Can't get response from server: " + server + ".");
}
}
} catch (SocketException se) {
System.out.println("Can't open client session");
} finally {
client.close();
}
return currentTime;
}
示例2: onCommand
import org.apache.commons.net.ntp.TimeStamp; //导入依赖的package包/类
@Override
public void onCommand(final MessageReceivedEvent e, final String[] args) {
EEWBot.instance.getExecutor().execute(() -> {
try {
final StringBuilder sb = new StringBuilder();
final TimeInfo info = NTPDispatcher.get();
info.computeDetails();
final NtpV3Packet message = info.getMessage();
final TimeStamp origNtpTime = message.getOriginateTimeStamp();
sb.append("コンピューターの時刻: `").append(origNtpTime.toDateString()).append("`\n");
final TimeStamp refNtpTime = message.getReferenceTimeStamp();
sb.append("サーバーからの時刻: `").append(refNtpTime.toDateString()).append("`\n");
final long offset = NTPDispatcher.getOffset(info);
sb.append("オフセット: `").append(offset).append("ms`");
reply(e, sb.toString());
NTPDispatcher.INSTANCE.setOffset(offset);
} catch (final IOException ex) {
Log.logger.error(ExceptionUtils.getStackTrace(ex));
reply(e, ":warning: エラーが発生しました");
}
});
}
示例3: setUpEnv
import org.apache.commons.net.ntp.TimeStamp; //导入依赖的package包/类
@BeforeClass
public void setUpEnv() throws Exception {
startTime = new TimeStamp(new Date());
repository = new StandardRepository();
repository
.addCommandSetProvider(new PackageCommandSetProvider(new Package[]{RepositoryTest.class.getPackage()}));
repository.addEventSetProvider(new PackageEventSetProvider(new Package[]{RepositoryTest.class.getPackage()}));
journal = createJournal();
repository.setJournal(journal);
timeProvider = new NTPServerTimeProvider(new String[]{"localhost"});
repository.setPhysicalTimeProvider(timeProvider);
indexEngine = createIndexEngine();
repository.setIndexEngine(indexEngine);
lockProvider = new LocalLockProvider();
repository.setLockProvider(lockProvider);
repository.startAsync().awaitRunning();
long size = journal.size(EntityLayoutIntroduced.class);
assertTrue(size > 0);
// make sure layout introductions don't duplicate
repository.publish(new IntroduceEntityLayouts(Iterables.concat(repository.getCommands(), repository.getEvents()))).join();
assertEquals(journal.size(EntityLayoutIntroduced.class), size);
}
示例4: getTimestamp
import org.apache.commons.net.ntp.TimeStamp; //导入依赖的package包/类
TimeStamp getTimestamp() throws TimeoutException {
if (timestamp == null) {
throw new TimeoutException();
}
TimeStamp ts = new TimeStamp(timestamp.ntpValue());
long fraction = ts.getFraction();
long seconds = ts.getSeconds();
long nanoTime = System.nanoTime();
long l = (nanoTime - nano) / 1_000_000_000;
double v = (nanoTime - nano) / 1_000_000_000.0 - l;
long i = (long) (v * 1_000_000_000);
long fraction_ = fraction + i;
if (fraction_ >= 1_000_000_000) {
fraction_ -= 1_000_000_000;
l++;
}
return new TimeStamp((seconds + l) << 32 | fraction_);
}
示例5: test
import org.apache.commons.net.ntp.TimeStamp; //导入依赖的package包/类
private void test(HashMap<String, HashMap<String, Set<PairXMLData>>> data, String model) {
// write the data to a file to be passed as an argument to the EOPRunner
ExperimenterFileUtils.writeDataToFile(data, tmpTestFile, language);
// replace the name of the model in the configuration file
ExperimenterFileUtils.writeModelInConfig(options.config,options.model);
String outFileStem = options.output + TimeStamp.getCurrentTime().toString();
// make the list of arguments for the EOPRunner
String[] args = new String[] {"-config", options.config, "-test", "-testFile", tmpTestFile, "-output", outFileStem, "-score"};
logger.info("Running the EOP with arguments: " + StringUtils.join(args," "));
if (! options.fakeRun) {
EOPRunner runner = new EOPRunner(args);
runner.run();
}
}
示例6: logStuff
import org.apache.commons.net.ntp.TimeStamp; //导入依赖的package包/类
private void logStuff(TimeInfo info, NtpV3Packet message) {
if (!log.isDebugEnabled()) {
return;
}
TimeStamp refNtpTime = message.getReferenceTimeStamp();
log.debug("Reference Timestamp:\t" + refNtpTime.toDateString());
// Originate Time is time request sent by client (t1)
TimeStamp origNtpTime = message.getOriginateTimeStamp();
log.debug("Originate Timestamp:\t" + origNtpTime.toDateString());
// Receive Time is time request received by server (t2)
TimeStamp rcvNtpTime = message.getReceiveTimeStamp();
log.debug("Receive Timestamp:\t" + rcvNtpTime.toDateString());
// Transmit time is time reply sent by server (t3)
TimeStamp xmitNtpTime = message.getTransmitTimeStamp();
log.debug("Transmit Timestamp:\t" + xmitNtpTime.toDateString());
// Destination time is time reply received by client (t4)
TimeStamp destNtpTime = TimeStamp.getNtpTime(info.getReturnTime());
log.debug("Destination Timestamp:\t" + destNtpTime.toDateString());
}
示例7: fromURL
import org.apache.commons.net.ntp.TimeStamp; //导入依赖的package包/类
private Single<Long> fromURL(final String ntpServerURL) {
return Single
.fromCallable(() -> InetAddress.getByName(ntpServerURL))
.doOnSubscribe(d -> logger.debug("Fetching NTP from " + ntpServerURL))
.map(ntpUDPClient::getTime)
.map(TimeInfo::getMessage)
.map(NtpV3Packet::getTransmitTimeStamp)
.map(TimeStamp::getTime)
.doOnError(e -> logger.error("NTP fetch task failed with error: " + e.getMessage()));
}
示例8: initialTimestamp
import org.apache.commons.net.ntp.TimeStamp; //导入依赖的package包/类
@Test @SneakyThrows
public void initialTimestamp() {
HybridTimestamp t = repository.getTimestamp();
long ts = t.timestamp();
TimeStamp soon = new TimeStamp(new Date(new Date().toInstant().plus(1, ChronoUnit.SECONDS).toEpochMilli()));
TimeStamp t1 = new TimeStamp(ts);
assertTrue(HybridTimestamp.compare(t1, startTime) > 0);
assertTrue(HybridTimestamp.compare(t1, soon) < 0);
}
示例9: compare
import org.apache.commons.net.ntp.TimeStamp; //导入依赖的package包/类
public static int compare(TimeStamp t1, TimeStamp t2) {
if (t1.getSeconds() == t2.getSeconds() && t1.getFraction() == t2.getFraction()) {
return 0;
}
if (t1.getSeconds() == t2.getSeconds()) {
return t1.getFraction() < t2.getFraction() ? -1 : 1;
}
return t1.getSeconds() < t2.getSeconds() ? -1 : 1;
}
示例10: getSerializableComparable
import org.apache.commons.net.ntp.TimeStamp; //导入依赖的package包/类
@Override public BigInteger getSerializableComparable() {
TimeStamp t = new TimeStamp(logicalTime);
return BigInteger.valueOf(t.getSeconds())
.shiftLeft(64)
.add(BigInteger.valueOf(t.getFraction()).shiftLeft(32))
.add(BigInteger.valueOf(logicalCounter));
}
示例11: secondsPassed
import org.apache.commons.net.ntp.TimeStamp; //导入依赖的package包/类
@Test(successPercentage = 99, dataProvider = "delays")
public void secondsPassed(int delay) throws UnknownHostException, InterruptedException, TimeoutException {
TimeStamp ts1 = provider.getTimestamp();
Thread.sleep(delay);
TimeStamp ts2 = provider.getTimestamp();
long seconds = delay / 1000;
// Verify that seconds passed were calculated properly
// since the last time NTP timestamp was fetched. Measuring fractions
// is pointless as there's a gap between sleeping and requesting the timestamp.
assertEquals("Delay=" + delay + " time_diff=" + (ts2.getTime() - ts1.getTime()), seconds,
(ts2.getTime() - ts1.getTime()) / 1000);
}
示例12: HybridTimestamp
import org.apache.commons.net.ntp.TimeStamp; //导入依赖的package包/类
public HybridTimestamp(PhysicalTimeProvider physicalTimeProvider) {
this(physicalTimeProvider, new TimeStamp(new Date(0)).ntpValue(), 0);
}
示例13: toString
import org.apache.commons.net.ntp.TimeStamp; //导入依赖的package包/类
public String toString() {
String logical = new TimeStamp(logicalTime).toUTCString();
String timeStamp = new TimeStamp(timestamp()).toUTCString();
return "<HybridTimestamp logical=" + logical + "@" + logicalCounter + " NTP=" + timeStamp + "/" + timestamp
() + ">";
}
示例14: initialTimestamp
import org.apache.commons.net.ntp.TimeStamp; //导入依赖的package包/类
@Test
public void initialTimestamp() {
HybridTimestamp timestamp = new HybridTimestamp(physicalTimeProvider);
TimeStamp ntpTime = new TimeStamp(timestamp.getLogicalTime());
assertEquals(ntpTime.getDate(), new Date(0));
}
示例15: Alert
import org.apache.commons.net.ntp.TimeStamp; //导入依赖的package包/类
/**
* Load an alert from a idmef:Alert element
*
* @param alertElement the DOM element idmef:Alert
*/
public Alert(Element alertElement) {
Namespace idmefNamespace = Namespace.getNamespace("http://iana.org/idmef");
if (alertElement == null || !alertElement.getName().equals("Alert")) {
throw new IllegalStateException("The IDMEF alert to parse is not valid");
}
Element createTimeElement = alertElement.getChild("CreateTime", idmefNamespace);
if (createTimeElement != null) {
timestamp = new TimeStamp(createTimeElement.getAttributeValue("ntpstamp").replaceAll("0x", "")).getDate();
}
Element detectTimeElement = alertElement.getChild("DetectTime", idmefNamespace);
if (detectTimeElement != null) {
timestamp = new TimeStamp(detectTimeElement.getAttributeValue("ntpstamp").replaceAll("0x", "")).getDate();
}
if (timestamp == null)
throw new IllegalStateException("invalid timestamp for the IDMEF alert");
//sources
for (Element sourceElement : alertElement.getChildren("Source", idmefNamespace)) {
Element sourceNode = sourceElement.getChild("Node", idmefNamespace);
if (sourceNode != null) {
Element sourceAddress = sourceNode.getChild("Address", idmefNamespace);
//If there is an address
if (sourceAddress != null) {
Element sourceIP = sourceAddress.getChild("address", idmefNamespace);
Element sourceMask = sourceAddress.getChild("netmask", idmefNamespace);
if (sourceIP != null && sourceMask != null) {
this.sources.add(sourceIP.getText() + "/" + sourceMask);
} else if (sourceIP != null) {
this.sources.add(sourceIP.getText());
}
} else { // There is no address, their must be a "name"
Element sourceName = sourceNode.getChild("name", idmefNamespace);
if (sourceName != null) {
this.sources.add(sourceName.getText());
}
}
}
}
//targets
for (Element targetElement : alertElement.getChildren("Target", idmefNamespace)) {
Element targetNode = targetElement.getChild("Node", idmefNamespace);
if (targetNode != null) {
Element targetAddress = targetNode.getChild("Address", idmefNamespace);
//If there is an address
if (targetAddress != null) {
Element targetIP = targetAddress.getChild("address", idmefNamespace);
Element targetMask = targetAddress.getChild("netmask", idmefNamespace);
if (targetIP != null && targetMask != null) {
this.targets.add(targetIP.getText() + "/" + targetMask);
} else if (targetIP != null) {
this.targets.add(targetIP.getText());
}
} else { // There is no address, their must be a "name"
Element targetName = targetNode.getChild("name", idmefNamespace);
if (targetName != null) {
this.targets.add(targetName.getText());
}
}
}
}
//add classification information
Element classificationElement = alertElement.getChild("Classification", idmefNamespace);
if (classificationElement != null) {
this.name = classificationElement.getAttributeValue("text");
for (Element referenceElement : classificationElement.getChildren("Reference", idmefNamespace)) {
switch (referenceElement.getAttributeValue("origin")) {
case "cve":
cveLinks.put(referenceElement.getChild("name", idmefNamespace).getText(), referenceElement.getChild("url", idmefNamespace).getText());
break;
}
}
}
}