本文整理汇总了Java中org.apache.commons.net.ntp.TimeInfo类的典型用法代码示例。如果您正苦于以下问题:Java TimeInfo类的具体用法?Java TimeInfo怎么用?Java TimeInfo使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TimeInfo类属于org.apache.commons.net.ntp包,在下文中一共展示了TimeInfo类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getCurrentTime
import org.apache.commons.net.ntp.TimeInfo; //导入依赖的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.TimeInfo; //导入依赖的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: getNTPDate
import org.apache.commons.net.ntp.TimeInfo; //导入依赖的package包/类
public static String getNTPDate() {
String[] hosts = new String[] { "ntp02.oal.ul.pt", "ntp04.oal.ul.pt",
"ntp.xs4all.nl", "time.foo.com", "time.nist.gov" };
NTPUDPClient client = new NTPUDPClient();
// We want to timeout if a response takes longer than 5 seconds
client.setDefaultTimeout(2000);
SimpleDateFormat OutPutFormat = new SimpleDateFormat(
"dd/M/yyyy HH:mm:ss", java.util.Locale.getDefault());
for (String host : hosts) {
try {
InetAddress hostAddr = InetAddress.getByName(host);
TimeInfo info = client.getTime(hostAddr);
Date date = new Date(info.getReturnTime());
String out = OutPutFormat.format(date);
return out;
} catch (IOException e) {
e.printStackTrace();
}
}
client.close();
return null;
}
示例4: getNtpServerDate
import org.apache.commons.net.ntp.TimeInfo; //导入依赖的package包/类
/**
* Queries the specified NTP Server for the time.
* Do not query time server more than once every 4 seconds.
* @param timeServerHost the time server host name.
* @return the NTP server {@link Date} or {@code null}.
* @throws IOException
*/
public static Date getNtpServerDate(final String timeServerHost) throws IOException {
try {
TimeInfo timeInfo = null;
final NTPUDPClient timeClient = new NTPUDPClient();
timeClient.setDefaultTimeout(NTP_SERVER_TIMEOUT_MS);
final InetAddress inetAddress = InetAddress.getByName(timeServerHost);
if (inetAddress != null) {
timeInfo = timeClient.getTime(inetAddress);
if (timeInfo != null) {
// TODO: which time to use?
final long serverTime = timeInfo.getMessage().getTransmitTimeStamp().getTime();
//long serverTime = timeInfo.getReturnTime();
final Date ntpDate = new Date(serverTime);
return ntpDate;
}
}
} catch (final IOException e) {
throw new IOException("Unable to get NTP server time.", e);
}
return null;
}
示例5: getTime
import org.apache.commons.net.ntp.TimeInfo; //导入依赖的package包/类
/**
* Queries the given timeserver <code>hostname</code> and returns the time
* in milliseconds.
*
* @param hostname the timeserver to query
* @return the time in milliseconds or the current time of the system if an
* error occurs.
*/
protected static long getTime(String hostname) {
try {
NTPUDPClient timeClient = new NTPUDPClient();
timeClient.setDefaultTimeout(NTP_TIMEOUT);
InetAddress inetAddress = InetAddress.getByName(hostname);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
return timeInfo.getReturnTime();
}
catch (UnknownHostException uhe) {
logger.warn("the given hostname '{}' of the timeserver is unknown -> returning current sytem time instead", hostname);
}
catch (IOException ioe) {
logger.warn("couldn't establish network connection [host '{}'] -> returning current sytem time instead", hostname);
}
return System.currentTimeMillis();
}
示例6: getCurrentNetworkTime
import org.apache.commons.net.ntp.TimeInfo; //导入依赖的package包/类
public long getCurrentNetworkTime() throws IOException{
NTPUDPClient timeClient = new NTPUDPClient();
TimeInfo timeInfo = null;
InetAddress inetAddress = InetAddress.getByName(Constants.TIME_SERVER);
timeInfo = timeClient.getTime(inetAddress);
//long returnTime = timeInfo.getReturnTime(); //local device time
long returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime(); //server time
timeInfo.getMessage().getTransmitTimeStamp().getTime();
Date time = new Date(returnTime);
//TODO make this into a debug mode/verbose?
if(true){
CharSequence text = "Time from " + Constants.TIME_SERVER + ": " + time +
timeInfo.getMessage().getTransmitTimeStamp().toDateString();
Log.d("ntp", text.toString());
}
return returnTime;
}
示例7: getOffset
import org.apache.commons.net.ntp.TimeInfo; //导入依赖的package包/类
private long getOffset(TimeInfo info) throws SystemTimeManagerException {
NtpV3Packet message = info.getMessage();
logStuff(info, message);
info.computeDetails(); // compute offset/delay if not already done
Long offsetValue = info.getOffset();
Long delayValue = info.getDelay();
log.info("Roundtrip delay=" + delayValue + "ms, clock offset="
+ offsetValue + "ms");
if (offsetValue == null) {
throw new SystemTimeManagerException(
"Could not get offset needed to adjust local clock to match remote clock");
} else {
return offsetValue;
}
}
示例8: logStuff
import org.apache.commons.net.ntp.TimeInfo; //导入依赖的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());
}
示例9: fromURL
import org.apache.commons.net.ntp.TimeInfo; //导入依赖的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()));
}
示例10: run
import org.apache.commons.net.ntp.TimeInfo; //导入依赖的package包/类
@Override
public void run() {
try {
final TimeInfo info = get();
info.computeDetails();
EEWBot.instance.getClient().getDispatcher().dispatch(new TimeEvent(EEWBot.instance.getClient(), info));
this.offset = getOffset(info);
} catch (final IOException e) {
Log.logger.error("NTPClient error", e);
}
}
示例11: get
import org.apache.commons.net.ntp.TimeInfo; //导入依赖的package包/类
public static TimeInfo get() throws IOException {
final NTPUDPClient client = new NTPUDPClient();
client.setDefaultTimeout(10000);
client.open();
final InetAddress hostAddr = InetAddress.getByName(EEWBot.instance.getConfig().getNptServer());
return client.getTime(hostAddr);
}
示例12: getTimeUpdates
import org.apache.commons.net.ntp.TimeInfo; //导入依赖的package包/类
/**
* Get the {@link Flux} on which time updates are published.
*
* @return the {@code Flux<TimeInfo>} to receive time updates
*/
public Flux<TimeInfo> getTimeUpdates() {
return Flux.create(sink -> {
synchronized (NTPClock.this) {
timeInfoSinks.add(sink);
}
});
}
示例13: getsTimeUpdatesFromPool
import org.apache.commons.net.ntp.TimeInfo; //导入依赖的package包/类
@Test
public void getsTimeUpdatesFromPool() throws InterruptedException {
NTPClock clock = NTPClock.getInstance();
TimeInfo timeInfo = clock.getTimeUpdates()
.publishOn(Schedulers.elastic())
.blockFirst();
assertNotNull(timeInfo.getAddress().getHostAddress(), "Got time from a real server");
Thread.sleep(500);
assertTrue(System.currentTimeMillis() > timeInfo.getReturnTime(), "Now should be greater than NTP time");
}
示例14: updateOffSet
import org.apache.commons.net.ntp.TimeInfo; //导入依赖的package包/类
private static void updateOffSet()
{
//CREATE CLIENT
NTPUDPClient client = new NTPUDPClient();
//SET TIMEOUT
client.setDefaultTimeout(10000);
try
{
//OPEN CLIENT
client.open();
//GET INFO FROM NTP SERVER
InetAddress hostAddr = InetAddress.getByName(NTP_SERVER);
TimeInfo info = client.getTime(hostAddr);
info.computeDetails();
//UPDATE OFFSET
if(info.getOffset() != null)
{
offset = info.getOffset();
}
}
catch (Exception e)
{
//ERROR GETTING OFFSET
}
client.close();
}
示例15: main
import org.apache.commons.net.ntp.TimeInfo; //导入依赖的package包/类
public static void main(String[] args) {
if (args.length == 0) {
System.err.println("Usage: NTPClient <hostname-or-address-list>");
System.exit(1);
}
NTPUDPClient client = new NTPUDPClient();
// We want to timeout if a response takes longer than 10 seconds
client.setDefaultTimeout(10000);
try {
client.open();
for (String arg : args) {
System.out.println();
try {
InetAddress hostAddr = InetAddress.getByName(arg);
System.out.println("> " + hostAddr.getHostName() + "/"
+ hostAddr.getHostAddress());
TimeInfo info = client.getTime(hostAddr);
processResponse(info);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
} catch (SocketException e) {
e.printStackTrace();
}
client.close();
}