From a892bb6f1685b84b74ba37e5a6a24ee8fb45eee7 Mon Sep 17 00:00:00 2001 From: steckbrief Date: Mon, 7 Aug 2017 12:57:57 +0200 Subject: Simplifying introduction of new XEP implementations, implements FS#250 and FS#251 (Privacy and Security settings), refactoring of location of some interfaces --- .../xmpp/time/TimePacketGenerator.java | 31 +++++++++++++++++----- 1 file changed, 25 insertions(+), 6 deletions(-) (limited to 'src/main/java/de/thedevstack/conversationsplus/xmpp/time/TimePacketGenerator.java') diff --git a/src/main/java/de/thedevstack/conversationsplus/xmpp/time/TimePacketGenerator.java b/src/main/java/de/thedevstack/conversationsplus/xmpp/time/TimePacketGenerator.java index 344beb9e..ef381d4d 100644 --- a/src/main/java/de/thedevstack/conversationsplus/xmpp/time/TimePacketGenerator.java +++ b/src/main/java/de/thedevstack/conversationsplus/xmpp/time/TimePacketGenerator.java @@ -1,7 +1,12 @@ package de.thedevstack.conversationsplus.xmpp.time; -import de.thedevstack.conversationsplus.xmpp.iqversion.IqVersionPacket; +import java.util.Locale; +import java.util.TimeZone; + +import de.thedevstack.conversationsplus.generator.AbstractGenerator; +import de.thedevstack.conversationsplus.xml.Element; import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacket; +import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacketGenerator; /** * Generates the IQ Packets for Entity Time @@ -26,12 +31,26 @@ public final class TimePacketGenerator { * @param packet * @return */ - public static TimePacket generateResponse(IqPacket packet) { - TimePacket timePacket = new TimePacket(); - timePacket.setTo(packet.getFrom()); - timePacket.setId(packet.getId()); + public static IqPacket generateResponse(IqPacket packet) { + IqPacket responsePacket = IqPacketGenerator.generateIqResultResponse(packet); + + Element time = responsePacket.addChild(EntityTime.ELEMENT, EntityTime.NAMESPACE); + final long now = System.currentTimeMillis(); + time.addChild("utc").setContent(AbstractGenerator.getTimestamp(now)); + TimeZone ourTimezone = TimeZone.getDefault(); + long offsetSeconds = ourTimezone.getOffset(now) / 1000; + long offsetMinutes = Math.abs((offsetSeconds % 3600) / 60); + long offsetHours = offsetSeconds / 3600; + String hours; + if (offsetHours < 0) { + hours = String.format(Locale.US, "%03d", offsetHours); + } else { + hours = String.format(Locale.US, "%02d", offsetHours); + } + String minutes = String.format(Locale.US, "%02d", offsetMinutes); + time.addChild("tzo").setContent(hours + ":" + minutes); - return timePacket; + return responsePacket; } private TimePacketGenerator() {} -- cgit v1.2.3